Add SQLite database setup for FastAPI Todo app

- Create app/db/base.py with SQLAlchemy Base definition
- Create app/db/session.py with database engine and session setup using absolute path /app/storage/db/db.sqlite
- Create app/models/todo.py with Todo model including id, title, description, completed, created_at, updated_at fields
- Create app/schemas/todo.py with Pydantic schemas for TodoCreate, TodoUpdate, and TodoResponse
- Add necessary __init__.py files for proper package structure
This commit is contained in:
Automated Action 2025-06-20 02:25:09 +00:00
parent 63b439176c
commit 7e2ec9f8e3
11 changed files with 127 additions and 0 deletions

1
app/__init__.py Normal file
View File

@ -0,0 +1 @@
# This makes app a package

0
app/api/__init__.py Normal file
View File

1
app/db/__init__.py Normal file
View File

@ -0,0 +1 @@
# Database package

3
app/db/base.py Normal file
View File

@ -0,0 +1,3 @@
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()

29
app/db/session.py Normal file
View File

@ -0,0 +1,29 @@
from pathlib import Path
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from .base import Base
# Database setup with absolute path as specified in guidelines
DB_DIR = Path("/app") / "storage" / "db"
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)
# Create tables
Base.metadata.create_all(bind=engine)
# Dependency to get database session
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()

1
app/models/__init__.py Normal file
View File

@ -0,0 +1 @@
# Models package

15
app/models/todo.py Normal file
View File

@ -0,0 +1,15 @@
from datetime import datetime
from sqlalchemy import Column, Integer, String, Text, Boolean, DateTime
from app.db.base import Base
class Todo(Base):
__tablename__ = "todos"
id = Column(Integer, primary_key=True, index=True)
title = Column(String(255), nullable=False)
description = Column(Text, nullable=True)
completed = Column(Boolean, default=False, nullable=False)
created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)

1
app/schemas/__init__.py Normal file
View File

@ -0,0 +1 @@
# Schemas package

26
app/schemas/todo.py Normal file
View File

@ -0,0 +1,26 @@
from datetime import datetime
from typing import Optional
from pydantic import BaseModel
class TodoCreate(BaseModel):
title: str
description: Optional[str] = None
class TodoUpdate(BaseModel):
title: Optional[str] = None
description: Optional[str] = None
completed: Optional[bool] = None
class TodoResponse(BaseModel):
id: int
title: str
description: Optional[str] = None
completed: bool
created_at: datetime
updated_at: datetime
class Config:
from_attributes = True

43
main.py Normal file
View File

@ -0,0 +1,43 @@
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI(
title="Todo App API",
description="A simple Todo application API built with FastAPI",
version="1.0.0",
openapi_url="/openapi.json",
docs_url="/docs",
redoc_url="/redoc"
)
# Configure CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/")
async def root():
"""Base endpoint that returns app information"""
return {
"title": "Todo App API",
"description": "A simple Todo application API built with FastAPI",
"version": "1.0.0",
"documentation": "/docs",
"health_check": "/health"
}
@app.get("/health")
async def health_check():
"""Health check endpoint"""
return {"status": "healthy", "message": "Todo App API is running"}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)

7
requirements.txt Normal file
View File

@ -0,0 +1,7 @@
fastapi==0.104.1
uvicorn==0.24.0
sqlalchemy==2.0.23
alembic==1.13.1
pydantic==2.5.0
python-multipart==0.0.6
ruff==0.1.7