Archived
24 lines
630 B
Python
24 lines
630 B
Python
from collections.abc import Generator
|
|
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import DeclarativeBase, Session, sessionmaker
|
|
|
|
from .config import settings
|
|
|
|
|
|
class Base(DeclarativeBase):
|
|
"""Базовый класс SQLAlchemy-моделей."""
|
|
|
|
|
|
engine = create_engine(settings.database_url, pool_pre_ping=True)
|
|
SessionLocal = sessionmaker(bind=engine, autoflush=False, autocommit=False)
|
|
|
|
|
|
def get_db() -> Generator[Session, None, None]:
|
|
"""Открывает сессию БД для одного HTTP-запроса."""
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|