35 lines
711 B
Python
35 lines
711 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,
|
|
pool_size=20,
|
|
max_overflow=10,
|
|
pool_timeout=1000
|
|
)
|
|
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
|