Skip to main content

Saga

TypeScript Support

A saga object has an associated TypeScript type:

  • Type Name - Saga
  • Package - @resolve-js/core

A saga's event handler receives an object that provides access to the saga-related API. This API includes the following objects:

Object NameDescription
storeProvides access to the saga's persistent store (similar to the Read Model store).
sideEffectsProvides access to the saga's side effect functions.

In addition to user-defined side effect functions, the SideEffects object contains the following default side effects:

Function NameDescription
executeCommandSends a command with the specified payload to an aggregate.
scheduleCommandSimilar to executeCommand, but delays command execution until a specified moment in time.

The sideEffects object's isEnabled field indicates whether or not side effects are enabled for the saga.

executeCommandā€‹

Sends a command with the specified payload to an aggregate.

Argumentsā€‹

Argument NameDescription
commandSpecifies a command object. Refer to the Write Side article for more information.

Exampleā€‹

await sideEffects.executeCommand({
aggregateName: 'User',
aggregateId: event.aggregateId,
type: 'requestConfirmUser',
payload: event.payload,
})

scheduleCommandā€‹

Similar to executeCommand but delays the command's execution until a specified moment in time.

Argumentsā€‹

Argument NameDescription
commandSpecifies a command object. Refer to the Write Side article for more information.

Exampleā€‹

await sideEffects.scheduleCommand(
event.timestamp + 1000 * 60 * 60 * 24 * 7,
{
aggregateName: 'User',
aggregateId: event.aggregateId,
type: 'forgetUser',
payload: {},
}
)