from typing import AsyncGenerator from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine from sqlalchemy.orm import DeclarativeBase from config import settings engine = create_async_engine( settings.database_url, echo=False, # logs all SQL — set False in production pool_size=10, max_overflow=20, pool_pre_ping=True, # reconnect if Neon pooler closed an idle connection pool_recycle=300, ) AsyncSessionLocal = async_sessionmaker( bind=engine, class_=AsyncSession, expire_on_commit=False, ) class Base(DeclarativeBase): pass async def get_db() -> AsyncGenerator[AsyncSession, None]: """FastAPI dependency — yields a session and closes it after the request.""" async with AsyncSessionLocal() as session: try: yield session await session.commit() except Exception: await session.rollback() raise finally: await session.close()