loom.core.use_case¶
- loom.core.use_case.Exists(entity_type, *, from_param=None, from_command=None, against, on_missing=OnMissing.RETURN_FALSE)[source]¶
Factory returning marker for boolean existence checks.
- class loom.core.use_case.Compute[source]¶
Bases:
objectCompute DSL namespace.
Example
Compute.set(F(UpdateUser).slug).from_command(F(UpdateUser).name, via=slugify)
- loom.core.use_case.F(root)¶
Build a typed field-reference factory.
Example
F(UpdateUserCommand).birthdate
- class loom.core.use_case.FieldRef(root, path)[source]¶
Bases:
objectDeclarative reference to a field path on a command (or loaded alias).
- loom.core.use_case.Input()[source]¶
Factory returning the runtime marker for command payload parameters.
Returned value is intentionally typed as
Anyin overloads to avoidmypydefault-argument incompatibility in signatures like:cmd: Command = Input().- Return type:
- loom.core.use_case.Load(entity_type, *, from_param=None, from_command=None, against, profile='default', on_missing=OnMissing.RAISE)[source]¶
Factory returning marker for preloaded entity parameters by field.
- loom.core.use_case.LoadById(entity_type, *, by='id', profile='default', on_missing=OnMissing.RAISE)[source]¶
Factory returning marker for preloaded entity parameters by id.
Returned value is intentionally typed as
Anyin overloads to avoidmypydefault-argument incompatibility in signatures like:entity: User = LoadById(User, by="id").- Parameters:
entity_type (type[EntityT]) – Domain entity type the repository should load.
by (str) – Name of the primitive parameter used as the lookup key. Defaults to
"id".profile (str) – Loading profile forwarded to
repo.get_by_id. Defaults to"default".on_missing (OnMissing) – Missing-entity policy. Defaults to
OnMissing.RAISE.
- Return type:
- class loom.core.use_case.OnMissing(value)[source]¶
Bases:
StrEnumPolicy applied when a marker lookup does not resolve an entity.
- class loom.core.use_case.UseCase(main_repo=None)[source]¶
Bases:
ABC,Generic[ModelT,ResultT]Base class for all use cases.
Subclass and implement
executewith typed parameters. Parameter defaults declare the execution contract:Input()— command payload, built from the raw request.LoadById(EntityType, by="param")— entity prefetched by id.Load(EntityType, ...)— entity prefetched by arbitrary field.Exists(EntityType, ...)— boolean existence check by field.No default — primitive param bound directly from the caller.
Class attributes
computes,rules, andread_onlydeclare the pre-execution pipeline and execution policy. They are inspected once at startup byUseCaseCompilerand embedded in the immutableExecutionPlan.- Parameters:
main_repo (RepoFor[Any] | None)
- computes¶
Compute transformations applied in order before rule checks.
- Type:
ClassVar[Sequence[ComputeFn[Any]]]
- read_only¶
When
True, the executor skips opening aUnitOfWorktransaction. Set this on query-only use cases that never mutate state. GET routes inRestInterfacealways bypass the UoW regardless of this flag.- Type:
ClassVar[bool]
Example:
class UpdateUserUseCase(UseCase[UserResponse]): computes = [set_updated_at] rules = [email_must_be_valid] def __init__(self, user_repo: UserRepository) -> None: self._user_repo = user_repo async def execute( self, user_id: int, cmd: UpdateUserCommand = Input(), user: User = LoadById(User, by="user_id"), ) -> UserResponse: ...