cqrs-swissknife
Advanced tools
Comparing version 0.4.45 to 0.4.46
@@ -10,6 +10,21 @@ 'use strict'; | ||
const compositeHandler = handlers => async (event, vm, api) => { | ||
for (const handler of handlers) | ||
await handler(event, vm, api); | ||
} | ||
const buildViewModelFunction = (viewModelFunctions) => { | ||
if (!viewModelFunctions.length) | ||
throw new Error(`No view model function specified for event ${eventFullName}`); | ||
if (viewModelFunctions.length === 1) | ||
return viewModelFunctions[0]; | ||
return compositeHandler(viewModelFunctions); | ||
} | ||
module.exports = ({ eventFullName, reaction, identifier }, { ViewBuilder, PreEventExtender, EventExtender }, customApiBuilder = noop) => { | ||
const [context, aggregate, name] = nameRetriever.event(eventFullName); | ||
let viewModelFunction = null; | ||
let viewModelFunctions = []; | ||
let eventExtenderFunction; | ||
@@ -30,3 +45,3 @@ let preEventExtenderFunction; | ||
if (typeof item === 'function') { | ||
viewModelFunction = asyncParamApiCallback(item, customApiBuilder, 'event', 'vm'); | ||
viewModelFunctions.push(item); // asyncParamApiCallback(item, customApiBuilder, 'event', 'vm')); | ||
return; | ||
@@ -59,4 +74,3 @@ } | ||
if (!viewModelFunction) | ||
throw new Error(`No view model function specified for event ${eventFullName}`); | ||
const viewModelFunction = asyncParamApiCallback(buildViewModelFunction(viewModelFunctions), customApiBuilder, 'event', 'vm'); | ||
@@ -66,3 +80,2 @@ if (!identifier) | ||
if (typeof identifier === 'string') | ||
@@ -69,0 +82,0 @@ modelSettings.id = identifier; |
@@ -5,2 +5,3 @@ 'use strict'; | ||
const merge = require('lodash.merge'); | ||
const { cloneDeep } = require('lodash'); | ||
@@ -12,5 +13,6 @@ const { valueop } = require('../../utils'); | ||
const modeSymbol = Symbol('aggregate:modeSymbol'); | ||
const apiSymbol = Symbol('aggregate:apiSymbol'); | ||
const HANDLER_MORE = 'handler'; | ||
const ASYNC_HANDLER_MODE = 'async_handle'; | ||
// const ASYNC_HANDLER_MODE = 'async_handle'; | ||
@@ -75,5 +77,6 @@ // direct copy from : https://github.com/adrai/node-cqrs-domain/blob/9b17c73853ec59d451d3101492cb00b16e1ec9e3/lib/definitions/aggregate.js#L70 | ||
const enrichedEvent = eventEnricher(event, model, cmd) || event; | ||
enrichedEvent.__command = cmd; | ||
model.addUncommittedEvent(enrichedEvent); | ||
aggregate.apply(enrichedEvent, model); | ||
aggregate.apply(cloneDeep(enrichedEvent), model); | ||
@@ -87,5 +90,62 @@ model.set = () => { | ||
const applyGenerator = (ctx, applyProto, aggregate, eventEnricher) => { | ||
const apply = function apply(name, payload, metadata) { | ||
if (this[modeSymbol] === HANDLER_MORE) | ||
return generateEvent(aggregate, this._aggregateModel, eventEnricher, this[commandSymbol], name, payload, metadata); | ||
return this[deferredEventsSymbol].push([name, payload, metadata]); | ||
// this._aggregateModel.apply(eventEnricher(evt, this._aggregateModel, this._command) || evt); | ||
}; | ||
apply.__self = ctx; | ||
Object.setPrototypeOf(apply, applyProto); | ||
return apply; | ||
}; | ||
class AggregateApi { | ||
constructor(aggregateModel, command, mode = HANDLER_MORE, { applyProto, aggregate, eventEnricher }) { | ||
this._aggregateModel = aggregateModel; | ||
this[deferredEventsSymbol] = []; | ||
this._aggregateModel[apiSymbol] = this; | ||
this.id = this._aggregateModel.id; | ||
this[modeSymbol] = mode; | ||
this[commandSymbol] = command; | ||
this.apply = applyGenerator(this, applyProto, aggregate, eventEnricher); | ||
} | ||
get(attr) { | ||
if (!attr) | ||
return this._aggregateModel.attributes; | ||
return this._aggregateModel.get(attr); | ||
} | ||
_getDeferredEvents() { | ||
return this[deferredEventsSymbol]; | ||
} | ||
} | ||
const generateAggregateApi = (aggregate, eventEnricher = valueop) => { | ||
const applyProto = Object.create(Function.prototype); | ||
aggregate.events.forEach(({ name }) => { | ||
applyProto[name] = function applyEvent(payload, metadata) { | ||
this.call(this.__self, name, payload, metadata); | ||
}; | ||
}); | ||
return (aggregateModel, command, mode = HANDLER_MORE) => { | ||
if (!aggregateModel[apiSymbol]) | ||
return new AggregateApi(aggregateModel, command, mode, { applyProto, aggregate, eventEnricher }); | ||
aggregateModel[apiSymbol][modeSymbol] = mode; | ||
return aggregateModel[apiSymbol]; | ||
}; | ||
/* | ||
const AggregateApi = function AggregateApi(aggregateModel, command, mode = HANDLER_MORE) { | ||
this._aggregateModel = aggregateModel; | ||
this[deferredEventsSymbol] = []; | ||
this._aggregateModel[apiSymbol] = this; | ||
this.id = this._aggregateModel.id; | ||
@@ -96,10 +156,3 @@ | ||
this.apply.__self = this; | ||
if (mode === ASYNC_HANDLER_MODE && !this._aggregateModel[deferredEventsSymbol]) { | ||
this._aggregateModel[deferredEventsSymbol] = this._aggregateModel[deferredEventsSymbol] || []; | ||
} else if (mode === HANDLER_MORE && this._aggregateModel[deferredEventsSymbol]) { | ||
this._aggregateModel[deferredEventsSymbol].forEach(params => this.apply(...params)); | ||
this._aggregateModel[deferredEventsSymbol] = []; | ||
} | ||
this.apply = applyGenerator(this, applyProto, aggregate, eventEnricher); | ||
}; | ||
@@ -113,6 +166,11 @@ | ||
AggregateApi | ||
/* | ||
AggregateApi.prototype.apply = function apply(name, payload, metadata) { | ||
if (aggregate.name === 'file' && aggregate.context.name === 'operations') | ||
console.log('--apply--', name, this[modeSymbol], this[commandSymbol].metadata.causationId, this.__mode); | ||
if (this[modeSymbol] === HANDLER_MORE) | ||
return generateEvent(aggregate, this._aggregateModel, eventEnricher, this[commandSymbol], name, payload, metadata); | ||
return this._aggregateModel[deferredEventsSymbol].push([name, payload, metadata]); | ||
console.log('deffering event', this[commandSymbol].metadata.causationId); | ||
return this.__deferredEventsSymbol.push([name, payload, metadata]); | ||
// this._aggregateModel.apply(eventEnricher(evt, this._aggregateModel, this._command) || evt); | ||
@@ -127,5 +185,15 @@ }; | ||
AggregateApi.generate = function generate(aggregateModel, command, mode = HANDLER_MORE) { | ||
if (!aggregateModel[apiSymbol]) | ||
return new AggregateApi(aggregateModel, command, mode); | ||
aggregateModel[apiSymbol][modeSymbol] = mode; | ||
return aggregateModel[apiSymbol]; | ||
}; | ||
return AggregateApi; | ||
*/ | ||
}; | ||
module.exports = generateAggregateApi; |
@@ -27,8 +27,8 @@ 'use strict'; | ||
return commandBusinessRules.map((f,i) => ({ | ||
name: `${aggregate.context}:${aggregate.name}:businessRule:${command.name}:${0}`, | ||
rule(current, previous, events, cmd) { | ||
return commandBusinessRules.map((f, i) => ({ | ||
name: `${aggregate.context}:${aggregate.name}:businessRule:${command.name}:${i}`, | ||
rule(current, previous, events, cmd, api) { | ||
if (dotty.get(cmd, command.definitions.command.name) === command.name) | ||
return f(current, previous, events, cmd) | ||
return; | ||
return f(current, previous, events, cmd, api); | ||
return null; | ||
}, | ||
@@ -70,11 +70,13 @@ })); | ||
// generate aggregateApi class | ||
const AggregateApi = generateAggregateApi(aggregate, eventEnricher); | ||
const aggregateApi = generateAggregateApi(aggregate, eventEnricher); | ||
// define commandModels | ||
const commandBussinessRules = toFlatArray(await Promise.all(Object.entries(commands).map(async ([commandName, command]) => addCommandToAggregate(aggregate, await commandBuilder({ commandName, command, AggregateApi }, definitions, customApiBuilder))))); | ||
const commandBussinessRules = toFlatArray(await Promise.all(Object.entries(commands).map(async ([commandName, command]) => addCommandToAggregate(aggregate, await commandBuilder({ commandName, command, aggregateApi }, definitions, customApiBuilder))))); | ||
// process buissnessRules | ||
businessRulesBuilder({ context, aggregateName, commandBussinessRules, rules }, definitions, customApiBuilder).map(rule => addRuleToAggregate(aggregate, { rule })); | ||
businessRulesBuilder({ | ||
context, aggregateName, commandBussinessRules, rules, | ||
}, definitions, customApiBuilder).map(rule => addRuleToAggregate(aggregate, { rule })); | ||
return aggregate; | ||
}; |
@@ -48,3 +48,3 @@ 'use strict'; | ||
command, | ||
AggregateApi, | ||
aggregateApi, | ||
}, | ||
@@ -71,3 +71,3 @@ { | ||
validator: null, | ||
businessRules: [], | ||
commandBusinessRules: [], | ||
}; | ||
@@ -78,3 +78,3 @@ | ||
if (typeof item === 'function') { | ||
result.command = new Command(commandSettings, (cmd, agg) => item(cmd, new AggregateApi(agg, cmd, 'handler'), customApiBuilder(cmd, agg))); | ||
result.command = new Command(commandSettings, (cmd, agg) => item(cmd, aggregateApi(agg, cmd, 'handler'), customApiBuilder(cmd, agg))); | ||
continue; | ||
@@ -100,3 +100,3 @@ } | ||
if (item.preCondition) { | ||
const condition = ('mode' in item) ? async (cmd, agg) => item.preCondition(cmd, new AggregateApi(agg, cmd, item.mode), customApiBuilder(cmd, agg)) : item.preCondition; | ||
const condition = ('mode' in item) ? async (cmd, agg, api) => item.preCondition(cmd, aggregateApi(agg, cmd, item.mode), api) : item.preCondition; | ||
result.preConditions.push(new PreCondition({ name: [commandName] }, asyncParamCustomErrorApiCallback(condition, errorBuilders.businessRule, customApiBuilder, 'cmd', 'agg'))); | ||
@@ -107,3 +107,2 @@ } | ||
result.commandBusinessRules.push(item.commandBusinessRule); | ||
} | ||
@@ -110,0 +109,0 @@ |
@@ -20,7 +20,7 @@ 'use strict'; | ||
const commandBusinessRule = (rule) => { | ||
if (!rule || typeof rule !== 'function') | ||
const commandBusinessRule = (commandBusinessRule) => { | ||
if (!commandBusinessRule || typeof commandBusinessRule !== 'function') | ||
throw new Error('Rule missing or not a function.'); | ||
return { commandBusinessRule: condition }; | ||
return { commandBusinessRule }; | ||
}; | ||
@@ -27,0 +27,0 @@ |
{ | ||
"name": "cqrs-swissknife", | ||
"version": "0.4.45", | ||
"version": "0.4.46", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
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
35718
926