cqrs-eventdenormalizer
Advanced tools
Comparing version 1.2.5 to 1.3.0
@@ -217,10 +217,16 @@ 'use strict'; | ||
* Handles denormalization for 1 viewmodel. | ||
* @param {Object} vm The viewModel. | ||
* @param {Object} evt The passed event. | ||
* @param {Function} callback The function, that will be called when this action is completed. | ||
* `function(err, notification){}` | ||
* @param {Object} vm The viewModel. | ||
* @param {Object} evt The passed event. | ||
* @param {Object} initValues The vm init values. [optional] | ||
* @param {Function} callback The function, that will be called when this action is completed. | ||
* `function(err, notification){}` | ||
*/ | ||
handleOne: function (vm, evt, callback) { | ||
handleOne: function (vm, evt, initValues, callback) { | ||
var self = this; | ||
if (!callback) { | ||
callback = initValues; | ||
initValues = null; | ||
} | ||
var payload = evt; | ||
@@ -232,2 +238,6 @@ | ||
if (initValues) { | ||
vm.set(initValues); | ||
} | ||
debug('call denormalizer function'); | ||
@@ -265,3 +275,3 @@ this.denormFn(_.cloneDeep(payload), vm, function (err) { | ||
self.handleOne(vm, evt, callback); | ||
self.handleOne(vm, evt, initValues, callback); | ||
}); | ||
@@ -291,3 +301,2 @@ }, retryIn); | ||
var self = this; | ||
var notifications = []; | ||
this.findViewModels(query, function (err, vms) { | ||
@@ -299,3 +308,3 @@ if (err) { | ||
async.each(vms, function (vm, callback) { | ||
async.map(vms, function (vm, callback) { | ||
self.handleOne(vm, evt, function (err, notification) { | ||
@@ -307,3 +316,39 @@ if (err) { | ||
notifications.push(notification); | ||
callback(null, notification); | ||
}); | ||
}, function (err, notifications) { | ||
if (err) { | ||
debug(err); | ||
return callback(err); | ||
} | ||
callback(null, notifications); | ||
}); | ||
}); | ||
}, | ||
/** | ||
* Denormalizes an event for each item passed in executeForEach function. | ||
* @param {Object} evt The passed event. | ||
* @param {Function} callback The function, that will be called when this action is completed. | ||
* `function(err, notifications){}` | ||
*/ | ||
denormalizeForEach: function (evt, callback) { | ||
var self = this; | ||
this.executeDenormFnForEach(evt, function (err, res) { | ||
if (err) { | ||
debug(err); | ||
return callback(err); | ||
} | ||
async.each(res, function (item, callback) { | ||
if (item.id) { | ||
return callback(null); | ||
} | ||
self.collection.getNewId(function (err, newId) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
item.id = newId; | ||
callback(null); | ||
@@ -317,3 +362,24 @@ }); | ||
callback(null, notifications); | ||
async.map(res, function (item, callback) { | ||
self.loadViewModel(item.id, function (err, vm) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
self.handleOne(vm, evt, item, function (err, notification) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
callback(null, notification); | ||
}); | ||
}); | ||
}, function (err, notis) { | ||
if (err) { | ||
debug(err); | ||
return callback(err); | ||
} | ||
callback(null, notis); | ||
}); | ||
}); | ||
@@ -327,3 +393,3 @@ }); | ||
* @param {Function} callback The function, that will be called when this action is completed. | ||
* `function(err, notification){}` | ||
* `function(err, notifications){}` | ||
*/ | ||
@@ -333,2 +399,6 @@ denormalize: function (evt, callback) { | ||
if (this.executeDenormFnForEach) { | ||
return this.denormalizeForEach(evt, callback); | ||
} | ||
if (this.query) { | ||
@@ -396,2 +466,26 @@ return this.handleQuery(evt, this.query, callback); | ||
return this; | ||
}, | ||
/** | ||
* Inject executeForEach function that will execute denormFn for all resulting objects. | ||
* @param {Function} fn The function to be injected. | ||
* @returns {ViewBuilder} to be able to chain... | ||
*/ | ||
executeForEach: function (fn) { | ||
if (!fn || !_.isFunction(fn)) { | ||
var err = new Error('Please pass a valid function!'); | ||
debug(err); | ||
throw err; | ||
} | ||
if (fn.length === 2) { | ||
this.executeDenormFnForEach = fn; | ||
return this; | ||
} | ||
this.executeDenormFnForEach = function (evt, callback) { | ||
callback(null, fn(evt)); | ||
}; | ||
return this; | ||
} | ||
@@ -398,0 +492,0 @@ |
{ | ||
"author": "adrai", | ||
"name": "cqrs-eventdenormalizer", | ||
"version": "1.2.5", | ||
"version": "1.3.0", | ||
"private": false, | ||
@@ -6,0 +6,0 @@ "main": "index.js", |
@@ -419,4 +419,8 @@ # Introduction | ||
col.find({ my: 'value' }, function (err, vms) {}); | ||
col.findViewModels({ my: 'value' }, function (err, vms) {}); | ||
or | ||
col.loadViewModel('id', function (err, vm) {}); | ||
But be careful with this! | ||
@@ -483,3 +487,3 @@ | ||
}); | ||
// optional define a function to that returns a query that will be used as query to find the viewmodels (but do not define the query in the options) | ||
// optional define a function that returns a query that will be used as query to find the viewmodels (but do not define the query in the options) | ||
//.useAsQuery(function (evt) { | ||
@@ -492,2 +496,10 @@ // return { my: evt.payload.my }; | ||
//}); | ||
// optional define a function that returns a list of items, for each the viewbuilder will run. | ||
//.executeForEach(function (evt) { | ||
// return [{ init: 'value1' }, { init: 'value2' }]; | ||
//}); | ||
// or async | ||
//.executeForEach(function (evt, callback) { | ||
// callback(null, [{ init: 'value1' }, { init: 'value2' }]); | ||
//}); | ||
@@ -494,0 +506,0 @@ ## EventExtender |
@@ -0,1 +1,4 @@ | ||
## [v1.3.0](https://github.com/adrai/node-cqrs-eventdenormalizer/compare/v1.2.5...v1.3.0) | ||
- added executeForEach function for viewBuilders | ||
## [v1.2.5](https://github.com/adrai/node-cqrs-eventdenormalizer/compare/v1.2.4...v1.2.5) | ||
@@ -2,0 +5,0 @@ - added payload functionality for eventExtenders |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
192849
3305
704