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"))

Module Attributes

CRUD_OP_CAPABILITY

Repository capability protocol each CRUD operation needs.

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.

_supported_ops(model)

Return the CRUD ops to mount for model when include is empty.

build_auto_routes(model, include)

Return RestRoute objects for the requested CRUD operations.

crud_op_of(use_case_type)

Return the CRUD operation a generated use case implements.

registered_repository_type(model)

Return the class an explicit repository_for registration names for model.

supported_crud_ops(repository_type)

Return the CRUD operations repository_type declares a capability for.

loom.rest.autocrud.CRUD_OP_CAPABILITY: dict[CrudOp, type] = {CrudOp.CREATE: <class 'loom.core.repository.abc.repo_for.Creatable'>, CrudOp.DELETE: <class 'loom.core.repository.abc.repo_for.Deletable'>, CrudOp.GET: <class 'loom.core.repository.abc.repo_for.Readable'>, CrudOp.LIST: <class 'loom.core.repository.abc.repo_for.Listable'>, CrudOp.UPDATE: <class 'loom.core.repository.abc.repo_for.Updatable'>}

Repository capability protocol each CRUD operation needs.

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, the supported operations are derived automatically from the repository’s -able capability protocols via its MRO. An explicit list is always honoured as-is.

Returns:

Tuple of RestRoute instances, one per requested operation.

Return type:

tuple[Any, …]

Example:

routes = build_auto_routes(User, include=("create", "get", "list"))
loom.rest.autocrud.crud_op_of(use_case_type)[source]

Return the CRUD operation a generated use case implements.

Derived from the use-case key <entity>:<operation> that _get_or_create() sets on every generated class.

Parameters:

use_case_type (type[Any]) – A use case class produced by build_auto_routes().

Returns:

The operation the class implements.

Raises:

ValueError – When the class was not generated by this module.

Return type:

CrudOp

loom.rest.autocrud.registered_repository_type(model)[source]

Return the class an explicit repository_for registration names for model.

Parameters:

model (type[Any])

Return type:

type[Any] | None

loom.rest.autocrud.supported_crud_ops(repository_type)[source]

Return the CRUD operations repository_type declares a capability for.

Parameters:

repository_type (type[Any]) – The repository class that will serve the model.

Returns:

The operations whose protocol in CRUD_OP_CAPABILITY is in capabilities_of(repository_type).

Return type:

frozenset[CrudOp]