from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker from sqlalchemy.orm import DeclarativeBase from app.core.config import settings from typing import AsyncGenerator engine = create_async_engine( settings.SQLALCHEMY_DATABASE_URI, echo=False, future=True, 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, ) class Base(DeclarativeBase): pass async def get_db() -> AsyncGenerator[AsyncSession, None]: async with AsyncSessionLocal() as session: try: yield session finally: await session.close()