37 lines
1.0 KiB
Python
37 lines
1.0 KiB
Python
import os
|
|
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
from dotenv import load_dotenv
|
|
|
|
load_dotenv()
|
|
|
|
DATABASE_URL = os.getenv("DATABASE_URL", "postgresql+asyncpg://postgres:postgres@localhost:5432/ingest_db")
|
|
DEBUG = os.getenv("DEBUG", "False").lower() in ("true", "1", "yes")
|
|
|
|
# Единый async движок для всего приложения
|
|
# echo=True включается только в режиме DEBUG
|
|
engine = create_async_engine(
|
|
DATABASE_URL,
|
|
echo=DEBUG,
|
|
pool_size=10,
|
|
max_overflow=20,
|
|
pool_pre_ping=True,
|
|
pool_recycle=3600,
|
|
)
|
|
|
|
AsyncSessionLocal = async_sessionmaker(
|
|
engine,
|
|
class_=AsyncSession,
|
|
expire_on_commit=False
|
|
)
|
|
|
|
Base = declarative_base()
|
|
|
|
|
|
async def get_db():
|
|
"""Dependency для получения async сессии БД"""
|
|
async with AsyncSessionLocal() as session:
|
|
try:
|
|
yield session
|
|
finally:
|
|
await session.close() |