"""Async engine, session factory and the per-request session dependency.""" import os from collections.abc import AsyncIterator from typing import Annotated from fastapi import Depends from sqlalchemy.ext.asyncio import AsyncAttrs, AsyncSession, async_sessionmaker, create_async_engine from sqlalchemy.orm import DeclarativeBase DATABASE_URL = os.environ.get("DATABASE_URL", "sqlite+aiosqlite:///./app.db") engine = create_async_engine(DATABASE_URL, pool_pre_ping=True) # expire_on_commit=False: attributes stay loaded after commit, so building the # response does not trigger a lazy load (which raises under asyncio). SessionFactory = async_sessionmaker(engine, expire_on_commit=False) class Base(AsyncAttrs, DeclarativeBase): pass async def get_session() -> AsyncIterator[AsyncSession]: async with SessionFactory() as session: yield session SessionDep = Annotated[AsyncSession, Depends(get_session)]