Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
adrai committed May 17, 2012
0 parents commit 0f1ac91
Show file tree
Hide file tree
Showing 144 changed files with 44,909 additions and 0 deletions.
69 changes: 69 additions & 0 deletions README.markdown
@@ -0,0 +1,69 @@
# Introduction

[![Build Status](https://secure.travis-ci.org/adrai/node-cqrs-eventdenormalizer.png)](http://travis-ci.org/adrai/node-cqrs-eventdenormalizer)

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

# Installation

$ npm install node-cqrs-eventdenormalizer

# Usage

## Initialization

var contextEventDenormalizer = require('node-cqrs-eventdenormalizer').contextEventDenormalizer;

contextEventDenormalizer.on('event', function(evt) {
// send to clients
});
contextEventDenormalizer.initialize({
denormalizersPath: __dirname + '/eventDenormalizers',
extendersPath: __dirname + '/eventExtenders'
}, function(err) {

});

contextEventDenormalizer.denormalize({ id: 'msgId', event: 'dummyChanged', payload: { id: '23445' } }, function(err) {

});

## Define eventdenormalizers...

var base = require('node-cqrs-eventdenormalizer').eventDenormalizerBase;

module.exports = base.extend({

events: ['dummied', {'dummyCreated': 'create'}, {'dummyChanged': 'update'}, {'dummyDeleted': 'delete'}],
collectionName: 'dummies',

dummied: function(evt, aux, callback) {
callback(null);
}

});

See [tests](https://github.com/adrai/node-cqrs-eventdenormalizer/tree/master/test) for detailed information...

# License

Copyright (c) 2012 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.
13 changes: 13 additions & 0 deletions index.js
@@ -0,0 +1,13 @@
var index;

if (typeof module.exports !== 'undefined') {
index = module.exports;
} else {
index = root.index = {};
}

index.VERSION = '0.0.1';

index.contextEventDenormalizer = require('./lib/contextEventDenormalizer');
index.eventDenormalizerBase = require('./lib/bases/eventDenormalizerBase');
index.eventExtenderBase = require('./lib/bases/eventExtenderBase');
161 changes: 161 additions & 0 deletions lib/bases/eventDenormalizerBase.js
@@ -0,0 +1,161 @@
var _ = require('underscore')
, eventEmitter = require('../eventEmitter')
, Queue = require('../orderQueue');

var EventDenormalizer = {};
EventDenormalizer.prototype = {

configure: function(fn) {
fn.call(this);
return this;
},

use: function(module) {
if (!module) return;

if (module.commit) {
this.repository = module;
}
},

create: function(evt, aux, callback) {
return this.defaultAction(evt, aux, 'create', callback);
},

update: function(evt, aux, callback) {
return this.defaultAction(evt, aux, 'update', callback);
},

'delete': function(evt, aux, callback) {
return this.defaultAction(evt, aux, 'delete', callback);
},

defaultAction: function(evt, aux, action, callback) {

var self = this;
aux.repository.get(evt.payload.id, function(err, vm) {
// If the view model has just been created (i.e. it has not been
// saved yet), and it shall be deleted, simply discard it and
// return.
if((vm.actionOnCommit === 'create') && (action === 'delete')) {
return callback(null);
}

if(!aux.defaultQueuingStrategy(evt, vm, callback)) {
return;
}

// set the next expected revision
aux.defaultRevisionUpdateStrategy(vm, evt);

if(action !== 'delete') {
_.extend(vm, evt.payload);
} else {
vm.destroy();
}

aux.repository.commit(vm, function(err) {
callback(err); // done this event

// dequeue
aux.defaultDequeuingStrategy(vm);
});
});

},

_getAux: function() {
var self = this;

this._aux = this._aux || {
repository: self.repository,

queueEvent: function(id, evt) {
self.queue.push(id, evt);
},
getQueuedEvents: function(id) {
return self.queue.get(id);
},
removeQueuedEvent: function(id, evt) {
self.queue.remove(id, evt);
},

defaultQueuingStrategy: function(evt, vm, callback) {
if(evt.head.revision < vm._revision) {
callback(null);
return false;
}
if(evt.head.revision > vm._revision) {
this.queueEvent(vm.id, evt);
return false;
}
return true;
},
defaultDequeuingStrategy: function(vm) {
var pendingEvents = this.getQueuedEvents(vm.id);
if(!pendingEvents) return;

var nextEvent = _.find(pendingEvents, function(item) {
return item.head.revision === vm._revision;
});
if(!nextEvent) return;

this.removeQueuedEvent(vm.id, nextEvent); // dequeue event
self.handle(nextEvent); // handle event
},
defaultRevisionUpdateStrategy: function(vm, evt) {
vm._revision = evt.head.revision + 1;
}
};

return this._aux;
},

handle: function(evt) {

// Map events to function names:
// - For the event handler matching the current event, its name is returned
// - For all other event handlers, undefined is returned
var fnNames = _.map(this.events, function(item) {
if (_.isString(item) && item === evt.event) {
return item;
} else if (item[evt.event]) {
return item[evt.event];
}
}
);

// Reduce function names to function name:
// - Replace all undefineds by an empty string
// - Keep all non-undefined values
//
// NOTE: This will fail if multiple event handlers match the current event,
// but this is not allowed anyway, so it can only happen on error.
var fnName = _.reduce(fnNames, function(memo, item) {
return memo + (item || '');
}, '');

if(this[fnName]) {
// Call the event handler found by map-reduce.
this[fnName](evt, this._getAux(), function(err) {
eventEmitter.emit('denormalized:' + evt.event, evt);
});
} else {
throw(new Error('missing handle function'));
}

}

};

module.exports = {

extend: function(obj) {
var newObj = _.extend(_.clone(EventDenormalizer.prototype), obj);

newObj.queue = new Queue();

return newObj;
}

};
80 changes: 80 additions & 0 deletions lib/bases/eventExtenderBase.js
@@ -0,0 +1,80 @@
var _ = require('underscore')
, eventEmitter = require('../eventEmitter');

var EventExtender = {};
EventExtender.prototype = {

configure: function(fn) {
fn.call(this);
return this;
},

use: function(module) {
if (!module) return;

if (module.commit) {
this.repository = module;
}
},

_getAux: function() {
var self = this;

this._aux = this._aux || {
repository: self.repository
};

return this._aux;
},

handle: function(evt) {

// Map events to function names:
// - For the event handler matching the current event, its name is returned
// - For all other event handlers, undefined is returned
var fnNames = _.map(this.events, function(item) {
if (_.isString(item) && item === evt.event) {
return item;
} else if (item[evt.event]) {
return item[evt.event];
}
}
);

// Reduce function names to function name:
// - Replace all undefineds by an empty string
// - Keep all non-undefined values
//
// NOTE: This will fail if multiple event handlers match the current event,
// but this is not allowed anyway, so it can only happen on error.
var fnName = _.reduce(fnNames, function(memo, item) {
return memo + (item || '');
}, '');

if(this[fnName]) {
// Call the event handler found by map-reduce.
this[fnName](evt, this._getAux(), function(err, extEvt) {
extEvt = extEvt || evt;
eventEmitter.emit('extended:' + extEvt.event, extEvt);
});
} else if(this.defaultAction) {
// Call the event handler found by map-reduce.
this.defaultAction(evt, this._getAux(), function(err, extEvt) {
extEvt = extEvt || evt;
eventEmitter.emit('extended:' + extEvt.event, extEvt);
});
} else {
eventEmitter.emit('extended:' + evt.event, evt);
}

}

};

module.exports = {

extend: function(obj) {
return _.extend(_.clone(EventExtender.prototype), obj);
}

};

0 comments on commit 0f1ac91

Please sign in to comment.