Skip to content

Commit

Permalink
Add externallyLoaded option to context, and load those separately (#121)
Browse files Browse the repository at this point in the history
* Add externallyLoaded option to context, and load those separately

* client.quit instead of client.end by redis aggregateLock

* same goes for commandBumper

* revert redis changes

* update readme
  • Loading branch information
nanov authored and adrai committed May 1, 2018
1 parent 1472bc7 commit f204720
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,18 @@ After the initialization you can request the domain information:
name: 'hr'
});

### Externally loaded context ( self-loaded )

A special option to define a context with all its aggregates, commands, events and rules exists by adding the externallyLoaded option to the context :

module.exports = require('cqrs-domain').defineContext({
// optional, default is the directory name
name: 'hr',
externallyLoaded: true
});

When doing so the context will be added 'as-is' to the domain, this means it won't go trough the normal tree loading and parsing process.
This option is aimed mainly at plugin developers, as it leaves the responsibility of structuring the domain right in the hand of the one defining the context ( most-probably a plug-in ).

## Aggregate

Expand Down
2 changes: 2 additions & 0 deletions lib/definitions/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ function Context (meta) {

meta = meta || {};

this.externallyLoaded = meta.externallyLoaded || false;

this.aggregates = [];
}

Expand Down
2 changes: 1 addition & 1 deletion lib/lock/databases/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function Redis(options) {
prefix: 'aggregatelock',
retry_strategy: function (options) {
return undefined;
}//,
}
// heartbeat: 60 * 1000
};

Expand Down
14 changes: 14 additions & 0 deletions lib/structure/structureLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,22 @@ function analyze (dir, useLoaderExtensions, callback) {
});
}

function reorderExternallyLoadedContexts (obj, ordered) {
obj.contexts.forEach(function (ctxItem) {
if (!ctxItem.value.externallyLoaded)
return;
ordered[ctxItem.name] = ctxItem.value;
});
}

function reorderAggregates (obj, ordered) {
var generalContext = new Context({ name: '_general' });

obj.aggregates.forEach(function (aggItem) {
var foundCtx = _.find(obj.contexts, function (ctx) {
if (ctx.value.externallyLoaded) {
return false;
}
if (aggItem.dottiedBase.indexOf('.') >= 0) {
return aggItem.dottiedBase.indexOf(ctx.dottiedBase + '.') === 0;
} else {
Expand All @@ -288,6 +299,9 @@ function reorderAggregates (obj, ordered) {

if (!foundCtx) {
foundCtx = _.find(obj.contexts, function (ctx) {
if (ctx.value.externallyLoaded) {
return false;
}
return ctx.dottiedBase === '';
});
}
Expand Down

0 comments on commit f204720

Please sign in to comment.