todoapi-la4vfq/app/database.py
Automated Action 8a1b373ff6 feat: Implement Todo API with FastAPI and SQLite
- Setup project structure and dependencies
- Create Todo model and SQLAlchemy database connection
- Set up Alembic for database migrations
- Implement CRUD operations and API endpoints
- Add health check endpoint
- Update README with project documentation

generated with BackendIM... (backend.im)
2025-05-12 10:11:52 +00:00

31 lines
734 B
Python

from pathlib import Path
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
# Create database directory
DB_DIR = Path("/app") / "storage" / "db"
DB_DIR.mkdir(parents=True, exist_ok=True)
# Database URL
SQLALCHEMY_DATABASE_URL = f"sqlite:///{DB_DIR}/db.sqlite"
# Create SQLAlchemy engine
engine = create_engine(
SQLALCHEMY_DATABASE_URL,
connect_args={"check_same_thread": False}
)
# Create SessionLocal class
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
# Create Base class
Base = declarative_base()
# Dependency
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()