30 lines
674 B
Python
30 lines
674 B
Python
"""Database configuration using synchronous SQLAlchemy."""
|
|
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import declarative_base, sessionmaker, Session
|
|
from fastapi import Depends
|
|
from typing import Generator
|
|
|
|
from config import settings
|
|
|
|
engine = create_engine(
|
|
settings.db_url,
|
|
pool_pre_ping=True,
|
|
pool_size=10,
|
|
max_overflow=20,
|
|
echo=False,
|
|
)
|
|
|
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
|
|
Base = declarative_base()
|
|
|
|
|
|
def get_db() -> Generator[Session, None, None]:
|
|
"""FastAPI dependency that yields a database session."""
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|