loom.rest.autocrud

Auto-CRUD route and UseCase generators.

Generates concrete UseCase subclasses and RestRoute objects for the five standard CRUD operations, given a domain model type.

The generated classes are cached per model — multiple interfaces sharing the same model reuse the same UseCase classes.

NOTE: This module intentionally omits from __future__ import annotations so that annotation expressions are evaluated eagerly at class-definition time. The factory functions capture model as a closure variable and embed it directly in the execute signature, making typing.get_type_hints work without requiring the model name to be present in the module’s global namespace.

Usage:

from loom.rest.autocrud import build_auto_routes

routes = build_auto_routes(User, include=("create", "get", "list"))

Functions

_derive_create_struct(model)

Derive a {ModelName}CreateInput command with only user-writable fields.

_derive_update_struct(model)

Derive a {ModelName}UpdateInput command with all writable fields optional.

_field_tuple(name, typ, sf)

Build a defstruct field specification from a struct field's default.

_get_or_create(model)

Return cached (or freshly built) UseCase classes for all five operations.

_id_annotation(model)

Return the declared id annotation for path-parameter typing.

_id_coerce(model)

Return int coercer when model declares id: int, else identity.

_make_create(model, create_input)

Generate a UseCase that creates a new entity via the main repository.

_make_delete(model, id_type, coerce)

Generate a UseCase that deletes an entity by id.

_make_get(model, id_type, coerce)

Generate a UseCase that fetches a single entity by id.

_make_list(model)

Generate a UseCase that lists entities via a QuerySpec.

_make_update(model, update_input, id_type, ...)

Generate a UseCase that applies a partial update to an entity.

_server_generated_names(model)

Return field names that are server-assigned and must not appear in write inputs.

build_auto_routes(model, include)

Return RestRoute objects for the requested CRUD operations.

loom.rest.autocrud.build_auto_routes(model, include)[source]

Return RestRoute objects for the requested CRUD operations.

Lazily imports RestRoute to avoid circular imports at module load time.

Parameters:
  • model (type[Any]) – Domain model type for which routes should be generated.

  • include (tuple[str, ...]) – Subset of operation names to expose. If empty, all five standard operations are included.

Returns:

Tuple of RestRoute instances, one per requested operation.

Return type:

tuple[Any, …]

Example:

routes = build_auto_routes(User, include=("create", "get", "list"))