node-cqrs 0.16.3 → 0.17.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.
Files changed (135) hide show
  1. package/CHANGELOG.md +53 -0
  2. package/README.md +2 -1
  3. package/dist/AbstractAggregate.js +178 -0
  4. package/dist/AbstractAggregate.js.map +1 -0
  5. package/dist/AbstractProjection.js +121 -0
  6. package/dist/AbstractProjection.js.map +1 -0
  7. package/dist/AbstractSaga.js +99 -0
  8. package/dist/AbstractSaga.js.map +1 -0
  9. package/dist/AggregateCommandHandler.js +85 -0
  10. package/dist/AggregateCommandHandler.js.map +1 -0
  11. package/dist/CommandBus.js +77 -0
  12. package/dist/CommandBus.js.map +1 -0
  13. package/dist/CqrsContainerBuilder.js +77 -0
  14. package/dist/CqrsContainerBuilder.js.map +1 -0
  15. package/dist/Event.js +43 -0
  16. package/dist/Event.js.map +1 -0
  17. package/dist/EventStore.js +229 -0
  18. package/dist/EventStore.js.map +1 -0
  19. package/dist/SagaEventHandler.js +117 -0
  20. package/dist/SagaEventHandler.js.map +1 -0
  21. package/dist/index.js +39 -0
  22. package/dist/index.js.map +1 -0
  23. package/dist/infrastructure/InMemoryEventStorage.js +53 -0
  24. package/dist/infrastructure/InMemoryEventStorage.js.map +1 -0
  25. package/dist/infrastructure/InMemoryLock.js +68 -0
  26. package/dist/infrastructure/InMemoryLock.js.map +1 -0
  27. package/dist/infrastructure/InMemoryMessageBus.js +95 -0
  28. package/dist/infrastructure/InMemoryMessageBus.js.map +1 -0
  29. package/dist/infrastructure/InMemorySnapshotStorage.js +26 -0
  30. package/dist/infrastructure/InMemorySnapshotStorage.js.map +1 -0
  31. package/dist/infrastructure/InMemoryView.js +173 -0
  32. package/dist/infrastructure/InMemoryView.js.map +1 -0
  33. package/dist/infrastructure/utils/Deferred.js +38 -0
  34. package/dist/infrastructure/utils/Deferred.js.map +1 -0
  35. package/dist/infrastructure/utils/index.js +19 -0
  36. package/dist/infrastructure/utils/index.js.map +1 -0
  37. package/dist/infrastructure/utils/nextCycle.js +9 -0
  38. package/dist/infrastructure/utils/nextCycle.js.map +1 -0
  39. package/dist/interfaces.js +4 -0
  40. package/dist/interfaces.js.map +1 -0
  41. package/dist/utils/getClassName.js +10 -0
  42. package/dist/utils/getClassName.js.map +1 -0
  43. package/dist/utils/getHandledMessageTypes.js +18 -0
  44. package/dist/utils/getHandledMessageTypes.js.map +1 -0
  45. package/dist/utils/getHandler.js +20 -0
  46. package/dist/utils/getHandler.js.map +1 -0
  47. package/dist/utils/getMessageHandlerNames.js +38 -0
  48. package/dist/utils/getMessageHandlerNames.js.map +1 -0
  49. package/dist/utils/index.js +25 -0
  50. package/dist/utils/index.js.map +1 -0
  51. package/dist/utils/isClass.js +8 -0
  52. package/dist/utils/isClass.js.map +1 -0
  53. package/dist/utils/setupOneTimeEmitterSubscription.js +46 -0
  54. package/dist/utils/setupOneTimeEmitterSubscription.js.map +1 -0
  55. package/dist/utils/subscribe.js +39 -0
  56. package/dist/utils/subscribe.js.map +1 -0
  57. package/dist/utils/validateHandlers.js +21 -0
  58. package/dist/utils/validateHandlers.js.map +1 -0
  59. package/package.json +26 -17
  60. package/src/AbstractAggregate.ts +223 -0
  61. package/src/AbstractProjection.ts +172 -0
  62. package/src/AbstractSaga.ts +118 -0
  63. package/src/AggregateCommandHandler.ts +129 -0
  64. package/src/CommandBus.ts +98 -0
  65. package/src/CqrsContainerBuilder.ts +120 -0
  66. package/src/Event.ts +43 -0
  67. package/src/EventStore.ts +315 -0
  68. package/src/SagaEventHandler.ts +161 -0
  69. package/src/index.ts +26 -0
  70. package/src/infrastructure/InMemoryEventStorage.ts +68 -0
  71. package/src/infrastructure/InMemoryLock.ts +73 -0
  72. package/src/infrastructure/InMemoryMessageBus.ts +118 -0
  73. package/src/infrastructure/InMemorySnapshotStorage.ts +27 -0
  74. package/src/infrastructure/InMemoryView.ts +221 -0
  75. package/src/infrastructure/utils/Deferred.ts +41 -0
  76. package/src/infrastructure/utils/index.ts +2 -0
  77. package/src/infrastructure/utils/nextCycle.ts +4 -0
  78. package/src/interfaces.ts +328 -0
  79. package/src/utils/getClassName.ts +6 -0
  80. package/src/utils/{getHandledMessageTypes.js → getHandledMessageTypes.ts} +4 -8
  81. package/src/utils/{getHandler.js → getHandler.ts} +6 -7
  82. package/src/utils/{getMessageHandlerNames.js → getMessageHandlerNames.ts} +2 -9
  83. package/src/utils/index.ts +8 -0
  84. package/src/utils/{isClass.js → isClass.ts} +2 -4
  85. package/src/utils/setupOneTimeEmitterSubscription.ts +57 -0
  86. package/src/{subscribe.js → utils/subscribe.ts} +21 -18
  87. package/src/utils/{validateHandlers.js → validateHandlers.ts} +2 -8
  88. package/index.d.ts +0 -43
  89. package/index.js +0 -3
  90. package/jsconfig.json +0 -15
  91. package/src/AbstractAggregate.js +0 -277
  92. package/src/AbstractProjection.js +0 -192
  93. package/src/AbstractSaga.js +0 -171
  94. package/src/AggregateCommandHandler.js +0 -126
  95. package/src/CommandBus.js +0 -91
  96. package/src/CqrsContainerBuilder.js +0 -134
  97. package/src/EventStore.js +0 -457
  98. package/src/EventStream.js +0 -63
  99. package/src/SagaEventHandler.js +0 -141
  100. package/src/index.js +0 -21
  101. package/src/infrastructure/InMemoryEventStorage.js +0 -76
  102. package/src/infrastructure/InMemoryMessageBus.js +0 -132
  103. package/src/infrastructure/InMemorySnapshotStorage.js +0 -40
  104. package/src/infrastructure/InMemoryView.js +0 -265
  105. package/src/utils/getClassName.js +0 -11
  106. package/src/utils/index.js +0 -6
  107. package/src/utils/nullLogger.js +0 -8
  108. package/types/classes/AbstractAggregate.d.ts +0 -64
  109. package/types/classes/AbstractProjection.d.ts +0 -46
  110. package/types/classes/AbstractSaga.d.ts +0 -39
  111. package/types/classes/AggregateCommandHandler.d.ts +0 -21
  112. package/types/classes/CommandBus.d.ts +0 -17
  113. package/types/classes/CqrsContainerBuilder.d.ts +0 -26
  114. package/types/classes/EventStore.d.ts +0 -53
  115. package/types/classes/EventStream.d.ts +0 -18
  116. package/types/classes/InMemoryEventStorage.d.ts +0 -21
  117. package/types/classes/InMemoryMessageBus.d.ts +0 -30
  118. package/types/classes/InMemorySnapshotStorage.d.ts +0 -18
  119. package/types/classes/InMemoryView.d.ts +0 -57
  120. package/types/classes/SagaEventHandler.d.ts +0 -20
  121. package/types/interfaces/IAggregate.d.ts +0 -30
  122. package/types/interfaces/IAggregateSnapshotStorage.d.ts +0 -4
  123. package/types/interfaces/ICommandBus.d.ts +0 -6
  124. package/types/interfaces/ICommandHandler.d.ts +0 -3
  125. package/types/interfaces/IConcurrentView.d.ts +0 -22
  126. package/types/interfaces/IEventReceptor.d.ts +0 -3
  127. package/types/interfaces/IEventStorage.d.ts +0 -20
  128. package/types/interfaces/IEventStore.d.ts +0 -19
  129. package/types/interfaces/IEventStream.d.ts +0 -13
  130. package/types/interfaces/ILogger.d.ts +0 -3
  131. package/types/interfaces/IMessageBus.d.ts +0 -5
  132. package/types/interfaces/IObserver.d.ts +0 -11
  133. package/types/interfaces/IProjection.d.ts +0 -10
  134. package/types/interfaces/ISaga.d.ts +0 -27
  135. package/types/interfaces/Identifier.d.ts +0 -1
