UNPKG

1 kBJavaScriptView Raw
1'use strict';
2
3const loader = require('./loader');
4const aggregateBuilder = require('./builders/aggregateBuilder');
5
6const buildContext = ([contextName, aggregates], { Context, ...definitions }) => {
7 const context = new Context({ name: contextName, externallyLoaded: true });
8
9 Object.entries(aggregates).forEach(([aggregateName, aggregate]) => {
10 const aggregateFile = aggregate.path;
11 aggregateBuilder(context, aggregateName, require(aggregateFile), definitions); // eslint-disable-line
12 });
13
14 return context;
15};
16
17const buildDomain = (domain, definitions) => Object.entries(domain).reduce((domainTree, entries) => {
18 domainTree[entries[0]] = buildContext(entries, definitions);
19 return domainTree;
20}, {});
21
22// Domain may be a path to the domain dir or a loaded domain object
23// Definitions come from cqrs-domain module
24module.exports = (domain, definitions) => {
25 if (typeof domain === 'string' || domain instanceof String)
26 domain = loader(domain);
27 return buildDomain(domain, definitions);
28};