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 ¶
model_post_init ¶
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 ¶
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:
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:
-
serialize_deleted_at–Serialize deleted_at field to ISO format string.
-
soft_delete–Mark record as soft-deleted.
-
restore–Restore a soft-deleted record.