from sqlalchemy import create_engine from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker from .config import settings # Get the database URL from settings (defaults to SQLite) SQLALCHEMY_DATABASE_URL = settings.DATABASE_URL # Create the SQLAlchemy engine. # connect_args is only needed for SQLite to handle concurrent access. engine = create_engine( SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False} ) # Create a configured "Session" class # expire_on_commit=False prevents objects from being detached after commit SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) # Base class for the data models (models.py will inherit from this) Base = declarative_base() # Dependency: Get Database Session def get_db(): """ Dependency injection function to provide a database session to FastAPI endpoints (routers). """ db = SessionLocal() try: yield db finally: db.close() # Note: Remember to call Base.metadata.create_all(bind=engine) in main.py # or use migrations (Alembic) to create tables once models.py is ready.