createProcessManager
Process managers are created via eventStore.createProcessManager(name). They coordinate workflows across multiple aggregate roots by listening for events and issuing commands in response.
Creation
Section titled “Creation”const pm = eventStore.createProcessManager('cascade-delete')Streams that handlers dispatch commands on are settled automatically when the handler completes — calling settled() inside a process manager throws. The optional second parameter declares defaults for those auto-settles, most notably a concurrency retry for unattended writes:
const pm = eventStore.createProcessManager('cascade-delete', { settled: { maxRetries: 3 },})See Retrying on concurrency conflicts for the retry semantics and the purity requirement it places on the dispatched commands.
Builder Methods
Section titled “Builder Methods”.withState(state)
Section titled “.withState(state)”Defines optional internal state. The state is persisted alongside checkpoints and restored on catch-up.
pm.withState({ pendingDeletions: [] }).withEventHandlers(handlers)
Section titled “.withEventHandlers(handlers)”Registers event handlers named on{EventName}. If state was defined, accepts a callback receiving the state.
// Without statepm.withEventHandlers({ async onOrganizationDeleted({ streamId }) { const users = await db.select().from(usersTable) .where(eq(usersTable.orgId, streamId)) for (const user of users) { userRoot.loadStream(user.id).delete() } },})
// With statepm.withEventHandlers((state) => ({ async onUserCreated({ payload }) { state.userCount++ },}))Each handler receives (event, { refreshing }) where refreshing is true during a refresh operation.
.withAfterEffects(handlers)
Section titled “.withAfterEffects(handlers)”Registers handlers that run after all on{EventName} handlers have completed. Uses the after{EventName} naming convention.
pm.withAfterEffects({ async afterOrderCompleted({ payload }) { await sendConfirmationEmail(payload.email) },}).concurrent()
Section titled “.concurrent()”Disables the default exclusive per-stream locking.
State Methods
Section titled “State Methods”Available only when .withState() has been called:
| Method | Return Type | Description |
|---|---|---|
.state() | Promise<State> | Returns current state after all pending events have been processed. |
.refreshState() | Promise<void> | Full state refresh — replays all events from scratch. |
Other Methods
Section titled “Other Methods”.isReady()
Section titled “.isReady()”Returns a promise that resolves when the process manager has finished its initial catch-up.
Context Helpers
Section titled “Context Helpers”import { isInsideProcessManager, getProcessManagerInfo } from '@requence/event-sourcing'| Function | Return Type | Description |
|---|---|---|
isInsideProcessManager() | boolean | true if called within a process manager event handler. |
getProcessManagerInfo() | { name, event } | undefined | Returns the process manager name and triggering event. |