server.web.db.engine
Database engine management using FastAPI lifespan pattern.
This module provides database engine creation and lifecycle management without global singletons. The engine is stored in FastAPI’s app.state and managed through the db_lifespan context manager.
Attributes
Functions
Create a SQLAlchemy engine from the current configuration. |
|
|
Manage database engine lifecycle via FastAPI lifespan. |
|
Check if the dialect is MySQL. |
|
Check if the dialect is SQLite. |
Module Contents
- server.web.db.engine.log
- server.web.db.engine.create_engine_from_config() tuple[sqlalchemy.engine.Engine, str, Dict[str, Any]]
Create a SQLAlchemy engine from the current configuration.
- Returns:
Tuple of (engine, dialect_name, config_info) where config_info contains the pool settings and connect args used for debugging.
- async server.web.db.engine.db_lifespan(app: fastapi.FastAPI) AsyncIterator[None]
Manage database engine lifecycle via FastAPI lifespan.
Creates the database engine and sessionmaker on startup, stores them in app.state, and properly disposes of the engine on shutdown.
Usage:
app = FastAPI(lifespan=db_lifespan) # In route handlers: def get_db(request: Request): SessionLocal = request.app.state.db_sessionmaker ...