Skip to content

Model & Field

Define tables with Model and Field. Model subclasses pydantic.BaseModel, so all Pydantic validation applies; Field adds TinyStore metadata (primary_key, unique, index, foreign_key, ...) on top.

Model

model

TinyStore model base.

Model is a thin layer over :class:pydantic.BaseModel. It uses a custom metaclass (subclassing Pydantic's own ModelMetaclass) so that class-level field access — e.g. Post.title — resolves to a :class:FieldProxy for both static type checkers and runtime query building. Field metadata (primary key, unique, index, foreign key, relationships) is introspected via Pydantic's own __pydantic_init_subclass__ hook and recorded on a TableSchema attached to the class as __tinystore_schema__.

Model

Bases: BaseModel

Base class for all TinyStore models.

Subclass it, declare Pydantic fields, and optionally a nested Meta class to override the table name::

class User(Model):
    id: int | None = Field(default=None, primary_key=True)
    name: str

    class Meta:
        table_name = "accounts"

Field

fields

TinyStore field declarations.

Field is a thin wrapper around :func:pydantic.Field that additionally carries TinyStore-specific metadata (primary key, foreign key, unique, index, on_delete). Because Pydantic v2's FieldInfo is @final, the metadata is attached through the supported json_schema_extra channel under a reserved __tinystore__ key and read back during model construction.

TinyStore never lets the metadata leak into the generated JSON Schema: it is popped before any schema is emitted (see :func:strip_meta).

get_meta

get_meta(info: FieldInfo) -> dict[str, Any] | None

Return TinyStore metadata attached to a Pydantic FieldInfo, if any.

strip_meta

strip_meta(info: FieldInfo) -> None

Remove the TinyStore metadata key so it never reaches the JSON Schema.

Field

Field(
    default: Any = ...,
    *,
    primary_key: bool = False,
    autoincrement: bool | None = None,
    unique: bool = False,
    index: bool = False,
    nullable: bool | None = None,
    foreign_key: str | None = None,
    on_delete: OnDelete = "RESTRICT",
    **kwargs: Any,
) -> Any

Declare a model field with TinyStore metadata.

See the project plan for the full semantics of each keyword.

RelationshipField

RelationshipField(
    *,
    foreign_key: str,
    on_delete: OnDelete = "RESTRICT",
    **kwargs: Any,
) -> Any

Marker field info for an annotated relationship field.