27 lines
618 B
Python
27 lines
618 B
Python
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker
|
|
from app.core.config import settings
|
|
|
|
engine = create_async_engine(
|
|
settings.database_url,
|
|
echo=settings.DEBUG,
|
|
future=True
|
|
)
|
|
|
|
AsyncSessionLocal = async_sessionmaker(
|
|
engine,
|
|
class_=AsyncSession,
|
|
expire_on_commit=False,
|
|
autocommit=False,
|
|
autoflush=False
|
|
)
|
|
|
|
|
|
async def get_db() -> AsyncSession:
|
|
"""Dependency для получения сессии БД"""
|
|
async with AsyncSessionLocal() as session:
|
|
try:
|
|
yield session
|
|
finally:
|
|
await session.close()
|
|
|