34 lines
671 B
Python
34 lines
671 B
Python
from sqlalchemy import create_engine, event
|
|
from sqlalchemy.orm import sessionmaker, DeclarativeBase
|
|
from app.config import get_settings
|
|
|
|
settings = get_settings()
|
|
|
|
_connect_args: dict = {}
|
|
if settings.DATABASE_URL.startswith("postgresql"):
|
|
_connect_args = {"sslmode": "prefer"}
|
|
|
|
engine = create_engine(
|
|
settings.DATABASE_URL,
|
|
connect_args=_connect_args,
|
|
pool_size=10,
|
|
max_overflow=20,
|
|
pool_pre_ping=True,
|
|
pool_recycle=3600,
|
|
)
|
|
|
|
|
|
class Base(DeclarativeBase):
|
|
pass
|
|
|
|
|
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
|
|
|
|
def get_db():
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|