21 lines
532 B
Python
21 lines
532 B
Python
from contextlib import asynccontextmanager
|
|
from typing import AsyncIterator
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
from config.config import settings
|
|
|
|
engine = create_async_engine(settings.database_url, echo=True)
|
|
async_session = sessionmaker(
|
|
bind=engine,
|
|
expire_on_commit=False,
|
|
class_=AsyncSession,
|
|
)
|
|
|
|
|
|
@asynccontextmanager
|
|
async def get_session() -> AsyncIterator[AsyncSession]:
|
|
async with async_session() as session:
|
|
yield session
|