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
Repository capability protocol each CRUD operation needs. |
Functions
|
Derive a |
|
Derive a |
|
Build a |
|
Return cached (or freshly built) UseCase classes for all five operations. |
|
Return the declared |
|
Return |
|
Generate a UseCase that creates a new entity via the main repository. |
|
Generate a UseCase that deletes an entity by id. |
|
Generate a UseCase that fetches a single entity by id. |
|
Generate a UseCase that lists entities via a QuerySpec. |
|
Generate a UseCase that applies a partial update to an entity. |
|
Return field names that are server-assigned and must not appear in write inputs. |
|
Return the CRUD ops to mount for model when |
|
Return |
|
Return the CRUD operation a generated use case implements. |
|
Return the class an explicit |
|
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
RestRouteobjects for the requested CRUD operations.Lazily imports
RestRouteto 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
-ablecapability protocols via its MRO. An explicit list is always honoured as-is.
- Returns:
Tuple of
RestRouteinstances, one per requested operation.- Return type:
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_forregistration names for model.
- 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_CAPABILITYis incapabilities_of(repository_type).- Return type:
frozenset[CrudOp]