diff --git a/app/db/session.py b/app/db/session.py new file mode 100644 index 0000000..fa7cbc4 --- /dev/null +++ b/app/db/session.py @@ -0,0 +1,29 @@ +from typing import AsyncGenerator +from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession +from app.core.config import settings + +engine = create_async_engine( + settings.DATABASE_URL, + echo=False, + future=True, + pool_size=10, + max_overflow=20, +) + +AsyncSessionLocal = async_sessionmaker( + bind=engine, + class_=AsyncSession, + expire_on_commit=False, +) + + +async def get_db() -> AsyncGenerator[AsyncSession, None]: + async with AsyncSessionLocal() as session: + try: + yield session + await session.commit() + except Exception: + await session.rollback() + raise + finally: + await session.close()