todoapp-7thmky/app/db/session.py
Automated Action e35edd0f6a Add database models and SQLAlchemy setup for todo app
- Created app directory structure with db, models, and schemas modules
- Added SQLAlchemy Base class in app/db/base.py to prevent circular imports
- Implemented database session setup with SQLite configuration
- Created Todo model with id, title, description, completed, created_at, updated_at fields
- Added Pydantic schemas for TodoCreate, TodoUpdate, and TodoResponse
- Added requirements.txt with FastAPI, SQLAlchemy, and other dependencies
2025-06-19 00:41:31 +00:00

30 lines
709 B
Python

from pathlib import Path
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from .base import Base
# Database setup with specified path format
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 all tables
def create_tables():
Base.metadata.create_all(bind=engine)
# Dependency to get database session
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()