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