from typing import AsyncGenerator, Generator from sqlalchemy.orm import sessionmaker from sqlalchemy.ext.asyncio import AsyncSession from app.db.engine import engine, async_engine # ========================= # SYNCHRONOUS SESSION # ========================= SessionLocal = sessionmaker( bind=engine, autocommit=False, autoflush=False, future=True ) def get_db() -> Generator: """ Yield a synchronous SQLAlchemy session Usage: Depends(get_db) """ db = SessionLocal() try: yield db finally: db.close() # ========================= # ASYNCHRONOUS SESSION # ========================= AsyncSessionLocal = sessionmaker( bind=async_engine, class_=AsyncSession, expire_on_commit=False, autocommit=False, autoflush=False, future=True ) async def get_async_db() -> AsyncGenerator: """ Yield an asynchronous SQLAlchemy session Usage: Depends(get_async_db) """ async with AsyncSessionLocal() as session: yield session