32 lines
717 B
Python
32 lines
717 B
Python
from collections.abc import Generator
|
|
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import sessionmaker, DeclarativeBase, Session
|
|
|
|
from app.core.settings import get_settings
|
|
|
|
|
|
class Base(DeclarativeBase):
|
|
pass
|
|
|
|
|
|
settings = get_settings()
|
|
engine = create_engine(
|
|
settings.mysql_dsn,
|
|
echo=settings.sqlalchemy_echo,
|
|
pool_pre_ping=True,
|
|
pool_recycle=3600,
|
|
pool_size=settings.db_pool_size,
|
|
max_overflow=settings.db_max_overflow,
|
|
pool_timeout=settings.db_pool_timeout,
|
|
)
|
|
SessionLocal = sessionmaker(bind=engine, autoflush=False, autocommit=False, expire_on_commit=False)
|
|
|
|
|
|
def get_db() -> Generator[Session, None, None]:
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|