loom.core.model¶
- class loom.core.model.BaseModel[source]¶
Bases:
StructBase for all loom domain models.
Subclasses must declare
__tablename__and provide typed attributes. Column metadata is optional viaColumnField(...).
- class loom.core.model.TimestampedModel(*, created_at=UNSET, updated_at=UNSET)[source]¶
Bases:
BaseModelOpt-in base for domain models that need audit timestamps.
Adds
created_atandupdated_atas nullable server-default columns. Both fields are omitted from serialisation output whenNone(omit_defaults=Trueis inherited fromBaseModel).Design note:
trace_idis intentionally absent. Trace identifiers are an observability concern belonging to the transport layer, not to the domain model. Correlating a record with the request that created it is the responsibility of structured logs and SQL query comments — not of a model field.- created_at¶
UTC timestamp set by the database at INSERT time.
Noneuntil the first flush/commit.- Type:
datetime.datetime | None
- updated_at¶
UTC timestamp updated by the database on every UPDATE.
Noneuntil the first flush/commit.- Type:
datetime.datetime | None
Example:
class Product(TimestampedModel): __tablename__ = "products" id: int = ColumnField(primary_key=True, autoincrement=True) name: str
- class loom.core.model.ColumnFieldInfo(name, python_type, column_type, field)[source]¶
Bases:
objectResolved metadata for a single column field.
- Parameters:
name (str)
python_type (type)
column_type (ColumnType)
field (Field)
- loom.core.model.ColumnField(column_type=None, *, primary_key=False, unique=False, index=False, nullable=False, autoincrement=False, server_default=None, server_onupdate=None, foreign_key=None, on_delete=None, default=msgspec.UNSET, length=None, db_type=None)[source]¶
Declare column metadata without using explicit
Annotatedsyntax.- Parameters:
column_type (ColumnType | None)
primary_key (bool)
unique (bool)
index (bool)
nullable (bool)
autoincrement (bool)
server_default (ServerDefault | None)
server_onupdate (ServerOnUpdate | str | None)
foreign_key (str | None)
on_delete (OnDelete | None)
default (Any)
length (int | None)
db_type (ColumnType | None)
- Return type:
- class loom.core.model.ColumnType(type_name, args=(), kwargs=<factory>)[source]¶
Bases:
objectBackend column type descriptor stored inside
Annotated[...].
- class loom.core.model.Field(primary_key=False, unique=False, index=False, nullable=False, autoincrement=False, server_default=None, server_onupdate=None, foreign_key=None, on_delete=None, default=UNSET, length=None)[source]¶
Bases:
objectColumn constraint metadata stored inside
Annotated[...].
- loom.core.model.ProjectionField(*, loader, profiles=('default',), depends_on=(), default=PROJECTION_DEFAULT_MISSING)[source]¶
Declare a projection field without triggering assignment type errors.
- Parameters:
loader (Any) – Loader descriptor or instance responsible for computing the value.
profiles (tuple[str, ...]) – Profile names in which this projection is active.
depends_on (tuple[str, ...]) – Names of other projections or relation events this depends on.
default (Any) – Fallback value when the loader returns no result for an entity.
- Returns:
A
Projectioninstance cast toAnyfor clean class-body assignment.- Return type:
Example:
count_reviews: int = ProjectionField( loader=CountLoader(model=ProductReview), profiles=("with_details",), default=0, )
- class loom.core.model.LoomStructMeta(name, bases, namespace, **kwargs)[source]¶
Bases:
StructMetaMetaclass that intercepts
RelationandProjectionassignments beforeStructMetaprocesses the class body.Each intercepted attribute is replaced with
UNSETas its default, and the type annotation is widened toT | UnsetTypeso thatomit_defaults=Truestrips unloaded fields from serialisation output.
- class loom.core.model.Projection(loader, profiles=('default', ), depends_on=(), default=<object object>)[source]¶
Bases:
objectDerived-field metadata assigned as a class attribute on a
BaseModel.
- class loom.core.model.Relation(foreign_key, cardinality, secondary=None, back_populates=None, on_delete=None, on_update=None, profiles=('default',), depends_on=(), default=None)[source]¶
Bases:
objectRelationship metadata assigned as a class attribute on a
BaseModel.
- loom.core.model.RelationField(*, foreign_key, cardinality, secondary=None, back_populates=None, on_delete=None, on_update=None, profiles=('default',), depends_on=(), default=None)[source]¶
Declare a relation field with normal typing (without assignment type errors).
- loom.core.model.get_column_fields(cls)[source]¶
Extract column fields from a model class.
- Parameters:
cls (type)
- Return type:
- loom.core.model.get_projections(cls)[source]¶
Return projections registered by
LoomStructMeta.- Parameters:
cls (type)
- Return type: