forked from thenativeweb/node-cqrs-domain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
49 lines (43 loc) · 1.55 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
var Domain = require('./lib/domain'),
ValidationError = require('./lib/errors/validationError'),
BusinessRuleError = require('./lib/errors/businessRuleError'),
AggregateConcurrencyError = require('./lib/errors/aggregateConcurrencyError'),
AggregateDestroyedError = require('./lib/errors/aggregateDestroyedError'),
ConcurrencyError = require('./lib/errors/concurrencyError'),
DuplicateCommandError = require('./lib/errors/duplicateCommandError'),
_ = require('lodash'),
fs = require('fs'),
path = require('path');
function domain (options) {
return new Domain(options);
}
/**
* Calls the constructor.
* @param {Object} klass Constructor function.
* @param {Array} args Arguments for the constructor function.
* @return {Object} The new object.
*/
function construct(klass, args) {
function T() {
klass.apply(this, arguments[0]);
}
T.prototype = klass.prototype;
return new T(args);
}
var files = fs.readdirSync(path.join(__dirname, 'lib/definitions'));
files.forEach(function (file) {
var name = path.basename(file, '.js');
var nameCap = name.charAt(0).toUpperCase() + name.slice(1);
domain['define' + nameCap] = function () {
return construct(require('./lib/definitions/' + name), _.toArray(arguments));
};
});
domain.errors = {
ValidationError: ValidationError,
BusinessRuleError: BusinessRuleError,
AggregateConcurrencyError: AggregateConcurrencyError,
AggregateDestroyedError: AggregateDestroyedError,
ConcurrencyError: ConcurrencyError,
DuplicateCommandError: DuplicateCommandError
};
module.exports = domain;