Skip to content
This repository was archived by the owner on Jul 23, 2024. It is now read-only.

Latest commit

 

History

History
68 lines (47 loc) · 1.75 KB

README.md

File metadata and controls

68 lines (47 loc) · 1.75 KB

@crawly/command-bus

This library provides a simple command bus library for Node.js

Installation

Run the following to install this library:

npm install @crawly/command-bus --save

Usage

You build a simple message object like this:

import { CommandInterface } from '@crawly/command-bus';

export class CreateUserCommand implements CommandInterface
{
    constructor(
    public readonly userName: string,
    public readonly userEmail: string,
  ) {}

  //...
}

And a Handler class that expects it:

import { SimpleCommandHandler, CommandInterface } from '@crawly/command-bus';

export class CreateUserCommandHandler extends SimpleCommandHandler
{
  public handleCreateUserCommand(createUserCommand: CommandInterface): void {
    // Logic to persist user
    // createUserCommand.getUserName();
    // createUserCommand.getUserEmail();
  }
}

Use CommandBus to dispatch commands.

const commandBus = new SimpleCommandBus();

// Register command handlers
commandBus.subscribe(new CreateUserCommandHandler());

// Dispatch command
commandBus.dispatch(new CreateUserCommand('John Doe', '[email protected]'));

Command Events

This library provides EventDispatchingCommandBus, a command bus decorator that dispatches events.

EventDispatchingCommandBus dispatches events signalling whether a command was executed successfully or if it failed.

Event name Description
crawly.command_bus.command_success Emitted when a command is executed without errors
crawly.command_bus.command_failure Emitted when an error occurred during command execution