Automated Action d20c3af0bc Build a simple Todo app with FastAPI
- Setup project structure with FastAPI, SQLAlchemy, and SQLite
- Create Todo model and database migrations
- Implement CRUD API endpoints
- Add error handling and validation
- Update README with documentation and examples

generated with BackendIM... (backend.im)
2025-05-15 22:01:40 +00:00

29 lines
731 B
Python

from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from pathlib import Path
from app.core.config import settings
# Ensure the database directory exists
settings.DB_DIR.mkdir(parents=True, exist_ok=True)
# Create the SQLAlchemy engine
engine = create_engine(
settings.DATABASE_URL,
connect_args={"check_same_thread": False} # Only needed for SQLite
)
# Create session factory
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
# Create base class for models
Base = declarative_base()
# Dependency to get DB session
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()