from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker from sqlalchemy.orm import declarative_base from app.config import settings _url = settings.database_url # SQLite for local dev — no pool settings if _url.startswith("sqlite"): engine = create_async_engine(_url, echo=False, connect_args={"check_same_thread": False}) else: engine = create_async_engine(_url, echo=False, pool_pre_ping=True, pool_size=10, max_overflow=20) AsyncSessionLocal = async_sessionmaker( bind=engine, class_=AsyncSession, expire_on_commit=False, autocommit=False, autoflush=False, ) Base = declarative_base() async def get_db(): async with AsyncSessionLocal() as session: try: yield session await session.commit() except Exception: await session.rollback() raise finally: await session.close()