How do I share the same object between separate fast-api routers?

Viewed 188

I have an object that is a data-base adapter layer.

adapter = MyDBAdapter()

I would like to be able to pass an instance of that adapter object to both routes.

from fastapi import FastAPI
from .routers import foo, bar

app = FastAPI()
app.include_router(foo.router)
app.include_router(bar.router)

For the sake of not having to instantiate two instances of the data-base connection, I was wondering if there was a pythonic fast-api way to pass in the adapter object reference? Or would it make more sense to just instantiate it twice in both routes?

0 Answers
Related