import os from typing import AsyncGenerator from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession DATABASE_URL = os.getenv("DATABASE_URL", "").strip() if not DATABASE_URL: raise RuntimeError("DATABASE_URL is required (e.g. postgresql+asyncpg://...)") engine = create_async_engine(DATABASE_URL, pool_pre_ping=True) SessionLocal = async_sessionmaker( bind=engine, class_=AsyncSession, autoflush=False, expire_on_commit=False, ) async def get_db() -> AsyncGenerator[AsyncSession, None]: async with SessionLocal() as session: yield session