cqrs-domain

Node-cqrs-domain is a node.js module based on nodeEventStore. It can be very useful as domain component if you work with (d)ddd, cqrs, eventdenormalizer, host, etc.

npm install cqrs-domain
99 downloads in the last week
126 downloads in the last month

Introduction

Build Status

Node-cqrs-domain is a node.js module based on nodeEventStore. It can be very useful as domain component if you work with (d)ddd, cqrs, eventdenormalizer, host, etc.

Installation

$ npm install cqrs-domain

Usage

Initialization

var domain = require('cqrs-domain').domain;

domain.on('event', function(evt) {
    // send to bus
});
domain.initialize({
    commandHandlersPath: __dirname + '/commandHandlers',
    aggregatesPath: __dirname + '/aggregates',
    sagaHandlersPath: __dirname + '/sagaHandlers',
    sagasPath: __dirname + '/sagas',
    snapshotThreshold: 10,
    forcedQueuing: false,
    disableQueuing: false,
    handleUndispatchedEvents: true//,
    // retryOnConcurrencyTimeout: 800,
    // commandLock: { type: 'inMemory', collectionName: 'commandlock' }
}, function(err) {

});

domain.handle({ id: 'msgId', command: 'changeDummy', payload: { id: '23445' } }, function(err) {

});

Define aggregates...

var base = require('cqrs-domain').aggregateBase;

module.exports = base.extend({

    // snapshotThreshold: 20, 
    // or
    // snapshotThreshold: function() { return 12 + 10; },
    // 
    // used to version the snap shots
    // version: 3,
    // 
    // laodSnapshot: function(data, version) {
    //     if (version === 1) {
    //         this.set(snap.data);
    //     } else {
    //         this.set(snap.data);
    //     }
    // },

    // commands

    changeDummy: function(data, callback) {
        this.apply(this.toEvent('dummyChanged', data));

        this.checkBusinessRules(callback);
    },

    destroyDummy: function(data, callback) {
        this.apply(this.toEvent('dummyDestroyed', data));

        this.checkBusinessRules(callback);
    },

    cancelDummy: function(data, callback) {
        this.apply(this.toEvent('dummyCancelled', data));

        this.checkBusinessRules(callback);
    },

    fooIt: function(data, callback) {
        this.apply(this.toEvent('fooIted', data));

        this.checkBusinessRules(callback);
    },

    versionedCmd: function(data, callback) {
        this.apply(this.toEvent('versionedEvt', data), callback);
    },

    versionedCmd_1: function(data, callback) {
        this.apply(this.toEvent('versionedEvt', data, 1), callback);
    },


    // events

    dummyChanged: function(data) {
        this.set(data);
    },

    dummyCancelled: function(data) {
        this.set('cancelled', true);
    },

    dummyDestroyed: function(data) {
        this.set('destroyed', true);
    },

    fooIted: function(data) {
        this.set('foo', true);
    },

    versionedEvt: function(data) {
        this.set(data);
    },

    versionedEvt_1: function(data) {
        this.set(data);
    }

});

See tests for detailed information...

Release Notes

v0.7.9

  • send commandRejected event with better reason

v0.7.8

  • added optional callback on commandhandler defaultHandle

v0.7.7

  • optimization for npm module naming

v0.7.6

  • updated eventstore

v0.7.5

  • introduce versioned messages and snapshots

v0.7.4

  • fixed naming of handleUndispatchedEvents option

v0.7.3

  • updated eventstore

v0.7.2

  • update dependencies

v0.7.1

  • load sagas always from db

v0.7.0

  • introduced commandLock for distributed domain (handling same aggregate instance on multiple machines)

v0.6.1

  • buffer commands by aggregate id

v0.6.0

  • don't publish in eventstore but publish in domain
  • removed flags: publishingInterval, forkEventDispatching
  • added handleUpdispatchedEvents flag

v0.5.3

  • fix for async business rules (issue #13)

v0.5.2

  • fix commandDispatcher if no commandqueue is used

v0.5.0

v0.4.4

  • added disableQueuing flag

v0.4.3

  • strip .js file extensions to enable loading of .coffee scripts too

v0.4.2

  • added forcedQueuing flag

v0.4.1

  • added optional snapshotThreshold on aggregate

v0.4.0

  • asynchronous api for saga

v0.3.9

  • optimized performance a little

v0.3.8

  • updated eventstore package
  • optimized initialization

License

Copyright (c) 2014 Adriano Raiano

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

npm loves you