Skip to content

Instantly share code, notes, and snippets.

@townie
Created July 15, 2025 05:25
Show Gist options
  • Save townie/2d045d2b2df4fee07008552d0c4f1f84 to your computer and use it in GitHub Desktop.
Save townie/2d045d2b2df4fee07008552d0c4f1f84 to your computer and use it in GitHub Desktop.
"""
Base SQLModel configuration for the application.
"""
from sqlmodel import SQLModel
# This is the base class that all models will inherit from
# SQLModel automatically creates the metadata that Alembic needs
Base = SQLModel
"""
Database initialization and configuration.
"""
from typing import Generator
from sqlmodel import create_engine, SQLModel, Session
from ca.config import settings
# Create the database engine
engine = create_engine(str(settings.SQLALCHEMY_DATABASE_URI), echo=settings.DB_ECHO)
def get_db() -> Generator[Session, None, None]:
"""
Database session generator for dependency injection.
Usage:
# As context manager
with next(get_db()) as session:
# work with session
# With FastAPI dependency injection
def endpoint(session: Session = Depends(get_db)):
# session automatically provided
"""
with Session(engine) as session:
yield session
def get_engine():
"""Get the database engine."""
return engine
from datetime import datetime, timezone
from typing import Optional
from uuid import UUID, uuid4
from sqlmodel import SQLModel, Field, Relationship, Column
from sqlalchemy import LargeBinary
class MyModelV1(SQLModel, table=True):
"""Book cover images with database storage and file system paths."""
__tablename__ = "my_model_v1"
__table_args__ = {"extend_existing": True}
id: UUID = Field(default_factory=uuid4, primary_key=True, description="UUID primary key")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment