Simple Command Bus Implementation for NodeJS. It is majorly inspired by Tactician Command Bus for PHP https://tactician.thephpleague.com/
This project requires nodejs 8 or higher.
npm install simple-command-bus
yarn add simple-command-bus
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);
yarn run test
yarn run test:coverage
node examples/index.js