from typing import AsyncGenerator from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession from app.core.config import settings # Create async database engine engine = create_async_engine( settings.DATABASE_URL, echo=False, future=True, pool_size=10, max_overflow=20, ) # Async session factory AsyncSessionLocal = async_sessionmaker( bind=engine, class_=AsyncSession, expire_on_commit=False, ) async def get_db() -> AsyncGenerator[AsyncSession, None]: """Dependency for obtaining an asynchronous database session""" async with AsyncSessionLocal() as session: try: yield session await session.commit() except Exception: await session.rollback() raise finally: await session.close()