"""SQLite engine + session helpers. SQLModel (SQLAlchemy + Pydantic) is used rather than Jason's usual Prisma — Prisma is JS-first and its Python client is unofficial, whereas SQLModel is the FastAPI- native ORM. SQLite stays the DB, single-file under the data volume. """ from sqlmodel import SQLModel, Session, create_engine from .config import DATA_DIR, DB_PATH, MODELS_DIR # check_same_thread=False: the worker thread shares the engine with request handlers. engine = create_engine( f"sqlite:///{DB_PATH}", connect_args={"check_same_thread": False}, ) def init_db() -> None: DATA_DIR.mkdir(parents=True, exist_ok=True) MODELS_DIR.mkdir(parents=True, exist_ok=True) # Import models so they register on SQLModel.metadata before create_all. from . import models # noqa: F401 SQLModel.metadata.create_all(engine) def get_session(): with Session(engine) as session: yield session