31 lines
654 B
Python
31 lines
654 B
Python
import os
|
|
|
|
from sqlalchemy.ext.asyncio import (
|
|
AsyncSession,
|
|
async_sessionmaker,
|
|
create_async_engine,
|
|
)
|
|
|
|
DATABASE_URL = os.getenv(
|
|
"DATABASE_URL",
|
|
"postgresql+asyncpg://messenger:strongpassword@localhost:5432/messenger",
|
|
)
|
|
|
|
engine = create_async_engine(
|
|
DATABASE_URL,
|
|
pool_size=10,
|
|
max_overflow=20,
|
|
pool_timeout=5,
|
|
pool_recycle=1800,
|
|
pool_pre_ping=True,
|
|
connect_args={"timeout": 5, "command_timeout": 10},
|
|
)
|
|
|
|
async_session = async_sessionmaker(
|
|
engine, class_=AsyncSession, expire_on_commit=False
|
|
)
|
|
|
|
|
|
async def get_db() -> AsyncSession:
|
|
async with async_session() as session:
|
|
yield session |