Skip to content

base

Base Database Models for Tux Bot.

This module provides the foundational database models and utilities used throughout the Tux bot's database layer, including base classes with automatic timestamp management and serialization utilities.

Classes:

  • TimestampMixin

    Mixin providing automatic created_at and updated_at timestamp fields.

  • BaseModel

    Base SQLModel class with automatic timestamp management.

  • UUIDMixin

    Mixin for models that need UUID primary keys.

  • SoftDeleteMixin

    Mixin for soft delete functionality.

Classes

TimestampMixin

Mixin providing automatic created_at and updated_at timestamp fields.

This mixin adds database-managed timestamp fields that automatically set created_at on insert and update updated_at on every update.

Usage: class MyModel(SQLModel, TimestampMixin, table=True): id: int | None = Field(default=None, primary_key=True) name: str

BaseModel

Bases: SQLModel, TimestampMixin

Base SQLModel class with automatic timestamp management.

This class provides automatic created_at and updated_at timestamp fields that are managed by the database, along with serialization utilities for JSON responses.

Attributes:

  • created_at ((datetime, optional)) –

    Timestamp when the record was created (database-managed).

  • updated_at ((datetime, optional)) –

    Timestamp when the record was last updated (database-managed).

Methods:

  • serialize_datetimes

    Serialize datetime objects to ISO format strings.

  • model_post_init

    Ensure timestamp fields are always present in dict for compatibility.

  • to_dict

    Convert model instance to dictionary with relationship support.

Functions

serialize_datetimes
Python
serialize_datetimes(value: datetime | None) -> str | None

Serialize datetime objects to ISO format strings.

Parameters:

  • value (datetime) –

    The datetime value to serialize.

Returns:

  • (str, optional)

    ISO format string representation of the datetime, or None if value is None.

model_post_init
Python
model_post_init(__context: Any) -> None

Ensure timestamp fields are always present in dict for compatibility.

Even though timestamps are database-managed, some SQLAlchemy operations expect these fields to be accessible in the model's dict. This ensures compatibility without interfering with database defaults.

to_dict
Python
to_dict(
    include_relationships: bool = False, relationships: list[str] | None = None
) -> dict[str, Any]

Convert model instance to dictionary with relationship support.

Parameters:

  • include_relationships (bool, default: False ) –

    Whether to include relationship fields, by default False.

  • relationships (list[str] | None, default: None ) –

    Specific relationships to include (if None, includes all), by default None.

Returns:

  • dict[str, Any]

    Dictionary representation of the model.

UUIDMixin

Bases: SQLModel

Mixin for models that need UUID primary keys.

Provides: - id: UUID primary key with auto-generation - Proper indexing for performance

SoftDeleteMixin

Bases: SQLModel

Mixin for soft delete functionality.

Provides: - deleted_at: Timestamp when record was soft-deleted - is_deleted: Boolean flag for soft delete status

Methods:

Functions

serialize_deleted_at
Python
serialize_deleted_at(value: datetime | None) -> str | None

Serialize deleted_at field to ISO format string.

Returns:

  • str | None

    ISO format datetime string, or None if value is None.

soft_delete
Python
soft_delete() -> None

Mark record as soft-deleted.

restore
Python
restore() -> None

Restore a soft-deleted record.