Automated Action 1349113fe0 Create FastAPI REST API with SQLite
Features:
- Project structure setup
- Database configuration with SQLAlchemy
- Item model and CRUD operations
- API endpoints for items
- Alembic migrations
- Health check endpoint
- Comprehensive documentation

generated with BackendIM... (backend.im)
2025-05-14 00:49:31 +00:00

31 lines
751 B
Python

from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from pathlib import Path
# Create directory for database
DB_DIR = Path("/app") / "storage" / "db"
DB_DIR.mkdir(parents=True, exist_ok=True)
# SQLite database URL
SQLALCHEMY_DATABASE_URL = f"sqlite:///{DB_DIR}/db.sqlite"
# Create engine
engine = create_engine(
SQLALCHEMY_DATABASE_URL,
connect_args={"check_same_thread": False}
)
# Create session
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
# Create base model class
Base = declarative_base()
# Dependency for database sessions
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()