25 lines
625 B
Python
25 lines
625 B
Python
from typing import AsyncGenerator
|
|
|
|
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession
|
|
|
|
from .config import (
|
|
PG_HOST,
|
|
PG_LOGIN,
|
|
PG_PORT,
|
|
PG_DATABASE,
|
|
PG_PASSWORD,
|
|
)
|
|
|
|
database_url = (
|
|
f"postgresql+asyncpg://"
|
|
f"{PG_LOGIN}:{PG_PASSWORD}@"
|
|
f"{PG_HOST}:{PG_PORT}/{PG_DATABASE}"
|
|
)
|
|
engine = create_async_engine(database_url)
|
|
session_factory = async_sessionmaker(engine, expire_on_commit=False, autoflush=False, autocommit=False)
|
|
|
|
|
|
async def get_session() -> AsyncGenerator[AsyncSession, None]:
|
|
async with session_factory() as session:
|
|
yield session
|