32 lines
600 B
Python
32 lines
600 B
Python
from collections.abc import Generator
|
|
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import Session, sessionmaker
|
|
|
|
from app.common.config.settings import get_settings
|
|
|
|
|
|
settings = get_settings()
|
|
|
|
engine = create_engine(
|
|
settings.database_url,
|
|
future=True,
|
|
echo=settings.app_debug,
|
|
pool_pre_ping=True,
|
|
)
|
|
SessionLocal = sessionmaker(
|
|
bind=engine,
|
|
autocommit=False,
|
|
autoflush=True,
|
|
expire_on_commit=False,
|
|
class_=Session,
|
|
)
|
|
|
|
|
|
def get_db() -> Generator[Session, None, None]:
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|