ApplyMutation
ApplyMutation<
TEntity>(options):MethodDecorator
Defined in: packages/ddd-core/domain/decorators/apply-mutation.decorator.ts:130
Completes a successful domain mutation by advancing onUpdate() once and
recording events from the resulting state. Preserves the method’s return value,
including an entity returned by an asynchronous method.
Domain methods validate inputs, call this.applyPatch(...) for mutable fields,
and return this. They must not apply events or advance lifecycle state directly.
Lifecycle operations are checked before invoking the method.
Patch validation and normalization finish before that patch writes any fields. Failures after a successful patch (including later method code, lifecycle hooks, or event construction/application) do not roll back state: discard or reload the aggregate. Rejected methods do not run the completion lifecycle.
Type Parameters
Section titled “Type Parameters”TEntity
Section titled “TEntity”TEntity = unknown
The aggregate the decorated method belongs to.
Parameters
Section titled “Parameters”options
Section titled “options”ApplyMutationOptions<TEntity>
The domain event(s) factory to record for this mutation.
Returns
Section titled “Returns”MethodDecorator
A method decorator that performs the full mutation lifecycle.
Example
Section titled “Example”export class User extends RootEntity<UserSnapshot> { @Mutable<string>({ normalize: (value) => User.normalizeUsername(value) }) private _username: string;
@ApplyMutation<User>({ event: (user) => new UserRenamedEvent(user) }) rename(name: string): this { this.applyPatch({ username: name }); return this; }}