todoapp-tbb9a5/app/db/session.py
2025-05-22 19:30:08 +00:00

22 lines
529 B
Python

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from app.core.config import settings
# Ensure the DB directory exists
settings.DB_DIR.mkdir(parents=True, exist_ok=True)
engine = create_engine(
settings.SQLALCHEMY_DATABASE_URL,
connect_args={"check_same_thread": False}
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
def get_db():
"""Dependency for getting DB session."""
db = SessionLocal()
try:
yield db
finally:
db.close()