<commit_before># Introduction

[![travis](https://img.shields.io/travis/adrai/node-evented-command.svg)](https://travis-ci.org/adrai/node-evented-command) [![npm](https://img.shields.io/npm/v/evented-command.svg)](https://npmjs.org/package/evented-command)

Project goal is to provide a simple command/event handling for evented systems like cqrs.

# Installation

  $ npm install evented-command

# Usage

	var evtCmd = require('evented-command')();

## Define the command structure [optional] 
The values describes the path to that property in the command message.

	evtCmd.defineCommand({
	  id: 'id',                       // optional
	  name: 'name',                   // optional
	  context: 'context.name',        // optional
	  aggregate: 'aggregate.name',    // optional
	  aggregateId: 'aggregate.id'     // optional
	});

## Define the event structure [optional]
The values describes the path to that property in the event message.

	evtCmd.defineEvent({
	  correlationId: 'correlationId', // optional
	  id: 'id',                       // optional
	  name: 'name',                   // optional
	  context: 'context.name',        // optional
	  aggregate: 'aggregate.name',    // optional
	  aggregateId: 'aggregate.id'     // optional
	});

## Define the id generator function [optional]
### you can define a synchronous function

	evtCmd.idGenerator(function() {
	  var id = require('node-uuid').v4().toString();
	  return id;
	});

### or you can define an asynchronous function

	evtCmd.idGenerator(function(callback) {
	  setTimeout(function() {
	    var id = require('node-uuid').v4().toString();
	    callback(null, id);
	  }, 50);
	});

## Wire up commands and events

	// pass in events from your bus
	bus.on('event', function(data){
	  evtCmd.emit('event', data);
	});

	// pass commands to bus
	evtCmd.on('command', function(data) {
	  bus.emit('command', data);
	});

## Send commands

	var cmd = new Command({
		// id: 'my onwn command id', // if you don't pass an id it will generate one, when emitting the command...
    name: 'changePerson',
    payload: {
      name: '<NAME>'
    },
    aggregate: {
      id: 8,
      name: 'jack'
    },
    context: {
    	name: 'hr'
    }
	});

	// emit it
	cmd.emit();



	// if you want to observe the command pass a callback
	cmd.emit(function(evt) {
		
	});


	// if you want to observe the command that generates any events pass an object like this:
	cmd.emit({

		event1: function(evt) {
			
		},

		event2: function(evt) {
			
		}
		
	});

### Send commands with the speakable api

	evtCmd.send('changePerson')
        .for('person') // aggregate name
        .instance('8') // aggregate id
        .in('hr')			 // context name
        .with({
        	// id: 'my onwn command id', // if you don't pass an id it will generate one, when emitting the command...
          revision: '12',
          payload: {
          	name: 'jack'
          }
        })
        .go(function(evt) {
          console.log('speakable', evt);
        });

	evtCmd.send('multi')
        .for('aggregate')
        .instance('instanceId')
        .in('context')
        .with({
          revision: '43',
          payload: 'data2'
        })
        .go({
          event1: function(evt) {
            console.log('speakable', evt);<commit_msg>fix tabs<commit_after># Introduction

[![travis](https://img.shields.io/travis/adrai/node-evented-command.svg)](https://travis-ci.org/adrai/node-evented-command) [![npm](https://img.shields.io/npm/v/evented-command.svg)](https://npmjs.org/package/evented-command)

Project goal is to provide a simple command/event handling for evented systems like cqrs.

# Installation

  $ npm install evented-command

# Usage

	var evtCmd = require('evented-command')();

## Define the command structure [optional] 
The values describes the path to that property in the command message.

	evtCmd.defineCommand({
	  id: 'id',                       // optional
	  name: 'name',                   // optional
	  context: 'context.name',        // optional
	  aggregate: 'aggregate.name',    // optional
	  aggregateId: 'aggregate.id'     // optional
	});

## Define the event structure [optional]
The values describes the path to that property in the event message.

	evtCmd.defineEvent({
	  correlationId: 'correlationId', // optional
	  id: 'id',                       // optional
	  name: 'name',                   // optional
	  context: 'context.name',        // optional
	  aggregate: 'aggregate.name',    // optional
	  aggregateId: 'aggregate.id'     // optional
	});

## Define the id generator function [optional]
### you can define a synchronous function

	evtCmd.idGenerator(function() {
	  var id = require('node-uuid').v4().toString();
	  return id;
	});

### or you can define an asynchronous function

	evtCmd.idGenerator(function(callback) {
	  setTimeout(function() {
	    var id = require('node-uuid').v4().toString();
	    callback(null, id);
	  }, 50);
	});

## Wire up commands and events

	// pass in events from your bus
	bus.on('event', function(data){
	  evtCmd.emit('event', data);
	});

	// pass commands to bus
	evtCmd.on('command', function(data) {
	  bus.emit('command', data);
	});

## Send commands

	var cmd = new Command({
		// id: 'my onwn command id', // if you don't pass an id it will generate one, when emitting the command...
    name: 'changePerson',
    payload: {
      name: '<NAME>'
    },
    aggregate: {
      id: 8,
      name: 'jack'
    },
    context: {
      name: 'hr'
    }
	});

	// emit it
	cmd.emit();



	// if you want to observe the command pass a callback
	cmd.emit(function(evt) {
		
	});


	// if you want to observe the command that generates any events pass an object like this:
	cmd.emit({

    event1: function(evt) {
    
    },
    
    event2: function(evt) {
    
    }
		
	});

### Send commands with the speakable api

	evtCmd.send('changePerson')
        .for('person') // aggregate name
        .instance('8') // aggregate id
        .in('hr')			 // context name
        .with({
          // id: 'my onwn command id', // if you don't pass an id it will generate one, when emitting the command...
          revision: '12',
          payload: {
          	name: 'jack'
          }
        })
        .go(function(evt) {
          console.log('speakable', evt);
        });

	evtCmd.send('multi')
        .for('aggregate')
        .instance('instanceId')
        .in('context')
        .with({
          revision: '43',
          payload: 'data2'
        })
        .go({
          event1: function(evt) {
            console.log('speakable', evt);
{'fraction_non_alphanumeric': 0.14422057264050903, 'fraction_numerical': 0.0042417815482502655, 'mean_word_length': 2.2977022977022976, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7496447', 'n_tokens_mistral': 2102, 'n_tokens_neox': 1995, 'n_words': 564}