CodeReview/backend/app/db/session.py

31 lines
778 B
Python
Raw Normal View History

from contextlib import asynccontextmanager
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
from sqlalchemy.orm import sessionmaker
from app.core.config import settings
engine = create_async_engine(settings.DATABASE_URL, echo=True, future=True)
AsyncSessionLocal = sessionmaker(
engine, class_=AsyncSession, expire_on_commit=False
)
async def get_db():
async with AsyncSessionLocal() as session:
try:
yield session
finally:
await session.close()
@asynccontextmanager
async def async_session_factory():
"""Async context manager for creating database sessions"""
async with AsyncSessionLocal() as session:
try:
yield session
finally:
await session.close()