from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine from sqlalchemy.orm import sessionmaker, declarative_base from app.config import settings async_engine = create_async_engine( settings.DATABASE_URL, echo=settings.DB_ECHO, pool_size=20, max_overflow=10, pool_pre_ping=True, ) AsyncSessionLocal = sessionmaker( async_engine, class_=AsyncSession, expire_on_commit=False, autocommit=False, autoflush=False, ) Base = declarative_base() async def get_db() -> AsyncSession: async with AsyncSessionLocal() as session: try: yield session await session.commit() except Exception: await session.rollback() raise finally: await session.close()