fire-planner/fire_planner/api/dependencies.py

19 lines
639 B
Python
Raw Normal View History

"""Shared FastAPI dependencies — DB session per request."""
from __future__ import annotations
from collections.abc import AsyncIterator
from fastapi import Request
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
async def get_session(request: Request) -> AsyncIterator[AsyncSession]:
"""Yield an AsyncSession bound to the engine on app.state.
The engine + session factory are wired up in `app.lifespan`. Tests
swap them out via dependency_overrides.
"""
factory: async_sessionmaker[AsyncSession] = request.app.state.session_factory
async with factory() as session:
yield session