Skip to content

OSSS.db.base

OSSS.db.base

Base

Bases: DeclarativeBase

metadata = MetaData(naming_convention=NAMING_CONVENTION) class-attribute instance-attribute

Shared declarative base for all models.

GUID

Bases: TypeDecorator

Platform-independent UUID type.

  • On PostgreSQL ⇒ uses UUID(as_uuid=True)
  • Elsewhere ⇒ stores as CHAR(36)

Returns/accepts Python uuid.UUID objects in both cases.

JSONB

Bases: TypeDecorator

Platform-aware JSON type.

  • On PostgreSQL ⇒ JSONB
  • Elsewhere ⇒ JSON

UUIDMixin

Mixin that adds
  • id: UUID primary key (GUID)
  • created_at: timezone-aware timestamp, defaults to NOW() on DB
  • updated_at: timezone-aware timestamp, defaults to NOW(), auto-updates

ts_cols(name='search_vector')

Return a pair (column, index_factory) you can use inside a model class.

Usage

class Document(UUIDMixin, Base): tablename = "documents"

# 1) Declare the column
search_vector: Mapped[Any] = ts_cols("search_vector")[0]

# 2) Add the index in __table_args__ using the factory
__table_args__ = (
    ts_cols("search_vector")[1](__tablename__),
)
Notes
  • On PostgreSQL, the column type is TSVECTOR and the index uses GIN.
  • On other DBs, the column is TEXT and the index is a regular btree.
  • You still need to populate/refresh the column yourself (e.g., triggers or app-level updates).