Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add externallyLoaded option to context, and load those separately #121

Merged
merged 5 commits into from
May 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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