Skip to content

handleCommandWithDecider

API reference for handleCommandWithDecider

@delta-base/toolkit


handleCommandWithDecider<State, TCommand, TEvent>(eventStore, streamId, command, decider, options?): Promise<CommandHandlerResult<State, TEvent>>

Creates a command handler with Decider pattern

Steps:

  1. Aggregate the stream to get current state
  2. Run business logic using Decider.decide function
  3. Append events to stream
  4. Return result with new state and events

State

TCommand extends Command

TEvent extends Readonly<{ data: EventData; metadata?: PlatformEventMetadata; type: string; }> = Readonly<{ data: EventData; metadata?: PlatformEventMetadata; type: string; }>

EventStore

The event store instance

string

The stream identifier

TCommand

The command to handle

Decider<State, TCommand, TEvent>

The decider implementation with decide, evolve, and initialState

DeciderCommandHandlerOptions

Optional command handler options

Promise<CommandHandlerResult<State, TEvent>>

Promise resolving to command handler result with new state and events

const result = await handleCommandWithDecider(
eventStore,
'user-123',
{ type: 'RegisterUser', data: { userId: '123' } },
{
decide: (state, cmd) => [{ type: 'UserRegistered', data: cmd.data }],
evolve: (state, event) => ({ ...state, registered: true }),
initialState: () => ({ registered: false })
}
);