This commit integrates the backend API for fetching and updating report data. It also includes a normalization function to handle data consistency between the API and local storage. Co-authored-by: anthonymuncher <anthonymuncher@gmail.com>
44 lines
1.1 KiB
Python
44 lines
1.1 KiB
Python
# app/database.py
|
|
import os
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import sessionmaker, declarative_base
|
|
import logging
|
|
|
|
# ----------------------
|
|
# Logging Configuration
|
|
# ----------------------
|
|
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
|
|
|
|
# ----------------------
|
|
# Database Configuration
|
|
# ----------------------
|
|
DB_PATH = os.environ.get("FIXMATE_DB", "app/db/fixmate.db")
|
|
os.makedirs(os.path.dirname(DB_PATH), exist_ok=True)
|
|
DATABASE_URL = f"sqlite:///{DB_PATH}"
|
|
|
|
engine = create_engine(
|
|
DATABASE_URL,
|
|
connect_args={"check_same_thread": False}, # Required for SQLite
|
|
echo=False # Set True for debugging SQL queries
|
|
)
|
|
|
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
Base = declarative_base()
|
|
|
|
# ----------------------
|
|
# Dependency
|
|
# ----------------------
|
|
def get_db():
|
|
"""
|
|
Yield a database session for FastAPI dependency injection.
|
|
Example usage in route:
|
|
db: Session = Depends(get_db)
|
|
"""
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
|
|
logging.info(f"Database initialized at {DB_PATH}")
|