📘
@node-test/bus
  • @node-ts/bus
  • Getting started
    • Installation
    • Handling messages
    • Shutting down cleanly
  • Reference
    • Bus
    • BusConfiguration
    • BusInstance
  • Getting help
  • Guide
    • Messages
      • Events
      • Commands
      • System messages
    • Message attributes
      • Correlation id
      • Attributes
      • Sticky attributes
    • Workflows
      • Creating a workflow
      • Starting
      • Handling
      • State
      • Completing
      • Example
    • Transports
      • RabbitMQ
      • Amazon SQS
      • Redis
      • Custom transports
    • Persistence
      • Postgres
      • MongoDB
      • Creating a persistence
    • Serializers
      • Class serializer
    • Loggers
      • Custom loggers
    • Middleware
    • Lifecycle hooks
    • Retry Strategies
    • Dependency injection
    • Long running processes
Powered by GitBook
On this page

Was this helpful?

  1. Guide
  2. Workflows

Starting

Instances of a workflow are started by one or more messages. When one of these types of messages are received a new workflow state is created and execution of the workflow begins.

Declare a handler for the message that starts your workflow, and then map it using the WorkflowMapper.

import { Workflow } from '@node-ts/bus-core'

export class FulfilmentWorkflow extends Workflow<FulfilmentWorkflowState> {
  configureWorkflow (
    mapper: WorkflowMapper<FulfilmentWorkflowState, FulfilmentWorkflow>
  ): void {
    mapper
      .withState(FulfilmentWorkflowState)
      // Start a new workflow when an `ItemPurchased` event is received
      .startedBy(ItemPurchased, 'shipItem')
  }
  
  // Handles an `ItemPurchased` event
  async shipItem (event: ItemPurchased) {
    // ...
  }
}

In this case, when a ItemPurchased event is received, it will start a new workflow and dispatch the message to the shipItem handler.

PreviousCreating a workflowNextHandling

Last updated 3 years ago

Was this helpful?