"""Database engine, session factory, and declarative base. All services use the async SQLAlchemy API via ``get_session()``. Alembic migrations use the synchronous psycopg2 URL (see alembic/env.py). """ from __future__ import annotations from collections.abc import AsyncGenerator from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine from sqlalchemy.orm import DeclarativeBase from shared.config import settings engine = create_async_engine( settings.DATABASE_URL, pool_pre_ping=True, echo=False, ) async_session = async_sessionmaker(engine, expire_on_commit=False) class Base(DeclarativeBase): """Shared declarative base for all ORM models.""" async def get_session() -> AsyncGenerator[AsyncSession, None]: """FastAPI dependency that yields an async DB session per request.""" async with async_session() as session: yield session