
- Set up project structure and FastAPI app - Create database models and SQLAlchemy connection - Implement Alembic migration scripts - Add CRUD API endpoints for Todo items - Add health check endpoint - Set up validation, error handling, and middleware - Add comprehensive documentation in README.md
30 lines
700 B
Python
30 lines
700 B
Python
from typing import Generator
|
|
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import Session, sessionmaker
|
|
|
|
from app.core.config import settings
|
|
|
|
# Ensure DB directory exists
|
|
DB_DIR = settings.DB_DIR
|
|
DB_DIR.mkdir(parents=True, exist_ok=True)
|
|
|
|
SQLALCHEMY_DATABASE_URL = f"sqlite:///{DB_DIR}/db.sqlite"
|
|
|
|
engine = create_engine(
|
|
SQLALCHEMY_DATABASE_URL,
|
|
connect_args={"check_same_thread": False}
|
|
)
|
|
|
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
|
|
|
|
def get_db() -> Generator[Session, None, None]:
|
|
"""
|
|
Get a database session that automatically closes when done.
|
|
"""
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close() |