from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from app.core.config import settings # Create SQLAlchemy engine engine = create_engine( settings.SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False} # Needed for SQLite ) # Create a SessionLocal class SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) def get_db(): """ Dependency function that creates a new SQLAlchemy SessionLocal that will be used in a single request, and then closed. """ db = SessionLocal() try: yield db finally: db.close()