@@ -1,76 +0,0 @@
1
- /* eslint no-return-assign: "off", eqeqeq: "off", require-jsdoc: "off" */
2
- 'use strict';
3
-
4
- /**
5
- * A simple event storage implementation intended to use for tests only.
6
- * Storage content resets on each app restart.
7
- *
8
- * @class InMemoryEventStorage
9
- * @implements {IEventStorage}
10
- */
11
- class InMemoryEventStorage {
12
-
13
- constructor() {
14
- this._nextId = 0;
15
- this._events = Promise.resolve([]);
16
- }
17
-
18
- /**
19
- * @param {IEvent[]} events
20
- * @returns {Promise<void>}
21
- */
22
- commitEvents(events) {
23
- return this._events = this._events.then(data =>
24
- data.concat(events));
25
- }
26
-
27
- /**
28
- * @param {Identifier} aggregateId
29
- * @param {object} [options]
30
- * @param {IEvent} [options.snapshot]
31
- * @returns {Promise<IEventStream>}
32
- */
33
- async getAggregateEvents(aggregateId, { snapshot } = {}) {
34
- const events = await this._events;
35
-
36
- if (snapshot)
37
- return events.filter(e => e.aggregateId == aggregateId && e.aggregateVersion > snapshot.aggregateVersion);
38
-
39
- return events.filter(e => e.aggregateId == aggregateId);
40
- }
41
-
42
- /**
43
- * @param {Identifier} sagaId
44
- * @param {object} [options]
45
- * @param {IEvent} [options.beforeEvent]
46
- * @returns {Promise<IEventStream>}
47
- */
48
- getSagaEvents(sagaId, { beforeEvent }) {
49
- return this._events.then(events =>
50
- events.filter(e =>
51
- e.sagaId == sagaId
52
- && e.sagaVersion < beforeEvent.sagaVersion));
53
- }
54
-
55
- /**
56
- * @param {string[]} eventTypes
57
- * @returns {Promise<IEventStream>}
58
- */
59
- getEvents(eventTypes) {
60
- if (!eventTypes)
61
- return this._events;
62
-
63
- return this._events.then(events =>
64
- events.filter(e => eventTypes.includes(e.type)));
65
- }
66
-
67
- /**
68
- * @returns {number}
69
- */
70
- getNewId() {
71
- this._nextId += 1;
72
- return this._nextId;
73
- }
74
- }
75
-
76
- module.exports = InMemoryEventStorage;
@@ -1,132 +0,0 @@
1
- 'use strict';
2
-
3
- /**
4
- * Default implementation of the message bus. Keeps all subscriptions and messages in memory.
5
- *
6
- * @class InMemoryMessageBus
7
- * @implements {IMessageBus}
8
- */
9
- class InMemoryMessageBus {
10
-
11
- /**
12
- * Indicates that message bus supports named queue subscriptions
13
- *
14
- * @type {boolean}
15
- * @readonly
16
- * @static
17
- */
18
- static get supportsQueues() {
19
- return true;
20
- }
21
-
22
- /**
23
- * Creates an instance of InMemoryMessageBus
24
- * @param {object} [options]
25
- * @param {string} [options.name]
26
- * @param {boolean} [options.uniqueEventHandlers]
27
- */
28
- constructor({ name, uniqueEventHandlers = !!name } = {}) {
29
- /** @type {Map<string, Set<IMessageHandler>>} */
30
- this._handlers = new Map();
31
-
32
- this._name = name;
33
- this._uniqueEventHandlers = uniqueEventHandlers;
34
-
35
- /** @type {Map<string, InMemoryMessageBus>} */
36
- this._queues = new Map();
37
- }
38
-
39
- /**
40
- * Subscribe to message type
41
- *
42
- * @param {string} messageType
43
- * @param {IMessageHandler} handler
44
- */
45
- on(messageType, handler) {
46
- if (typeof messageType !== 'string' || !messageType.length) throw new TypeError('messageType argument must be a non-empty String');
47
- if (typeof handler !== 'function') throw new TypeError('handler argument must be a Function');
48
- if (arguments.length !== 2) throw new TypeError(`2 arguments are expected, but ${arguments.length} received`);
49
-
50
- // Events published to a named queue must be consumed only once.
51
- // For example, for sending a welcome email, NotificationReceptor will subscribe to "notifications:userCreated".
52
- // Since we use an in-memory bus, there is no need to track message handling by multiple distributed subscribers,
53
- // and we only need to make sure that no more than 1 such subscriber will be created
54
- if (!this._handlers.has(messageType))
55
- this._handlers.set(messageType, new Set());
56
- else if (this._uniqueEventHandlers)
57
- throw new Error(`"${messageType}" handler is already set up on the "${this._name}" queue`);
58
-
59
- this._handlers.get(messageType).add(handler);
60
- }
61
-
62
- /**
63
- * Get or create a named queue.
64
- * Named queues support only one handler per event type.
65
- *
66
- * @param {string} name
67
- * @returns {IObservable}
68
- */
69
- queue(name) {
70
- if (!this._queues.has(name))
71
- this._queues.set(name, new InMemoryMessageBus({ name, uniqueEventHandlers: true }));
72
-
73
- return this._queues.get(name);
74
- }
75
-
76
- /**
77
- * Remove subscription
78
- *
79
- * @param {string} messageType
80
- * @param {IMessageHandler} handler
81
- */
82
- off(messageType, handler) {
83
- if (typeof messageType !== 'string' || !messageType.length) throw new TypeError('messageType argument must be a non-empty String');
84
- if (typeof handler !== 'function') throw new TypeError('handler argument must be a Function');
85
- if (arguments.length !== 2) throw new TypeError(`2 arguments are expected, but ${arguments.length} received`);
86
- if (!this._handlers.has(messageType)) throw new Error(`No ${messageType} subscribers found`);
87
-
88
- this._handlers.get(messageType).delete(handler);
89
- }
90
-
91
- /**
92
- * Send command to exactly 1 command handler
93
- *
94
- * @param {ICommand} command
95
- * @returns {Promise<any>}
96
- */
97
- async send(command) {
98
- if (typeof command !== 'object' || !command) throw new TypeError('command argument must be an Object');
99
- if (typeof command.type !== 'string' || !command.type.length) throw new TypeError('command.type argument must be a non-empty String');
100
-
101
- const handlers = this._handlers.get(command.type);
102
- if (!handlers || !handlers.size)
103
- throw new Error(`No '${command.type}' subscribers found`);
104
- if (handlers.size > 1)
105
- throw new Error(`More than one '${command.type}' subscriber found`);
106
-
107
- const commandHandler = handlers.values().next().value;
108
-
109
- return commandHandler(command);
110
- }
111
-
112
- /**
113
- * Publish event to all subscribers (if any)
114
- *
115
- * @param {IEvent} event
116
- * @returns {Promise<any>}
117
- */
118
- async publish(event) {
119
- if (typeof event !== 'object' || !event) throw new TypeError('event argument must be an Object');
120
- if (typeof event.type !== 'string' || !event.type.length) throw new TypeError('event.type argument must be a non-empty String');
121
-
122
- const handlers = [
123
- ...this._handlers.get(event.type) || [],
124
- ...Array.from(this._queues.values()).map(namedQueue =>
125
- e => namedQueue.publish(e))
126
- ];
127
-
128
- return Promise.all(handlers.map(handler => handler(event)));
129
- }
130
- }
131
-
132
- module.exports = InMemoryMessageBus;
@@ -1,40 +0,0 @@
1
- 'use strict';
2
-
3
- /**
4
- * In-memory storage for aggregate snapshots.
5
- * Storage content resets on app restart
6
- *
7
- * @class InMemorySnapshotStorage
8
- * @implements {IAggregateSnapshotStorage}
9
- */
10
- class InMemorySnapshotStorage {
11
-
12
- /**
13
- * Creates an instance of InMemorySnapshotStorage
14
- */
15
- constructor() {
16
- /** @type {Map<Identifier, IEvent>} */
17
- this._snapshots = new Map();
18
- }
19
-
20
- /**
21
- * Get latest aggregate snapshot
22
- *
23
- * @param {Identifier} aggregateId
24
- * @returns {Promise<IEvent>}
25
- */
26
- async getAggregateSnapshot(aggregateId) {
27
- return this._snapshots.get(aggregateId);
28
- }
29
-
30
- /**
31
- * Save new aggregate snapshot
32
- *
33
- * @param {IEvent} snapshotEvent
34
- */
35
- async saveAggregateSnapshot(snapshotEvent) {
36
- this._snapshots.set(snapshotEvent.aggregateId, snapshotEvent);
37
- }
38
- }
39
-
40
- module.exports = InMemorySnapshotStorage;
@@ -1,265 +0,0 @@
1
- 'use strict';
2
-
3
- const nextCycle = () => new Promise(setImmediate);
4
-
5
- /**
6
- * Update given value with an update Cb and return updated value.
7
- * Wrapper is needed for backward compatibility with update methods that were modifying the passed in objects directly
8
- *
9
- * @template TObjectValue
10
- * @param {TObjectValue} view
11
- * @param {function (TObjectValue): TObjectValue} update
12
- * @returns {TObjectValue}
13
- */
14
- const applyUpdate = (view, update) => {
15
- const valueReturnedByUpdate = update(view);
16
- return valueReturnedByUpdate === undefined ?
17
- view :
18
- valueReturnedByUpdate;
19
- };
20
-
21
- /**
22
- * In-memory Projection View, which suspends get()'s until it is ready
23
- *
24
- * @class InMemoryView
25
- * @template TRecord
26
- */
27
- class InMemoryView {
28
-
29
- /**
30
- * Whether the view is restored
31
- *
32
- * @type {boolean}
33
- */
34
- get ready() {
35
- return this._ready;
36
- }
37
-
38
- /**
39
- * Number of records in the View
40
- *
41
- * @type {number}
42
- * @readonly
43
- */
44
- get size() {
45
- return this._map.size;
46
- }
47
-
48
- /**
49
- * Creates an instance of InMemoryView
50
- */
51
- constructor() {
52
- /** @type {Map<Identifier, TRecord>} */
53
- this._map = new Map();
54
-
55
- // explicitly bind the `get` method to this object for easier using in Promises
56
- Object.defineProperty(this, this.get.name, {
57
- value: this.get.bind(this)
58
- });
59
- }
60
-
61
- /**
62
- * Lock the view to prevent concurrent modifications
63
- *
64
- * @returns {Promise<void>}
65
- */
66
- async lock() {
67
- if (this.ready === false)
68
- await this.once('ready');
69
-
70
- this._lockPromise = new Promise(resolve => {
71
- this._unlock = resolve;
72
- });
73
-
74
- this._ready = false;
75
- }
76
-
77
- /**
78
- * Release the lock
79
- */
80
- unlock() {
81
- this._ready = true;
82
- if (typeof this._unlock === 'function')
83
- this._unlock();
84
- }
85
-
86
- /**
87
- * Check if view contains a record with a given key.
88
- * This is the only synchronous method, so make sure to check the `ready` flag, if necessary
89
- *
90
- * @deprecated Use `async get()` instead
91
- *
92
- * @param {Identifier} key
93
- * @returns {boolean}
94
- */
95
- has(key) {
96
- return this._map.has(key);
97
- }
98
-
99
- /**
100
- * Get record with a given key; await until the view is restored
101
- *
102
- * @param {Identifier} key
103
- * @param {object} [options]
104
- * @param {boolean} [options.nowait] Skip waiting until the view is restored/ready
105
- * @returns {Promise<TRecord>}
106
- */
107
- async get(key, options) {
108
- if (!key) throw new TypeError('key argument required');
109
-
110
- if (!this._ready && !(options && options.nowait))
111
- await this.once('ready');
112
-
113
- await nextCycle();
114
-
115
- return this._map.get(key);
116
- }
117
-
118
- /**
119
- * Get all records matching an optional filter
120
- *
121
- * @param {function(TRecord, Identifier): boolean} [filter]
122
- */
123
- async getAll(filter) {
124
- if (filter && typeof filter !== 'function')
125
- throw new TypeError('filter argument, when defined, must be a Function');
126
-
127
- if (!this._ready)
128
- await this.once('ready');
129
-
130
- await nextCycle();
131
-
132
- const r = [];
133
- for (const entry of this._map.entries()) {
134
- if (!filter || filter(entry[1], entry[0]))
135
- r.push(entry);
136
- }
137
-
138
- return r;
139
- }
140
-
141
- /**
142
- * Create record with a given key and value
143
- *
144
- * @param {Identifier} key
145
- * @param {TRecord} [value]
146
- */
147
- create(key, value = {}) {
148
- if (!key) throw new TypeError('key argument required');
149
- if (typeof value === 'function') throw new TypeError('value argument must be an instance of an Object');
150
-
151
- if (this._map.has(key))
152
- throw new Error(`Key '${key}' already exists`);
153
-
154
- this._map.set(key, value);
155
- }
156
-
157
- /**
158
- * Update existing view record
159
- *
160
- * @param {Identifier} key
161
- * @param {function(TRecord): TRecord} update
162
- */
163
- update(key, update) {
164
- if (!key) throw new TypeError('key argument required');
165
- if (typeof update !== 'function') throw new TypeError('update argument must be a Function');
166
-
167
- if (!this._map.has(key))
168
- throw new Error(`Key '${key}' does not exist`);
169
-
170
- this._update(key, update);
171
- }
172
-
173
- /**
174
- * Update existing view record or create new
175
- *
176
- * @param {Identifier} key
177
- * @param {function(TRecord): TRecord} update
178
- */
179
- updateEnforcingNew(key, update) {
180
- if (!key) throw new TypeError('key argument required');
181
- if (typeof update !== 'function') throw new TypeError('update argument must be a Function');
182
-
183
- if (!this._map.has(key))
184
- return this.create(key, applyUpdate(undefined, update));
185
-
186
- return this._update(key, update);
187
- }
188
-
189
- /**
190
- * Update all records that match filter criteria
191
- *
192
- * @param {function(TRecord): boolean} [filter]
193
- * @param {function(TRecord): TRecord} update
194
- */
195
- updateAll(filter, update) {
196
- if (filter && typeof filter !== 'function') throw new TypeError('filter argument, when specified, must be a Function');
197
- if (typeof update !== 'function') throw new TypeError('update argument must be a Function');
198
-
199
- for (const [key, value] of this._map) {
200
- if (!filter || filter(value))
201
- this._update(key, update);
202
- }
203
- }
204
-
205
- /**
206
- * Update existing record
207
- *
208
- * @private
209
- * @param {Identifier} key
210
- * @param {function(TRecord): TRecord} update
211
- */
212
- _update(key, update) {
213
- const value = this._map.get(key);
214
- this._map.set(key, applyUpdate(value, update));
215
- }
216
-
217
- /**
218
- * Delete record
219
- *
220
- * @param {Identifier} key
221
- */
222
- delete(key) {
223
- if (!key) throw new TypeError('key argument required');
224
-
225
- this._map.delete(key);
226
- }
227
-
228
- /**
229
- * Delete all records that match filter criteria
230
- *
231
- * @param {function(TRecord): boolean} [filter]
232
- */
233
- deleteAll(filter) {
234
- if (filter && typeof filter !== 'function') throw new TypeError('filter argument, when specified, must be a Function');
235
-
236
- for (const [key, value] of this._map) {
237
- if (!filter || filter(value))
238
- this._map.delete(key);
239
- }
240
- }
241
-
242
- /**
243
- * Create a Promise which will resolve to a first emitted event of a given type
244
- *
245
- * @param {"ready"} eventType
246
- * @returns {Promise<any>}
247
- */
248
- once(eventType) {
249
- if (eventType !== 'ready')
250
- throw new TypeError(`Unexpected event type: ${eventType}`);
251
-
252
- return this._lockPromise;
253
- }
254
-
255
- /**
256
- * Get view summary as string
257
- *
258
- * @returns {string}
259
- */
260
- toString() {
261
- return `${this.size} record${this.size !== 1 ? 's' : ''}`;
262
- }
263
- }
264
-
265
- module.exports = InMemoryView;
@@ -1,11 +0,0 @@
1
- 'use strict';
2
-
3
- /**
4
- * Get instance class name
5
- *
6
- * @param {object} instance
7
- * @returns {string}
8
- */
9
- module.exports = function getClassName(instance) {
10
- return Object.getPrototypeOf(instance).constructor.name;
11
- };
@@ -1,6 +0,0 @@
1
- 'use strict';
2
-
3
- exports.getClassName = require('./getClassName');
4
- exports.getHandler = require('./getHandler');
5
- exports.isClass = require('./isClass');
6
- exports.validateHandlers = require('./validateHandlers');
@@ -1,8 +0,0 @@
1
- 'use strict';
2
-
3
- /**
4
- * @type {ILogger}
5
- */
6
- module.exports = {
7
- log() { }
8
- };
@@ -1,64 +0,0 @@
1
- namespace NodeCqrs {
2
-
3
- /** Base class for Aggregate definition */
4
- declare abstract class AbstractAggregate implements IAggregate {
5
-
6
- /**
7
- * Optional list of commands handled by Aggregate.
8
- * Can be overridden in the aggregate implementation
9
- */
10
- static readonly handles: Array<string>;
11
-
12
- /** Aggregate ID */
13
- readonly id: string | number;
14
-
15
- /** Aggregate Version */
16
- readonly version: number;
17
-
18
- /** Aggregate Snapshot Version */
19
- readonly snapshotVersion: number | undefined;
20
-
21
- /** Events emitted by Aggregate */
22
- readonly changes: IEventStream;
23
-
24
- /** Override to define whether an aggregate state snapshot should be taken */
25
- readonly shouldTakeSnapshot: boolean;
26
-
27
- state;
28
-
29
- command;
30
-
31
- /** Creates an instance of AbstractAggregate. */
32
- constructor(options: TAggregateConstructorParams): void;
33
-
34
- /** Pass command to command handler */
35
- handle(command: ICommand): any;
36
-
37
- /** Mutate aggregate state and increment aggregate version */
38
- protected mutate(event: IEvent): void;
39
-
40
- /** Format and register aggregate event and mutate aggregate state */
41
- protected emit(type: string, payload?: object): void;
42
-
43
- /**
44
- * Format event based on a current aggregate state
45
- * and a command being executed
46
- */
47
- protected makeEvent(type: string, payload?: any, sourceCommand?: ICommand): IEvent;
48
-
49
- /** Register aggregate event and mutate aggregate state */
50
- protected emitRaw(event: IEvent): void;
51
-
52
- /** Take an aggregate state snapshot and add it to the changes queue */
53
- takeSnapshot(): void;
54
-
55
- /** Create an aggregate state snapshot */
56
- protected makeSnapshot(): object;
57
-
58
- /** Restore aggregate state from a snapshot */
59
- protected restoreSnapshot(snapshotEvent: IEvent): void;
60
-
61
- /** Get human-readable aggregate identifier */
62
- toString(): string;
63
- }
64
- }
@@ -1,46 +0,0 @@
1
- namespace NodeCqrs {
2
-
3
- /** Base class for Projection definition */
4
- declare abstract class AbstractProjection implements IProjection {
5
-
6
- /**
7
- * Optional list of event types being handled by projection.
8
- * Can be overridden in projection implementation.
9
- * If not overridden, will detect event types from event handlers declared on the Projection class
10
- */
11
- static readonly handles: Array<string>;
12
-
13
- /**
14
- * Default view associated with projection.
15
- * If not defined, an instance of `NodeCqrs.InMemoryView` is created on first access.
16
- */
17
- readonly view;
18
-
19
- /**
20
- * Indicates if view should be restored from EventStore on start.
21
- * Override for custom behavior.
22
- */
23
- readonly shouldRestoreView: boolean | Promise<boolean>;
24
-
25
- /** Creates an instance of AbstractProjection */
26
- constructor(options?: { view?: any, logger?: ILogger }): void;
27
-
28
- /** Subscribe to event store */
29
- subscribe(eventStore: IEventStore): Promise<void>;
30
-
31
- /** Pass event to projection event handler */
32
- project(event: IEvent): Promise<void>;
33
-
34
- /** Pass event to projection event handler, without awaiting for restore operation to complete */
35
- protected _project(event: IEvent): Promise<void>;
36
-
37
- /** Restore projection view from event store */
38
- restore(eventStore: IEventStore): Promise<void>;
39
-
40
- /** Restore projection view from event store */
41
- protected _restore(eventStore: IEventStore): Promise<void>;
42
-
43
- /** Handle error on restoring */
44
- protected _onRestoringError(error: Error, event: IEvent): void;
45
- }
46
- }
@@ -1,39 +0,0 @@
1
- namespace NodeCqrs {
2
-
3
- /** Base class for Saga definition */
4
- declare abstract class AbstractSaga implements ISaga {
5
-
6
- /** List of events that start new saga, must be overridden in Saga implementation */
7
- static readonly startsWith: Array<string>;
8
-
9
- /** List of event types being handled by Saga, must be overridden in Saga implementation */
10
- static readonly handles: Array<string>;
11
-
12
- /** Saga ID */
13
- readonly id: string | number;
14
-
15
- /** Saga version */
16
- readonly version: number;
17
-
18
- /** Command execution queue */
19
- readonly uncommittedMessages: Array<ICommand>;
20
-
21
- /** Creates an instance of AbstractSaga */
22
- constructor(options: TSagaConstructorParams): void;
23
-
24
- /** Modify saga state by applying an event */
25
- apply(event: IEvent): void | Promise<void>;
26
-
27
- /** Format a command and put it to the execution queue */
28
- protected enqueue(commandType: string, aggregateId: string | number, payload: object): void;
29
-
30
- /** Put a command to the execution queue */
31
- protected enqueueRaw(command: ICommand): void;
32
-
33
- /** Clear the execution queue */
34
- resetUncommittedMessages(): void;
35
-
36
- /** Get human-readable Saga name */
37
- toString(): string;
38
- }
39
- }