Automated Action 413eb37e87 Add simple todo app using FastAPI and SQLite
- Implement Todo database model with SQLAlchemy
- Set up Alembic for database migrations
- Create CRUD operations for Todo items
- Implement RESTful API endpoints
- Add health check endpoint
- Include comprehensive README with usage instructions
2025-05-17 21:39:34 +00:00

35 lines
1011 B
Python

from pathlib import Path
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, declarative_base
from sqlalchemy.ext.declarative import DeclarativeMeta
# Create database directory
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 SQLAlchemy engine with SQLite-specific connection arguments
engine = create_engine(
SQLALCHEMY_DATABASE_URL,
connect_args={"check_same_thread": False}
)
# Create a SessionLocal class
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
# Create a Base class for declarative models
Base: DeclarativeMeta = declarative_base()
def init_db() -> None:
"""Initialize database by creating all tables."""
Base.metadata.create_all(bind=engine)
def get_db():
"""Dependency function for getting database session."""
db = SessionLocal()
try:
yield db
finally:
db.close()