
- Set up project structure - Create FastAPI app with health endpoint - Implement SQLAlchemy with SQLite database - Set up Alembic for database migrations - Create CRUD operations for items - Add comprehensive documentation
33 lines
824 B
Python
33 lines
824 B
Python
from sqlalchemy import create_engine
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
from sqlalchemy.orm import sessionmaker
|
|
from pathlib import Path
|
|
|
|
# Create the database directory if it doesn't exist
|
|
DB_DIR = Path("/app") / "storage" / "db"
|
|
DB_DIR.mkdir(parents=True, exist_ok=True)
|
|
|
|
# Database URL
|
|
SQLALCHEMY_DATABASE_URL = f"sqlite:///{DB_DIR}/db.sqlite"
|
|
|
|
# Create SQLAlchemy engine
|
|
engine = create_engine(
|
|
SQLALCHEMY_DATABASE_URL,
|
|
connect_args={"check_same_thread": False}, # SQLite specific
|
|
)
|
|
|
|
# Create a session factory
|
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
|
|
# Create a Base class for model declaration
|
|
Base = declarative_base()
|
|
|
|
|
|
# Dependency for database session
|
|
def get_db():
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|