connect database
This commit is contained in:
20
app/infrastructure/database/session.py
Normal file
20
app/infrastructure/database/session.py
Normal file
@@ -0,0 +1,20 @@
|
||||
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
|
||||
Reference in New Issue
Block a user