mfreeborn / fastapi-sqlalchemy

Adds simple SQLAlchemy support to FastAPI
MIT License
594 stars 34 forks source link

[Question] startup and shutdown: AttributeError: type object 'DBSession' has no attribute 'connected' #40

Closed scheung38 closed 2 years ago

scheung38 commented 2 years ago

from the standard SQLALCHEMY there is db.connect() and db.disconnect() inside startup and shutdown event respectively, how to do it here thanks

api = FastAPI()
api.add_middleware(DBSessionMiddleware, db_url=DATABASE_URL)

@api.on_event("startup")
async def startup():
    await db.connected()  <-- ?

@api.on_event("shutdown")
async def shutdown():
    await db.disconnect() <--- replace with db.session.close_all() ?
mfreeborn commented 2 years ago

A new session is created before each request and closed after each request.

scheung38 commented 2 years ago

So we don't need it here on startup and shutdown? As response seems slow when making FastAPI request