Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

simple-command-bus

1.0.7 • Public • Published

Simple Command Bus

npm version Build Status codecov

Simple Command Bus Implementation for NodeJS. It is majorly inspired by Tactician Command Bus for PHP https://tactician.thephpleague.com/

Requirements

This project requires nodejs 8 or higher.

Install

NPM

npm install simple-command-bus

Yarn

yarn add simple-command-bus

Basic Usage

const {
    Command,
    CommandBus,
    CommandHandlerMiddleware,
    ClassNameExtractor,
    InMemoryLocator,
    HandleInflector,
    LoggerMiddleware 
} = require('simple-command-bus');

// CreateAccount Command
class CreateAccountCommand extends Command {
    constructor(firstName, lastName) {
        super();
        this.firstName = firstName;
        this.lastName = lastName;
    }
}

// CreateAccount Handler
class CreateAccountHandler {
    handle(command) {
        // Logic to create an account.
    }
};

// Handler middleware
var commandHandlerMiddleware = new CommandHandlerMiddleware(
    new ClassNameExtractor(),
    new InMemoryLocator({ CreateAccountHandler: new CreateAccountHandler() }),
    new HandleInflector()
);

// Command bus instance
var commandBus = new CommandBus([
    new LoggerMiddleware(console),
    commandHandlerMiddleware
]);

const createAccountCommand = new CreateAccountCommand('John', 'Doe');
var result = commandBus.handle(createAccountCommand);
console.log('Result:', result);

Run tests

yarn run test

Run tests with coverage

yarn run test:coverage

Check example

  • node examples/index.js

Install

npm i simple-command-bus

DownloadsWeekly Downloads

762

Version

1.0.7

License

MIT

Unpacked Size

250 kB

Total Files

71

Last publish

Collaborators

  • avatar