Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.
/ command-bus Public archive

A simple command bus library for Node.js

Notifications You must be signed in to change notification settings

crawly/command-bus

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@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