Skip to content

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.

const pm = eventStore.createProcessManager('cascade-delete')

Defines optional internal state. The state is persisted alongside checkpoints and restored on catch-up.

pm.withState({ pendingDeletions: [] })

Registers event handlers named on{EventName}. If state was defined, accepts a callback receiving the state.

// Without state
pm.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 state
pm.withEventHandlers((state) => ({
async onUserCreated({ payload }) {
state.userCount++
},
}))

Each handler receives (event, { refreshing }) where refreshing is true during a refresh operation.

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)
},
})

Disables the default exclusive per-stream locking.

Available only when .withState() has been called:

MethodReturn TypeDescription
.state()Promise<State>Returns current state after all pending events have been processed.
.refreshState()Promise<void>Full state refresh — replays all events from scratch.

Returns a promise that resolves when the process manager has finished its initial catch-up.

import { isInsideProcessManager, getProcessManagerInfo } from '@requence/event-sourcing'
FunctionReturn TypeDescription
isInsideProcessManager()booleantrue if called within a process manager event handler.
getProcessManagerInfo(){ name, event } | undefinedReturns the process manager name and triggering event.