@celljs/cqrs 0.0.0-stage → 4.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +193 -2
- package/esm/node/aggregate/aggregate-root.d.ts +24 -0
- package/esm/node/aggregate/aggregate-root.js +69 -0
- package/esm/node/aggregate/aggregate-root.js.map +1 -0
- package/esm/node/aggregate/event-publisher.d.ts +16 -0
- package/esm/node/aggregate/event-publisher.js +42 -0
- package/esm/node/aggregate/event-publisher.js.map +1 -0
- package/esm/node/aggregate/index.d.ts +2 -0
- package/esm/node/aggregate/index.js +3 -0
- package/esm/node/aggregate/index.js.map +1 -0
- package/esm/node/command/command-bus.d.ts +11 -0
- package/esm/node/command/command-bus.js +36 -0
- package/esm/node/command/command-bus.js.map +1 -0
- package/esm/node/command/command-protocol.d.ts +30 -0
- package/esm/node/command/command-protocol.js +17 -0
- package/esm/node/command/command-protocol.js.map +1 -0
- package/esm/node/command/index.d.ts +2 -0
- package/esm/node/command/index.js +3 -0
- package/esm/node/command/index.js.map +1 -0
- package/esm/node/error/cqrs-error.d.ts +18 -0
- package/esm/node/error/cqrs-error.js +19 -0
- package/esm/node/error/cqrs-error.js.map +1 -0
- package/esm/node/error/index.d.ts +1 -0
- package/esm/node/error/index.js +2 -0
- package/esm/node/error/index.js.map +1 -0
- package/esm/node/event/event-bus.d.ts +17 -0
- package/esm/node/event/event-bus.js +81 -0
- package/esm/node/event/event-bus.js.map +1 -0
- package/esm/node/event/event-protocol.d.ts +42 -0
- package/esm/node/event/event-protocol.js +17 -0
- package/esm/node/event/event-protocol.js.map +1 -0
- package/esm/node/event/index.d.ts +3 -0
- package/esm/node/event/index.js +4 -0
- package/esm/node/event/index.js.map +1 -0
- package/esm/node/event/unhandled-exception-bus.d.ts +8 -0
- package/esm/node/event/unhandled-exception-bus.js +35 -0
- package/esm/node/event/unhandled-exception-bus.js.map +1 -0
- package/esm/node/index.d.ts +6 -0
- package/esm/node/index.js +7 -0
- package/esm/node/index.js.map +1 -0
- package/esm/node/module.d.ts +3 -0
- package/esm/node/module.js +4 -0
- package/esm/node/module.js.map +1 -0
- package/esm/node/query/index.d.ts +2 -0
- package/esm/node/query/index.js +3 -0
- package/esm/node/query/index.js.map +1 -0
- package/esm/node/query/query-bus.d.ts +11 -0
- package/esm/node/query/query-bus.js +36 -0
- package/esm/node/query/query-bus.js.map +1 -0
- package/esm/node/query/query-protocol.d.ts +31 -0
- package/esm/node/query/query-protocol.js +18 -0
- package/esm/node/query/query-protocol.js.map +1 -0
- package/esm/node/utils/cqrs-util.d.ts +25 -0
- package/esm/node/utils/cqrs-util.js +85 -0
- package/esm/node/utils/cqrs-util.js.map +1 -0
- package/esm/node/utils/index.d.ts +1 -0
- package/esm/node/utils/index.js +2 -0
- package/esm/node/utils/index.js.map +1 -0
- package/esm/package.json +3 -0
- package/lib/node/aggregate/aggregate-root.d.ts +25 -0
- package/lib/node/aggregate/aggregate-root.d.ts.map +1 -0
- package/lib/node/aggregate/aggregate-root.js +73 -0
- package/lib/node/aggregate/aggregate-root.js.map +1 -0
- package/lib/node/aggregate/event-publisher.d.ts +17 -0
- package/lib/node/aggregate/event-publisher.d.ts.map +1 -0
- package/lib/node/aggregate/event-publisher.js +45 -0
- package/lib/node/aggregate/event-publisher.js.map +1 -0
- package/lib/node/aggregate/index.d.ts +3 -0
- package/lib/node/aggregate/index.d.ts.map +1 -0
- package/lib/node/aggregate/index.js +19 -0
- package/lib/node/aggregate/index.js.map +1 -0
- package/lib/node/command/command-bus.d.ts +12 -0
- package/lib/node/command/command-bus.d.ts.map +1 -0
- package/lib/node/command/command-bus.js +39 -0
- package/lib/node/command/command-bus.js.map +1 -0
- package/lib/node/command/command-protocol.d.ts +31 -0
- package/lib/node/command/command-protocol.d.ts.map +1 -0
- package/lib/node/command/command-protocol.js +22 -0
- package/lib/node/command/command-protocol.js.map +1 -0
- package/lib/node/command/index.d.ts +3 -0
- package/lib/node/command/index.d.ts.map +1 -0
- package/lib/node/command/index.js +19 -0
- package/lib/node/command/index.js.map +1 -0
- package/lib/node/cqrs.spec.d.ts +76 -0
- package/lib/node/cqrs.spec.d.ts.map +1 -0
- package/lib/node/cqrs.spec.js +319 -0
- package/lib/node/cqrs.spec.js.map +1 -0
- package/lib/node/error/cqrs-error.d.ts +19 -0
- package/lib/node/error/cqrs-error.d.ts.map +1 -0
- package/lib/node/error/cqrs-error.js +23 -0
- package/lib/node/error/cqrs-error.js.map +1 -0
- package/lib/node/error/index.d.ts +2 -0
- package/lib/node/error/index.d.ts.map +1 -0
- package/lib/node/error/index.js +18 -0
- package/lib/node/error/index.js.map +1 -0
- package/lib/node/event/event-bus.d.ts +18 -0
- package/lib/node/event/event-bus.d.ts.map +1 -0
- package/lib/node/event/event-bus.js +84 -0
- package/lib/node/event/event-bus.js.map +1 -0
- package/lib/node/event/event-protocol.d.ts +43 -0
- package/lib/node/event/event-protocol.d.ts.map +1 -0
- package/lib/node/event/event-protocol.js +22 -0
- package/lib/node/event/event-protocol.js.map +1 -0
- package/lib/node/event/index.d.ts +4 -0
- package/lib/node/event/index.d.ts.map +1 -0
- package/lib/node/event/index.js +20 -0
- package/lib/node/event/index.js.map +1 -0
- package/lib/node/event/unhandled-exception-bus.d.ts +9 -0
- package/lib/node/event/unhandled-exception-bus.d.ts.map +1 -0
- package/lib/node/event/unhandled-exception-bus.js +38 -0
- package/lib/node/event/unhandled-exception-bus.js.map +1 -0
- package/lib/node/index.d.ts +7 -0
- package/lib/node/index.d.ts.map +1 -0
- package/lib/node/index.js +23 -0
- package/lib/node/index.js.map +1 -0
- package/lib/node/module.d.ts +4 -0
- package/lib/node/module.d.ts.map +1 -0
- package/lib/node/module.js +6 -0
- package/lib/node/module.js.map +1 -0
- package/lib/node/query/index.d.ts +3 -0
- package/lib/node/query/index.d.ts.map +1 -0
- package/lib/node/query/index.js +19 -0
- package/lib/node/query/index.js.map +1 -0
- package/lib/node/query/query-bus.d.ts +12 -0
- package/lib/node/query/query-bus.d.ts.map +1 -0
- package/lib/node/query/query-bus.js +39 -0
- package/lib/node/query/query-bus.js.map +1 -0
- package/lib/node/query/query-protocol.d.ts +32 -0
- package/lib/node/query/query-protocol.d.ts.map +1 -0
- package/lib/node/query/query-protocol.js +23 -0
- package/lib/node/query/query-protocol.js.map +1 -0
- package/lib/node/utils/cqrs-util.d.ts +26 -0
- package/lib/node/utils/cqrs-util.d.ts.map +1 -0
- package/lib/node/utils/cqrs-util.js +88 -0
- package/lib/node/utils/cqrs-util.js.map +1 -0
- package/lib/node/utils/index.d.ts +2 -0
- package/lib/node/utils/index.d.ts.map +1 -0
- package/lib/node/utils/index.js +18 -0
- package/lib/node/utils/index.js.map +1 -0
- package/package.json +88 -4
- package/src/node/aggregate/aggregate-root.ts +78 -0
- package/src/node/aggregate/event-publisher.ts +43 -0
- package/src/node/aggregate/index.ts +2 -0
- package/src/node/command/command-bus.ts +28 -0
- package/src/node/command/command-protocol.ts +39 -0
- package/src/node/command/index.ts +2 -0
- package/src/node/cqrs.spec.ts +316 -0
- package/src/node/error/cqrs-error.ts +23 -0
- package/src/node/error/index.ts +1 -0
- package/src/node/event/event-bus.ts +70 -0
- package/src/node/event/event-protocol.ts +60 -0
- package/src/node/event/index.ts +3 -0
- package/src/node/event/unhandled-exception-bus.ts +21 -0
- package/src/node/index.ts +6 -0
- package/src/node/module.ts +4 -0
- package/src/node/query/index.ts +2 -0
- package/src/node/query/query-bus.ts +28 -0
- package/src/node/query/query-protocol.ts +40 -0
- package/src/node/utils/cqrs-util.ts +90 -0
- package/src/node/utils/index.ts +1 -0
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CqrsUtil = exports.METADATA_KEY = void 0;
|
|
4
|
+
const core_1 = require("@celljs/core");
|
|
5
|
+
const error_1 = require("../error");
|
|
6
|
+
exports.METADATA_KEY = {
|
|
7
|
+
commandHandler: 'cell:cqrs:command-handler',
|
|
8
|
+
queryHandler: 'cell:cqrs:query-handler',
|
|
9
|
+
eventHandler: 'cell:cqrs:event-handler',
|
|
10
|
+
saga: 'cell:cqrs:saga',
|
|
11
|
+
};
|
|
12
|
+
const DECORATOR = { command: '@CommandHandler', query: '@QueryHandler', event: '@EventHandler' };
|
|
13
|
+
var CqrsUtil;
|
|
14
|
+
(function (CqrsUtil) {
|
|
15
|
+
/**
|
|
16
|
+
* Registers the class as a component that is also bound to `id` and records the message classes it handles,
|
|
17
|
+
* for the bus that collects the components of `id`.
|
|
18
|
+
*/
|
|
19
|
+
function applyHandlerDecorator(target, id, metadataKey, types, option) {
|
|
20
|
+
const parsed = (0, core_1.parseComponentOption)(target, option);
|
|
21
|
+
parsed.id.push(id);
|
|
22
|
+
Reflect.defineMetadata(metadataKey, types, target);
|
|
23
|
+
(0, core_1.Component)(parsed)(target);
|
|
24
|
+
}
|
|
25
|
+
CqrsUtil.applyHandlerDecorator = applyHandlerDecorator;
|
|
26
|
+
/** The class of a command, query or event: what selects its handlers. */
|
|
27
|
+
function typeOf(message, kind) {
|
|
28
|
+
const type = message !== null && typeof message === 'object' ? message.constructor : undefined;
|
|
29
|
+
if (typeof type !== 'function' || type === Object) {
|
|
30
|
+
throw new error_1.CqrsError({
|
|
31
|
+
code: 'CELL_CQRS_NOT_AN_INSTANCE',
|
|
32
|
+
message: `${kind === 'event' ? 'An' : 'A'} ${kind} must be an instance of a class, got ${describe(message)}.`,
|
|
33
|
+
hint: `Declare a class for the ${kind}, dispatch new MyClass(...) and decorate its handler with ${DECORATOR[kind]}(MyClass).`,
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
return type;
|
|
37
|
+
}
|
|
38
|
+
CqrsUtil.typeOf = typeOf;
|
|
39
|
+
/** The handlers by the message class they handle, in the order the container gives them. */
|
|
40
|
+
function index(handlers, metadataKey) {
|
|
41
|
+
const result = new Map();
|
|
42
|
+
for (const handler of handlers) {
|
|
43
|
+
const types = Reflect.getOwnMetadata(metadataKey, (0, core_1.getTargetClass)(handler)) || [];
|
|
44
|
+
for (const type of types) {
|
|
45
|
+
const list = result.get(type);
|
|
46
|
+
if (list) {
|
|
47
|
+
list.push(handler);
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
result.set(type, [handler]);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return result;
|
|
55
|
+
}
|
|
56
|
+
CqrsUtil.index = index;
|
|
57
|
+
/** The one handler of each message class; commands and queries have exactly one. */
|
|
58
|
+
function single(handlers, metadataKey, kind) {
|
|
59
|
+
const result = new Map();
|
|
60
|
+
for (const [type, list] of index(handlers, metadataKey)) {
|
|
61
|
+
if (list.length > 1) {
|
|
62
|
+
throw new error_1.CqrsError({
|
|
63
|
+
code: 'CELL_CQRS_DUPLICATE_HANDLER',
|
|
64
|
+
message: `The ${kind} ${type.name} has ${list.length} handlers: ${list.map(h => (0, core_1.getTargetClass)(h).name).join(', ')}.`,
|
|
65
|
+
hint: `Keep one ${DECORATOR[kind]}(${type.name}) class; to react to something in several places, publish an event.`,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
result.set(type, list[0]);
|
|
69
|
+
}
|
|
70
|
+
return result;
|
|
71
|
+
}
|
|
72
|
+
CqrsUtil.single = single;
|
|
73
|
+
function noHandler(type, kind) {
|
|
74
|
+
return new error_1.CqrsError({
|
|
75
|
+
code: 'CELL_CQRS_NO_HANDLER',
|
|
76
|
+
message: `No handler for the ${kind} ${type.name}.`,
|
|
77
|
+
hint: `Decorate a class with ${DECORATOR[kind]}(${type.name}) in a file that the DI module of your application imports.`,
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
CqrsUtil.noHandler = noHandler;
|
|
81
|
+
function describe(value) {
|
|
82
|
+
if (value === null || value === undefined) {
|
|
83
|
+
return String(value);
|
|
84
|
+
}
|
|
85
|
+
return typeof value === 'object' ? 'a plain object' : `a ${typeof value}`;
|
|
86
|
+
}
|
|
87
|
+
})(CqrsUtil || (exports.CqrsUtil = CqrsUtil = {}));
|
|
88
|
+
//# sourceMappingURL=cqrs-util.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cqrs-util.js","sourceRoot":"","sources":["../../../src/node/utils/cqrs-util.ts"],"names":[],"mappings":";;;AAAA,uCAAuH;AACvH,oCAAqC;AAOxB,QAAA,YAAY,GAAG;IACxB,cAAc,EAAE,2BAA2B;IAC3C,YAAY,EAAE,yBAAyB;IACvC,YAAY,EAAE,yBAAyB;IACvC,IAAI,EAAE,gBAAgB;CACzB,CAAC;AAEF,MAAM,SAAS,GAAgC,EAAE,OAAO,EAAE,iBAAiB,EAAE,KAAK,EAAE,eAAe,EAAE,KAAK,EAAE,eAAe,EAAE,CAAC;AAE9H,IAAiB,QAAQ,CAwExB;AAxED,WAAiB,QAAQ;IACrB;;;OAGG;IACH,SAAgB,qBAAqB,CAAC,MAAW,EAAE,EAAe,EAAE,WAAmB,EAAE,KAAoB,EAAE,MAAwB;QACnI,MAAM,MAAM,GAAG,IAAA,2BAAoB,EAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QACnD,MAAM,CAAC,EAAoB,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QACtC,OAAO,CAAC,cAAc,CAAC,WAAW,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC;QACnD,IAAA,gBAAS,EAAC,MAAM,CAAC,CAAC,MAAM,CAAC,CAAC;IAC9B,CAAC;IALe,SAAA,qBAAqB,wBAKpC,CAAA;IAED,yEAAyE;IACzE,SAAgB,MAAM,CAAC,OAAgB,EAAE,IAAiB;QACtD,MAAM,IAAI,GAAG,OAAO,KAAK,IAAI,IAAI,OAAO,OAAO,KAAK,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,SAAS,CAAC;QAC/F,IAAI,OAAO,IAAI,KAAK,UAAU,IAAI,IAAI,KAAK,MAAM,EAAE,CAAC;YAChD,MAAM,IAAI,iBAA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aAAR,QAAQ,GAAR,QAAQ,QAwExB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/node/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./cqrs-util"), exports);
|
|
18
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/node/utils/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,8CAA4B"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,90 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@celljs/cqrs",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "4.4.0",
|
|
4
|
+
"description": "Commands, queries, events, sagas and event-sourced aggregates for Cell applications (CQRS)",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"cell-component"
|
|
7
|
+
],
|
|
8
|
+
"homepage": "https://github.com/cellbang/cell",
|
|
9
|
+
"bugs": {
|
|
10
|
+
"url": "https://github.com/cellbang/cell/issues"
|
|
11
|
+
},
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "https://github.com/cellbang/cell.git"
|
|
15
|
+
},
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"engines": {
|
|
18
|
+
"node": ">=18.0.0"
|
|
19
|
+
},
|
|
20
|
+
"main": "lib/node/index.js",
|
|
21
|
+
"typings": "lib/node/index.d.ts",
|
|
22
|
+
"files": [
|
|
23
|
+
"lib",
|
|
24
|
+
"esm",
|
|
25
|
+
"src"
|
|
26
|
+
],
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "tsc -b && bun ../../scripts/build-esm.ts",
|
|
29
|
+
"clean": "rm -rf lib esm *.tsbuildinfo",
|
|
30
|
+
"test": "bun test --preload reflect-metadata ./lib",
|
|
31
|
+
"watch": "tsc -b -w --preserveWatchOutput"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@celljs/core": "4.4.0"
|
|
35
|
+
},
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public"
|
|
38
|
+
},
|
|
39
|
+
"cell": {
|
|
40
|
+
"modules": {
|
|
41
|
+
"backend": [
|
|
42
|
+
"lib/node/module"
|
|
43
|
+
]
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
"exports": {
|
|
47
|
+
".": {
|
|
48
|
+
"import": "./esm/node/index.js",
|
|
49
|
+
"default": "./lib/node/index.js"
|
|
50
|
+
},
|
|
51
|
+
"./package.json": "./package.json",
|
|
52
|
+
"./lib/node": {
|
|
53
|
+
"import": "./esm/node/index.js",
|
|
54
|
+
"default": "./lib/node/index.js"
|
|
55
|
+
},
|
|
56
|
+
"./lib/node/aggregate": {
|
|
57
|
+
"import": "./esm/node/aggregate/index.js",
|
|
58
|
+
"default": "./lib/node/aggregate/index.js"
|
|
59
|
+
},
|
|
60
|
+
"./lib/node/command": {
|
|
61
|
+
"import": "./esm/node/command/index.js",
|
|
62
|
+
"default": "./lib/node/command/index.js"
|
|
63
|
+
},
|
|
64
|
+
"./lib/node/error": {
|
|
65
|
+
"import": "./esm/node/error/index.js",
|
|
66
|
+
"default": "./lib/node/error/index.js"
|
|
67
|
+
},
|
|
68
|
+
"./lib/node/event": {
|
|
69
|
+
"import": "./esm/node/event/index.js",
|
|
70
|
+
"default": "./lib/node/event/index.js"
|
|
71
|
+
},
|
|
72
|
+
"./lib/node/query": {
|
|
73
|
+
"import": "./esm/node/query/index.js",
|
|
74
|
+
"default": "./lib/node/query/index.js"
|
|
75
|
+
},
|
|
76
|
+
"./lib/node/utils": {
|
|
77
|
+
"import": "./esm/node/utils/index.js",
|
|
78
|
+
"default": "./lib/node/utils/index.js"
|
|
79
|
+
},
|
|
80
|
+
"./lib/*.js": {
|
|
81
|
+
"import": "./esm/*.js",
|
|
82
|
+
"default": "./lib/*.js"
|
|
83
|
+
},
|
|
84
|
+
"./lib/*": {
|
|
85
|
+
"import": "./esm/*.js",
|
|
86
|
+
"default": "./lib/*.js"
|
|
87
|
+
},
|
|
88
|
+
"./*": "./*"
|
|
89
|
+
}
|
|
90
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { CqrsError } from '../error';
|
|
2
|
+
|
|
3
|
+
const uncommitted = new WeakMap<AggregateRoot<any>, object[]>();
|
|
4
|
+
|
|
5
|
+
function eventsOf(aggregate: AggregateRoot<any>): object[] {
|
|
6
|
+
let events = uncommitted.get(aggregate);
|
|
7
|
+
if (!events) {
|
|
8
|
+
events = [];
|
|
9
|
+
uncommitted.set(aggregate, events);
|
|
10
|
+
}
|
|
11
|
+
return events;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* An entity whose changes are events: `apply(event)` records the event and calls the method `on<EventClassName>`
|
|
16
|
+
* of the aggregate, if there is one, to change its state; `commit()` publishes the recorded events.
|
|
17
|
+
* Before `commit()`, connect the aggregate to the event bus with `EventPublisher.mergeObjectContext`
|
|
18
|
+
* (or create it from a class given by `EventPublisher.mergeClassContext`).
|
|
19
|
+
*/
|
|
20
|
+
export abstract class AggregateRoot<E extends object = object> {
|
|
21
|
+
/** Records the event, unless it is replayed from history, and applies it to the state with `on<EventClassName>`. */
|
|
22
|
+
apply(event: E, fromHistory = false): void {
|
|
23
|
+
if (!fromHistory) {
|
|
24
|
+
eventsOf(this).push(event);
|
|
25
|
+
}
|
|
26
|
+
this.getEventHandler(event)?.call(this, event);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Publishes the recorded events, in order, and forgets them. */
|
|
30
|
+
async commit(): Promise<void> {
|
|
31
|
+
const events = eventsOf(this);
|
|
32
|
+
if (events.length === 0) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const committing = events.slice() as E[];
|
|
36
|
+
await this.publishAll(committing);
|
|
37
|
+
events.splice(0, committing.length);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Forgets the recorded events without publishing them. */
|
|
41
|
+
uncommit(): void {
|
|
42
|
+
eventsOf(this).length = 0;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
getUncommittedEvents(): E[] {
|
|
46
|
+
return eventsOf(this).slice() as E[];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Rebuilds the state from past events; they are not recorded again. */
|
|
50
|
+
loadFromHistory(history: E[]): void {
|
|
51
|
+
for (const event of history) {
|
|
52
|
+
this.apply(event, true);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Replaced by `EventPublisher`, which publishes with the `EventBus`. */
|
|
57
|
+
publish(event: E): Promise<void> {
|
|
58
|
+
return this.publishAll([event]);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Replaced by `EventPublisher`, which publishes with the `EventBus`. */
|
|
62
|
+
publishAll(events: E[]): Promise<void> {
|
|
63
|
+
return Promise.reject(this.notMerged(events));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
protected getEventHandler(event: E): ((event: E) => void) | undefined {
|
|
67
|
+
const handler = (this as any)[`on${event.constructor.name}`];
|
|
68
|
+
return typeof handler === 'function' ? handler : undefined;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
protected notMerged(events: E[]): CqrsError {
|
|
72
|
+
return new CqrsError({
|
|
73
|
+
code: 'CELL_CQRS_AGGREGATE_NOT_MERGED',
|
|
74
|
+
message: `${this.constructor.name} cannot publish ${events.map(e => e.constructor.name).join(', ')}: it is not connected to the event bus.`,
|
|
75
|
+
hint: 'Inject EventPublisher and pass the aggregate to eventPublisher.mergeObjectContext(aggregate) before commit().',
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { Autowired, Component } from '@celljs/core';
|
|
2
|
+
import { EventBus } from '../event/event-protocol';
|
|
3
|
+
import type { AggregateRoot } from './aggregate-root';
|
|
4
|
+
|
|
5
|
+
export const EventPublisher = Symbol('EventPublisher');
|
|
6
|
+
|
|
7
|
+
export type AggregateClass<T extends AggregateRoot<any>> = new (...args: any[]) => T;
|
|
8
|
+
|
|
9
|
+
/** Connects aggregates to the `EventBus`, so that their `commit()` publishes their events. */
|
|
10
|
+
export interface EventPublisher {
|
|
11
|
+
/** Connects the aggregate and returns it. */
|
|
12
|
+
mergeObjectContext<T extends AggregateRoot<any>>(aggregate: T): T;
|
|
13
|
+
/** A subclass of the aggregate class whose instances are connected. */
|
|
14
|
+
mergeClassContext<T extends AggregateClass<AggregateRoot<any>>>(type: T): T;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
@Component(EventPublisher)
|
|
18
|
+
export class EventPublisherImpl implements EventPublisher {
|
|
19
|
+
@Autowired(EventBus)
|
|
20
|
+
protected readonly eventBus: EventBus;
|
|
21
|
+
|
|
22
|
+
mergeObjectContext<T extends AggregateRoot<any>>(aggregate: T): T {
|
|
23
|
+
const eventBus = this.eventBus;
|
|
24
|
+
aggregate.publish = event => eventBus.publish(event);
|
|
25
|
+
aggregate.publishAll = events => eventBus.publishAll(events);
|
|
26
|
+
return aggregate;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
mergeClassContext<T extends AggregateClass<AggregateRoot<any>>>(type: T): T {
|
|
30
|
+
const eventBus = this.eventBus;
|
|
31
|
+
const merged = class extends type {
|
|
32
|
+
override publish(event: object): Promise<void> {
|
|
33
|
+
return eventBus.publish(event);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
override publishAll(events: object[]): Promise<void> {
|
|
37
|
+
return eventBus.publishAll(events);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
Object.defineProperty(merged, 'name', { value: type.name });
|
|
41
|
+
return merged;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { AutowiredProvider, Component, type Provider } from '@celljs/core';
|
|
2
|
+
import { CqrsUtil, METADATA_KEY, type MessageType } from '../utils';
|
|
3
|
+
import { COMMAND_HANDLER, CommandBus, type CommandHandler, type CommandMessage } from './command-protocol';
|
|
4
|
+
|
|
5
|
+
@Component(CommandBus)
|
|
6
|
+
export class CommandBusImpl implements CommandBus {
|
|
7
|
+
/** A provider: the handlers are created when the first command runs, so they can inject the buses. */
|
|
8
|
+
@AutowiredProvider(COMMAND_HANDLER)
|
|
9
|
+
protected readonly handlerProvider: Provider<CommandHandler>;
|
|
10
|
+
|
|
11
|
+
protected handlers?: Map<MessageType, CommandHandler>;
|
|
12
|
+
|
|
13
|
+
execute<R>(command: CommandMessage<R>): Promise<R>;
|
|
14
|
+
execute<R = any>(command: object): Promise<R>;
|
|
15
|
+
async execute(command: object): Promise<any> {
|
|
16
|
+
return this.getHandler(command).execute(command);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
protected getHandler(command: object): CommandHandler {
|
|
20
|
+
const type = CqrsUtil.typeOf(command, 'command');
|
|
21
|
+
this.handlers ??= CqrsUtil.single(this.handlerProvider.get(), METADATA_KEY.commandHandler, 'command');
|
|
22
|
+
const handler = this.handlers.get(type);
|
|
23
|
+
if (!handler) {
|
|
24
|
+
throw CqrsUtil.noHandler(type, 'command');
|
|
25
|
+
}
|
|
26
|
+
return handler;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { ComponentOption, MaybePromise, Newable } from '@celljs/core';
|
|
2
|
+
import { CqrsUtil, METADATA_KEY } from '../utils';
|
|
3
|
+
|
|
4
|
+
export const CommandBus = Symbol('CommandBus');
|
|
5
|
+
export const COMMAND_HANDLER = Symbol('CommandHandler');
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Optional base class of a command that gives `CommandBus.execute` its result type:
|
|
9
|
+
* `class CreateUser extends CommandMessage<string> { ... }` makes `execute(new CreateUser(...))` a `Promise<string>`.
|
|
10
|
+
*/
|
|
11
|
+
export abstract class CommandMessage<R = void> {
|
|
12
|
+
/** Carries the result type only; never set. */
|
|
13
|
+
declare readonly __result: R;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type CommandResult<C> = C extends CommandMessage<infer R> ? R : any;
|
|
17
|
+
|
|
18
|
+
/** Changes the state of the application; the one handler of a command class is selected by the class of the command. */
|
|
19
|
+
export interface CommandHandler<C extends object = any, R = CommandResult<C>> {
|
|
20
|
+
execute(command: C): MaybePromise<R>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Makes the class a component that handles the commands of the given class.
|
|
25
|
+
* Each command class has exactly one handler.
|
|
26
|
+
*/
|
|
27
|
+
export function CommandHandler(command: Newable<object>, option?: ComponentOption): ClassDecorator {
|
|
28
|
+
return target => CqrsUtil.applyHandlerDecorator(target, COMMAND_HANDLER, METADATA_KEY.commandHandler, [command], option);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Runs a command with its handler. */
|
|
32
|
+
export interface CommandBus {
|
|
33
|
+
/**
|
|
34
|
+
* Runs the handler of the command and resolves to what it returns.
|
|
35
|
+
* Rejects with what the handler throws, or a `CqrsError` when no handler handles the command.
|
|
36
|
+
*/
|
|
37
|
+
execute<R>(command: CommandMessage<R>): Promise<R>;
|
|
38
|
+
execute<R = any>(command: object): Promise<R>;
|
|
39
|
+
}
|