@nestjs/cqrs 11.0.3 → 12.0.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/.circleci/config.yml +3 -3
- package/.nvmrc +1 -0
- package/dist/aggregate-root.d.ts +3 -67
- package/dist/aggregate-root.js +6 -83
- package/dist/classes/command.d.ts +2 -2
- package/dist/classes/command.js +2 -6
- package/dist/classes/constants.js +1 -4
- package/dist/classes/index.d.ts +2 -2
- package/dist/classes/index.js +2 -18
- package/dist/classes/query.d.ts +2 -2
- package/dist/classes/query.js +2 -6
- package/dist/command-bus.d.ts +5 -5
- package/dist/command-bus.js +29 -32
- package/dist/constants.js +1 -4
- package/dist/cqrs.module.d.ts +5 -5
- package/dist/cqrs.module.js +34 -37
- package/dist/decorators/command-handler.decorator.d.ts +1 -1
- package/dist/decorators/command-handler.decorator.js +9 -13
- package/dist/decorators/constants.js +7 -10
- package/dist/decorators/events-handler.decorator.d.ts +1 -1
- package/dist/decorators/events-handler.decorator.js +9 -13
- package/dist/decorators/index.d.ts +5 -5
- package/dist/decorators/index.js +5 -21
- package/dist/decorators/publishable.decorator.js +3 -6
- package/dist/decorators/query-handler.decorator.d.ts +1 -1
- package/dist/decorators/query-handler.decorator.js +9 -13
- package/dist/decorators/saga.decorator.js +5 -9
- package/dist/event-bus.d.ts +10 -8
- package/dist/event-bus.js +51 -51
- package/dist/event-publisher.d.ts +6 -7
- package/dist/event-publisher.js +7 -10
- package/dist/exceptions/command-not-found.exception.js +1 -5
- package/dist/exceptions/index.d.ts +7 -7
- package/dist/exceptions/index.js +7 -23
- package/dist/exceptions/invalid-command-handler.exception.js +1 -5
- package/dist/exceptions/invalid-events-handler.exception.js +1 -5
- package/dist/exceptions/invalid-query-handler.exception.js +1 -5
- package/dist/exceptions/invalid-saga.exception.js +1 -5
- package/dist/exceptions/query-not-found.exception.js +1 -5
- package/dist/exceptions/unsupported-saga-scope.exception.js +1 -5
- package/dist/helpers/default-command-pubsub.d.ts +1 -1
- package/dist/helpers/default-command-pubsub.js +1 -5
- package/dist/helpers/default-event-id-provider.d.ts +1 -1
- package/dist/helpers/default-event-id-provider.js +3 -6
- package/dist/helpers/default-pubsub.d.ts +1 -1
- package/dist/helpers/default-pubsub.js +1 -5
- package/dist/helpers/default-query-pubsub.d.ts +1 -1
- package/dist/helpers/default-query-pubsub.js +1 -5
- package/dist/helpers/default-unhandled-exception-pubsub.d.ts +1 -1
- package/dist/helpers/default-unhandled-exception-pubsub.js +1 -5
- package/dist/index.d.ts +15 -14
- package/dist/index.js +15 -30
- package/dist/interfaces/aggregate-root.interface.d.ts +53 -0
- package/dist/interfaces/aggregate-root.interface.js +1 -0
- package/dist/interfaces/commands/command-bus.interface.d.ts +3 -3
- package/dist/interfaces/commands/command-bus.interface.js +1 -2
- package/dist/interfaces/commands/command-handler.interface.d.ts +2 -2
- package/dist/interfaces/commands/command-handler.interface.js +1 -2
- package/dist/interfaces/commands/command-metadata.interface.js +1 -2
- package/dist/interfaces/commands/command-publisher.interface.d.ts +1 -1
- package/dist/interfaces/commands/command-publisher.interface.js +1 -2
- package/dist/interfaces/commands/command.interface.js +1 -2
- package/dist/interfaces/cqrs-module-options.interface.d.ts +5 -5
- package/dist/interfaces/cqrs-module-options.interface.js +1 -2
- package/dist/interfaces/events/event-bus.interface.d.ts +2 -2
- package/dist/interfaces/events/event-bus.interface.js +1 -2
- package/dist/interfaces/events/event-handler.interface.d.ts +1 -1
- package/dist/interfaces/events/event-handler.interface.js +1 -2
- package/dist/interfaces/events/event-id-provider.interface.d.ts +1 -1
- package/dist/interfaces/events/event-id-provider.interface.js +1 -2
- package/dist/interfaces/events/event-publisher.interface.d.ts +2 -2
- package/dist/interfaces/events/event-publisher.interface.js +1 -2
- package/dist/interfaces/events/event.interface.js +1 -2
- package/dist/interfaces/events/message-source.interface.d.ts +1 -1
- package/dist/interfaces/events/message-source.interface.js +1 -2
- package/dist/interfaces/exceptions/unhandled-exception-info.interface.d.ts +2 -2
- package/dist/interfaces/exceptions/unhandled-exception-info.interface.js +1 -2
- package/dist/interfaces/exceptions/unhandled-exception-publisher.interface.d.ts +3 -3
- package/dist/interfaces/exceptions/unhandled-exception-publisher.interface.js +1 -2
- package/dist/interfaces/index.d.ts +20 -19
- package/dist/interfaces/index.js +20 -35
- package/dist/interfaces/providers-introspection-result.interface.d.ts +4 -4
- package/dist/interfaces/providers-introspection-result.interface.js +1 -2
- package/dist/interfaces/queries/query-bus.interface.d.ts +3 -3
- package/dist/interfaces/queries/query-bus.interface.js +1 -2
- package/dist/interfaces/queries/query-handler.interface.d.ts +2 -2
- package/dist/interfaces/queries/query-handler.interface.js +1 -2
- package/dist/interfaces/queries/query-metadata.interface.js +1 -2
- package/dist/interfaces/queries/query-publisher.interface.d.ts +1 -1
- package/dist/interfaces/queries/query-publisher.interface.js +1 -2
- package/dist/interfaces/queries/query-result.interface.js +1 -2
- package/dist/interfaces/queries/query.interface.js +1 -2
- package/dist/interfaces/saga.type.d.ts +2 -2
- package/dist/interfaces/saga.type.js +1 -2
- package/dist/mixins/index.d.ts +1 -0
- package/dist/mixins/index.js +1 -0
- package/dist/mixins/with-aggregate-root.mixin.d.ts +11 -0
- package/dist/mixins/with-aggregate-root.mixin.js +67 -0
- package/dist/operators/index.d.ts +1 -1
- package/dist/operators/index.js +1 -17
- package/dist/operators/of-type.d.ts +1 -1
- package/dist/operators/of-type.js +3 -6
- package/dist/query-bus.d.ts +5 -5
- package/dist/query-bus.js +28 -31
- package/dist/scopes/async.context.js +9 -13
- package/dist/scopes/index.d.ts +1 -1
- package/dist/scopes/index.js +1 -17
- package/dist/services/explorer.service.d.ts +5 -5
- package/dist/services/explorer.service.js +14 -20
- package/dist/storages/aggregate-root.storage.d.ts +3 -3
- package/dist/storages/aggregate-root.storage.js +1 -5
- package/dist/unhandled-exception-bus.d.ts +2 -2
- package/dist/unhandled-exception-bus.js +14 -17
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/index.js +1 -17
- package/dist/utils/observable-bus.js +3 -7
- package/package.json +34 -26
- package/test/e2e/basic-flows.spec.ts +46 -26
- package/test/e2e/generics.spec.ts +3 -3
- package/test/e2e/request-scope.spec.ts +23 -22
- package/test/e2e/sync-event-handlers-app.spec.ts +123 -0
- package/test/e2e/unhandled-exceptions.spec.ts +36 -20
- package/tsconfig.build.json +3 -2
- package/vitest.config.e2e.ts +13 -0
- package/vitest.config.ts +8 -0
- package/dist/exceptions/invalid-module-ref.exception.d.ts +0 -3
- package/dist/exceptions/invalid-module-ref.exception.js +0 -8
- package/dist/helpers/default-get-event-id.d.ts +0 -9
- package/dist/helpers/default-get-event-id.js +0 -18
- package/dist/helpers/default-get-event-name.d.ts +0 -2
- package/dist/helpers/default-get-event-name.js +0 -8
- package/dist/interfaces/cqrs-options.interface.d.ts +0 -10
- package/dist/interfaces/cqrs-options.interface.js +0 -2
- package/dist/interfaces/event-observable.interface.d.ts +0 -4
- package/dist/interfaces/event-observable.interface.js +0 -2
- package/dist/utils/command-handler.decorator.d.ts +0 -3
- package/dist/utils/command-handler.decorator.js +0 -9
- package/dist/utils/constants.d.ts +0 -2
- package/dist/utils/constants.js +0 -4
- package/dist/utils/default-pubsub.d.ts +0 -9
- package/dist/utils/default-pubsub.js +0 -14
- package/dist/utils/events-handler.decorator.d.ts +0 -3
- package/dist/utils/events-handler.decorator.js +0 -9
- package/eslint.config.mjs +0 -42
- package/index.d.ts +0 -1
- package/index.js +0 -6
- package/jest.config.js +0 -9
- package/test/jest-e2e.json +0 -9
package/.circleci/config.yml
CHANGED
|
@@ -17,7 +17,7 @@ jobs:
|
|
|
17
17
|
build:
|
|
18
18
|
working_directory: ~/nest
|
|
19
19
|
docker:
|
|
20
|
-
- image: cimg/node:
|
|
20
|
+
- image: cimg/node:24.19.0
|
|
21
21
|
steps:
|
|
22
22
|
- checkout
|
|
23
23
|
- restore_cache:
|
|
@@ -35,7 +35,7 @@ jobs:
|
|
|
35
35
|
e2e_test:
|
|
36
36
|
working_directory: ~/nest
|
|
37
37
|
docker:
|
|
38
|
-
- image: cimg/node:
|
|
38
|
+
- image: cimg/node:24.19.0
|
|
39
39
|
steps:
|
|
40
40
|
- checkout
|
|
41
41
|
- *restore-cache
|
|
@@ -46,7 +46,7 @@ jobs:
|
|
|
46
46
|
test:
|
|
47
47
|
working_directory: ~/nest
|
|
48
48
|
docker:
|
|
49
|
-
- image: cimg/node:
|
|
49
|
+
- image: cimg/node:24.19.0
|
|
50
50
|
steps:
|
|
51
51
|
- checkout
|
|
52
52
|
- *restore-cache
|
package/.nvmrc
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
24
|
package/dist/aggregate-root.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { Type } from '@nestjs/common';
|
|
2
|
-
import { IEvent, IEventHandler } from './interfaces';
|
|
3
|
-
declare const
|
|
4
|
-
declare const IS_AUTO_COMMIT_ENABLED: unique symbol;
|
|
2
|
+
import { IEvent, IEventHandler } from './interfaces/index.js';
|
|
3
|
+
declare const AggregateRoot_base: abstract new <T extends IEvent = IEvent>(...args: any[]) => import("./interfaces/aggregate-root.interface.js").IAggregateRoot<T> & {};
|
|
5
4
|
/**
|
|
6
5
|
* Represents an aggregate root.
|
|
7
6
|
* An aggregate root is an entity that represents a meaningful concept in the domain.
|
|
@@ -9,70 +8,7 @@ declare const IS_AUTO_COMMIT_ENABLED: unique symbol;
|
|
|
9
8
|
*
|
|
10
9
|
* @template EventBase The base type of the events.
|
|
11
10
|
*/
|
|
12
|
-
export declare abstract class AggregateRoot<EventBase extends IEvent = IEvent> {
|
|
13
|
-
[IS_AUTO_COMMIT_ENABLED]: boolean;
|
|
14
|
-
private readonly [INTERNAL_EVENTS];
|
|
15
|
-
/**
|
|
16
|
-
* Sets whether the aggregate root should automatically commit events.
|
|
17
|
-
*/
|
|
18
|
-
set autoCommit(value: boolean);
|
|
19
|
-
/**
|
|
20
|
-
* Gets whether the aggregate root should automatically commit events.
|
|
21
|
-
*/
|
|
22
|
-
get autoCommit(): boolean;
|
|
23
|
-
/**
|
|
24
|
-
* Publishes an event. Must be merged with the publisher context in order to work.
|
|
25
|
-
* @param event The event to publish.
|
|
26
|
-
*/
|
|
27
|
-
publish<T extends EventBase = EventBase>(event: T): void;
|
|
28
|
-
/**
|
|
29
|
-
* Publishes multiple events. Must be merged with the publisher context in order to work.
|
|
30
|
-
* @param events The events to publish.
|
|
31
|
-
*/
|
|
32
|
-
publishAll<T extends EventBase = EventBase>(events: T[]): void;
|
|
33
|
-
/**
|
|
34
|
-
* Commits all uncommitted events.
|
|
35
|
-
*/
|
|
36
|
-
commit(): void;
|
|
37
|
-
/**
|
|
38
|
-
* Uncommits all events.
|
|
39
|
-
*/
|
|
40
|
-
uncommit(): void;
|
|
41
|
-
/**
|
|
42
|
-
* Returns all uncommitted events.
|
|
43
|
-
* @returns All uncommitted events.
|
|
44
|
-
*/
|
|
45
|
-
getUncommittedEvents(): EventBase[];
|
|
46
|
-
/**
|
|
47
|
-
* Loads events from history.
|
|
48
|
-
* @param history The history to load.
|
|
49
|
-
*/
|
|
50
|
-
loadFromHistory(history: EventBase[]): void;
|
|
51
|
-
/**
|
|
52
|
-
* Applies an event.
|
|
53
|
-
* If auto commit is enabled, the event will be published immediately (note: must be merged with the publisher context in order to work).
|
|
54
|
-
* Otherwise, the event will be stored in the internal events array, and will be published when the commit method is called.
|
|
55
|
-
* Also, the corresponding event handler will be called (if exists).
|
|
56
|
-
* For example, if the event is called UserCreatedEvent, the "onUserCreatedEvent" method will be called.
|
|
57
|
-
*
|
|
58
|
-
* @param event The event to apply.
|
|
59
|
-
* @param isFromHistory Whether the event is from history.
|
|
60
|
-
*/
|
|
61
|
-
apply<T extends EventBase = EventBase>(event: T, isFromHistory?: boolean): void;
|
|
62
|
-
/**
|
|
63
|
-
* Applies an event.
|
|
64
|
-
* If auto commit is enabled, the event will be published immediately (note: must be merged with the publisher context in order to work).
|
|
65
|
-
* Otherwise, the event will be stored in the internal events array, and will be published when the commit method is called.
|
|
66
|
-
* Also, the corresponding event handler will be called (if exists).
|
|
67
|
-
* For example, if the event is called UserCreatedEvent, the "onUserCreatedEvent" method will be called.
|
|
68
|
-
*
|
|
69
|
-
* @param event The event to apply.
|
|
70
|
-
* @param options The options.
|
|
71
|
-
*/
|
|
72
|
-
apply<T extends EventBase = EventBase>(event: T, options?: {
|
|
73
|
-
fromHistory?: boolean;
|
|
74
|
-
skipHandler?: boolean;
|
|
75
|
-
}): void;
|
|
11
|
+
export declare abstract class AggregateRoot<EventBase extends IEvent = IEvent> extends AggregateRoot_base<EventBase> {
|
|
76
12
|
protected getEventHandler<T extends EventBase = EventBase>(event: T): Type<IEventHandler> | undefined;
|
|
77
13
|
protected getEventName(event: any): string;
|
|
78
14
|
}
|
package/dist/aggregate-root.js
CHANGED
|
@@ -1,10 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
3
|
-
var _a, _b;
|
|
4
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
-
exports.AggregateRoot = void 0;
|
|
6
|
-
const INTERNAL_EVENTS = Symbol();
|
|
7
|
-
const IS_AUTO_COMMIT_ENABLED = Symbol();
|
|
1
|
+
import { WithAggregateRoot } from './mixins/index.js';
|
|
8
2
|
/**
|
|
9
3
|
* Represents an aggregate root.
|
|
10
4
|
* An aggregate root is an entity that represents a meaningful concept in the domain.
|
|
@@ -12,84 +6,13 @@ const IS_AUTO_COMMIT_ENABLED = Symbol();
|
|
|
12
6
|
*
|
|
13
7
|
* @template EventBase The base type of the events.
|
|
14
8
|
*/
|
|
15
|
-
class AggregateRoot {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
this[_b] = [];
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* Sets whether the aggregate root should automatically commit events.
|
|
22
|
-
*/
|
|
23
|
-
set autoCommit(value) {
|
|
24
|
-
this[IS_AUTO_COMMIT_ENABLED] = value;
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* Gets whether the aggregate root should automatically commit events.
|
|
28
|
-
*/
|
|
29
|
-
get autoCommit() {
|
|
30
|
-
return this[IS_AUTO_COMMIT_ENABLED];
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Publishes an event. Must be merged with the publisher context in order to work.
|
|
34
|
-
* @param event The event to publish.
|
|
35
|
-
*/
|
|
36
|
-
publish(event) { }
|
|
37
|
-
/**
|
|
38
|
-
* Publishes multiple events. Must be merged with the publisher context in order to work.
|
|
39
|
-
* @param events The events to publish.
|
|
40
|
-
*/
|
|
41
|
-
publishAll(events) { }
|
|
42
|
-
/**
|
|
43
|
-
* Commits all uncommitted events.
|
|
44
|
-
*/
|
|
45
|
-
commit() {
|
|
46
|
-
this.publishAll(this[INTERNAL_EVENTS]);
|
|
47
|
-
this[INTERNAL_EVENTS].length = 0;
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Uncommits all events.
|
|
51
|
-
*/
|
|
52
|
-
uncommit() {
|
|
53
|
-
this[INTERNAL_EVENTS].length = 0;
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* Returns all uncommitted events.
|
|
57
|
-
* @returns All uncommitted events.
|
|
58
|
-
*/
|
|
59
|
-
getUncommittedEvents() {
|
|
60
|
-
return this[INTERNAL_EVENTS];
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* Loads events from history.
|
|
64
|
-
* @param history The history to load.
|
|
65
|
-
*/
|
|
66
|
-
loadFromHistory(history) {
|
|
67
|
-
history.forEach((event) => this.apply(event, true));
|
|
68
|
-
}
|
|
69
|
-
apply(event, optionsOrIsFromHistory = {}) {
|
|
70
|
-
const isFromHistory = (typeof optionsOrIsFromHistory === 'boolean'
|
|
71
|
-
? optionsOrIsFromHistory
|
|
72
|
-
: optionsOrIsFromHistory.fromHistory) ?? false;
|
|
73
|
-
const skipHandler = typeof optionsOrIsFromHistory === 'boolean'
|
|
74
|
-
? false
|
|
75
|
-
: optionsOrIsFromHistory.skipHandler;
|
|
76
|
-
if (!isFromHistory && !this.autoCommit) {
|
|
77
|
-
this[INTERNAL_EVENTS].push(event);
|
|
78
|
-
}
|
|
79
|
-
this.autoCommit && this.publish(event);
|
|
80
|
-
if (!skipHandler) {
|
|
81
|
-
const handler = this.getEventHandler(event);
|
|
82
|
-
handler && handler.call(this, event);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
9
|
+
export class AggregateRoot extends WithAggregateRoot(class {
|
|
10
|
+
}) {
|
|
11
|
+
// redeclaring protected methods to preserve typings (since this typescript version does not support the "declare" modifier on overrides)
|
|
85
12
|
getEventHandler(event) {
|
|
86
|
-
|
|
87
|
-
return this[handler];
|
|
13
|
+
return super['getEventHandler'](event);
|
|
88
14
|
}
|
|
89
15
|
getEventName(event) {
|
|
90
|
-
|
|
91
|
-
return constructor.name;
|
|
16
|
+
return super['getEventName'](event);
|
|
92
17
|
}
|
|
93
18
|
}
|
|
94
|
-
exports.AggregateRoot = AggregateRoot;
|
|
95
|
-
_a = IS_AUTO_COMMIT_ENABLED, _b = INTERNAL_EVENTS;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { ICommand } from '../interfaces';
|
|
2
|
-
import { RESULT_TYPE_SYMBOL } from './constants';
|
|
1
|
+
import { ICommand } from '../interfaces/index.js';
|
|
2
|
+
import { RESULT_TYPE_SYMBOL } from './constants.js';
|
|
3
3
|
/**
|
|
4
4
|
* Utility type to extract the result type of a command.
|
|
5
5
|
*/
|
package/dist/classes/command.js
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.Command = void 0;
|
|
4
|
-
const constants_1 = require("./constants");
|
|
5
|
-
class Command {
|
|
1
|
+
import { RESULT_TYPE_SYMBOL } from './constants.js';
|
|
2
|
+
export class Command {
|
|
6
3
|
}
|
|
7
|
-
exports.Command = Command;
|
package/dist/classes/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from './command';
|
|
2
|
-
export * from './query';
|
|
1
|
+
export * from './command.js';
|
|
2
|
+
export * from './query.js';
|
package/dist/classes/index.js
CHANGED
|
@@ -1,18 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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("./command"), exports);
|
|
18
|
-
__exportStar(require("./query"), exports);
|
|
1
|
+
export * from './command.js';
|
|
2
|
+
export * from './query.js';
|
package/dist/classes/query.d.ts
CHANGED
package/dist/classes/query.js
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.Query = void 0;
|
|
4
|
-
const constants_1 = require("./constants");
|
|
5
|
-
class Query {
|
|
1
|
+
import { RESULT_TYPE_SYMBOL } from './constants.js';
|
|
2
|
+
export class Query {
|
|
6
3
|
}
|
|
7
|
-
exports.Query = Query;
|
package/dist/command-bus.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { Type } from '@nestjs/common';
|
|
2
2
|
import { ModuleRef } from '@nestjs/core';
|
|
3
|
-
import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper';
|
|
3
|
+
import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper.js';
|
|
4
4
|
import 'reflect-metadata';
|
|
5
|
-
import { Command } from './classes';
|
|
6
|
-
import { CqrsModuleOptions, ICommand, ICommandBus, ICommandHandler, ICommandPublisher } from './interfaces/index';
|
|
7
|
-
import { AsyncContext } from './scopes/async.context';
|
|
8
|
-
import { ObservableBus } from './utils/observable-bus';
|
|
5
|
+
import { Command } from './classes/index.js';
|
|
6
|
+
import { CqrsModuleOptions, ICommand, ICommandBus, ICommandHandler, ICommandPublisher } from './interfaces/index.js';
|
|
7
|
+
import { AsyncContext } from './scopes/async.context.js';
|
|
8
|
+
import { ObservableBus } from './utils/observable-bus.js';
|
|
9
9
|
export type CommandHandlerType<T extends ICommand = ICommand> = Type<ICommandHandler<T>>;
|
|
10
10
|
/**
|
|
11
11
|
* @publicApi
|
package/dist/command-bus.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
2
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
3
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
@@ -12,27 +11,25 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
|
12
11
|
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
12
|
};
|
|
14
13
|
var CommandBus_1;
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
const async_context_1 = require("./scopes/async.context");
|
|
26
|
-
const observable_bus_1 = require("./utils/observable-bus");
|
|
14
|
+
import { Inject, Injectable, Logger, Optional } from '@nestjs/common';
|
|
15
|
+
import { ModuleRef } from '@nestjs/core';
|
|
16
|
+
import 'reflect-metadata';
|
|
17
|
+
import { CQRS_MODULE_OPTIONS } from './constants.js';
|
|
18
|
+
import { COMMAND_HANDLER_METADATA, COMMAND_METADATA, } from './decorators/constants.js';
|
|
19
|
+
import { CommandHandlerNotFoundException } from './exceptions/command-not-found.exception.js';
|
|
20
|
+
import { DefaultCommandPubSub } from './helpers/default-command-pubsub.js';
|
|
21
|
+
import { InvalidCommandHandlerException } from './index.js';
|
|
22
|
+
import { AsyncContext } from './scopes/async.context.js';
|
|
23
|
+
import { ObservableBus } from './utils/observable-bus.js';
|
|
27
24
|
/**
|
|
28
25
|
* @publicApi
|
|
29
26
|
*/
|
|
30
|
-
let CommandBus = CommandBus_1 = class CommandBus extends
|
|
27
|
+
let CommandBus = CommandBus_1 = class CommandBus extends ObservableBus {
|
|
31
28
|
constructor(moduleRef, options) {
|
|
32
29
|
super();
|
|
33
30
|
this.moduleRef = moduleRef;
|
|
34
31
|
this.options = options;
|
|
35
|
-
this.logger = new
|
|
32
|
+
this.logger = new Logger(CommandBus_1.name);
|
|
36
33
|
this.handlers = new Map();
|
|
37
34
|
if (this.options?.commandPublisher) {
|
|
38
35
|
this._publisher = this.options.commandPublisher;
|
|
@@ -67,7 +64,7 @@ let CommandBus = CommandBus_1 = class CommandBus extends observable_bus_1.Observ
|
|
|
67
64
|
const executeFn = this.handlers.get(commandId);
|
|
68
65
|
if (!executeFn) {
|
|
69
66
|
const commandName = this.getCommandName(command);
|
|
70
|
-
throw new
|
|
67
|
+
throw new CommandHandlerNotFoundException(commandName);
|
|
71
68
|
}
|
|
72
69
|
this._publisher.publish(command);
|
|
73
70
|
return executeFn(command, context);
|
|
@@ -76,14 +73,14 @@ let CommandBus = CommandBus_1 = class CommandBus extends observable_bus_1.Observ
|
|
|
76
73
|
if (handler.isDependencyTreeStatic()) {
|
|
77
74
|
const instance = handler.instance;
|
|
78
75
|
if (!instance.execute) {
|
|
79
|
-
throw new
|
|
76
|
+
throw new InvalidCommandHandlerException();
|
|
80
77
|
}
|
|
81
78
|
this.handlers.set(id, (command) => instance.execute(command));
|
|
82
79
|
return;
|
|
83
80
|
}
|
|
84
81
|
this.handlers.set(id, async (command, context) => {
|
|
85
|
-
context ??=
|
|
86
|
-
if (!
|
|
82
|
+
context ??= AsyncContext.of(command) ?? new AsyncContext();
|
|
83
|
+
if (!AsyncContext.isAttached(context)) {
|
|
87
84
|
// Commands returned by sagas may already have an async context set
|
|
88
85
|
// and a corresponding request provider registered.
|
|
89
86
|
this.moduleRef.registerRequestByContextId(context, context.id);
|
|
@@ -99,10 +96,10 @@ let CommandBus = CommandBus_1 = class CommandBus extends observable_bus_1.Observ
|
|
|
99
96
|
handlers.forEach((handler) => this.registerHandler(handler));
|
|
100
97
|
}
|
|
101
98
|
registerHandler(handler) {
|
|
102
|
-
const typeRef = handler.metatype;
|
|
99
|
+
const typeRef = (handler.inject ? handler.instance?.constructor : handler.metatype);
|
|
103
100
|
const target = this.reflectCommandId(typeRef);
|
|
104
101
|
if (!target) {
|
|
105
|
-
throw new
|
|
102
|
+
throw new InvalidCommandHandlerException();
|
|
106
103
|
}
|
|
107
104
|
if (this.handlers.has(target)) {
|
|
108
105
|
this.logger.warn(`Command handler [${typeRef.name}] is already registered. Overriding previously registered handler.`);
|
|
@@ -111,9 +108,9 @@ let CommandBus = CommandBus_1 = class CommandBus extends observable_bus_1.Observ
|
|
|
111
108
|
}
|
|
112
109
|
getCommandId(command) {
|
|
113
110
|
const { constructor: commandType } = Object.getPrototypeOf(command);
|
|
114
|
-
const commandMetadata = Reflect.getMetadata(
|
|
111
|
+
const commandMetadata = Reflect.getMetadata(COMMAND_METADATA, commandType);
|
|
115
112
|
if (!commandMetadata) {
|
|
116
|
-
throw new
|
|
113
|
+
throw new CommandHandlerNotFoundException(commandType.name);
|
|
117
114
|
}
|
|
118
115
|
return commandMetadata.id;
|
|
119
116
|
}
|
|
@@ -122,18 +119,18 @@ let CommandBus = CommandBus_1 = class CommandBus extends observable_bus_1.Observ
|
|
|
122
119
|
return constructor.name;
|
|
123
120
|
}
|
|
124
121
|
reflectCommandId(handler) {
|
|
125
|
-
const command = Reflect.getMetadata(
|
|
126
|
-
const commandMetadata = Reflect.getMetadata(
|
|
122
|
+
const command = Reflect.getMetadata(COMMAND_HANDLER_METADATA, handler);
|
|
123
|
+
const commandMetadata = Reflect.getMetadata(COMMAND_METADATA, command);
|
|
127
124
|
return commandMetadata.id;
|
|
128
125
|
}
|
|
129
126
|
useDefaultPublisher() {
|
|
130
|
-
this._publisher = new
|
|
127
|
+
this._publisher = new DefaultCommandPubSub(this.subject$);
|
|
131
128
|
}
|
|
132
129
|
};
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
(
|
|
136
|
-
__param(1, (
|
|
137
|
-
|
|
138
|
-
__metadata("design:paramtypes", [core_1.ModuleRef, Object])
|
|
130
|
+
CommandBus = CommandBus_1 = __decorate([
|
|
131
|
+
Injectable(),
|
|
132
|
+
__param(1, Optional()),
|
|
133
|
+
__param(1, Inject(CQRS_MODULE_OPTIONS)),
|
|
134
|
+
__metadata("design:paramtypes", [ModuleRef, Object])
|
|
139
135
|
], CommandBus);
|
|
136
|
+
export { CommandBus };
|
package/dist/constants.js
CHANGED
package/dist/cqrs.module.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { DynamicModule, OnApplicationBootstrap } from '@nestjs/common';
|
|
2
|
-
import { CommandBus } from './command-bus';
|
|
3
|
-
import { EventBus } from './event-bus';
|
|
4
|
-
import { CqrsModuleAsyncOptions, CqrsModuleOptions, IEvent } from './interfaces';
|
|
5
|
-
import { QueryBus } from './query-bus';
|
|
6
|
-
import { ExplorerService } from './services/explorer.service';
|
|
2
|
+
import { CommandBus } from './command-bus.js';
|
|
3
|
+
import { EventBus } from './event-bus.js';
|
|
4
|
+
import { CqrsModuleAsyncOptions, CqrsModuleOptions, IEvent } from './interfaces/index.js';
|
|
5
|
+
import { QueryBus } from './query-bus.js';
|
|
6
|
+
import { ExplorerService } from './services/explorer.service.js';
|
|
7
7
|
/**
|
|
8
8
|
* @publicApi
|
|
9
9
|
*/
|
package/dist/cqrs.module.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
2
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
3
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
@@ -9,17 +8,15 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
9
8
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
9
|
};
|
|
11
10
|
var CqrsModule_1;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const unhandled_exception_bus_1 = require("./unhandled-exception-bus");
|
|
22
|
-
const aggregate_root_storage_1 = require("./storages/aggregate-root.storage");
|
|
11
|
+
import { Module, } from '@nestjs/common';
|
|
12
|
+
import { CommandBus } from './command-bus.js';
|
|
13
|
+
import { CQRS_MODULE_OPTIONS } from './constants.js';
|
|
14
|
+
import { EventBus } from './event-bus.js';
|
|
15
|
+
import { EventPublisher } from './event-publisher.js';
|
|
16
|
+
import { QueryBus } from './query-bus.js';
|
|
17
|
+
import { ExplorerService } from './services/explorer.service.js';
|
|
18
|
+
import { UnhandledExceptionBus } from './unhandled-exception-bus.js';
|
|
19
|
+
import { AggregateRootStorage } from './storages/aggregate-root.storage.js';
|
|
23
20
|
/**
|
|
24
21
|
* @publicApi
|
|
25
22
|
*/
|
|
@@ -29,7 +26,7 @@ let CqrsModule = CqrsModule_1 = class CqrsModule {
|
|
|
29
26
|
module: CqrsModule_1,
|
|
30
27
|
providers: [
|
|
31
28
|
{
|
|
32
|
-
provide:
|
|
29
|
+
provide: CQRS_MODULE_OPTIONS,
|
|
33
30
|
useValue: options ?? {},
|
|
34
31
|
},
|
|
35
32
|
],
|
|
@@ -42,14 +39,14 @@ let CqrsModule = CqrsModule_1 = class CqrsModule {
|
|
|
42
39
|
global: true,
|
|
43
40
|
imports: options.imports || [],
|
|
44
41
|
providers: [...this.createAsyncProviders(options)],
|
|
45
|
-
exports: [
|
|
42
|
+
exports: [CQRS_MODULE_OPTIONS],
|
|
46
43
|
};
|
|
47
44
|
}
|
|
48
45
|
static createAsyncProviders(options) {
|
|
49
46
|
if (options.useValue) {
|
|
50
47
|
return [
|
|
51
48
|
{
|
|
52
|
-
provide:
|
|
49
|
+
provide: CQRS_MODULE_OPTIONS,
|
|
53
50
|
useValue: options.useValue,
|
|
54
51
|
},
|
|
55
52
|
];
|
|
@@ -57,7 +54,7 @@ let CqrsModule = CqrsModule_1 = class CqrsModule {
|
|
|
57
54
|
if (options.useFactory) {
|
|
58
55
|
return [
|
|
59
56
|
{
|
|
60
|
-
provide:
|
|
57
|
+
provide: CQRS_MODULE_OPTIONS,
|
|
61
58
|
useFactory: options.useFactory,
|
|
62
59
|
inject: options.inject || [],
|
|
63
60
|
},
|
|
@@ -70,7 +67,7 @@ let CqrsModule = CqrsModule_1 = class CqrsModule {
|
|
|
70
67
|
useClass: options.useClass,
|
|
71
68
|
},
|
|
72
69
|
{
|
|
73
|
-
provide:
|
|
70
|
+
provide: CQRS_MODULE_OPTIONS,
|
|
74
71
|
useFactory: async (optionsFactory) => optionsFactory.createCqrsOptions(),
|
|
75
72
|
inject: [options.useClass],
|
|
76
73
|
},
|
|
@@ -79,7 +76,7 @@ let CqrsModule = CqrsModule_1 = class CqrsModule {
|
|
|
79
76
|
if (options.useExisting) {
|
|
80
77
|
return [
|
|
81
78
|
{
|
|
82
|
-
provide:
|
|
79
|
+
provide: CQRS_MODULE_OPTIONS,
|
|
83
80
|
useFactory: async (optionsFactory) => optionsFactory.createCqrsOptions(),
|
|
84
81
|
inject: [options.useExisting],
|
|
85
82
|
},
|
|
@@ -99,30 +96,30 @@ let CqrsModule = CqrsModule_1 = class CqrsModule {
|
|
|
99
96
|
this.commandBus.register(commands);
|
|
100
97
|
this.queryBus.register(queries);
|
|
101
98
|
this.eventBus.registerSagas(sagas);
|
|
102
|
-
|
|
99
|
+
AggregateRootStorage.mergeContext(this.eventBus);
|
|
103
100
|
}
|
|
104
101
|
};
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
(0, common_1.Module)({
|
|
102
|
+
CqrsModule = CqrsModule_1 = __decorate([
|
|
103
|
+
Module({
|
|
108
104
|
providers: [
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
105
|
+
CommandBus,
|
|
106
|
+
QueryBus,
|
|
107
|
+
EventBus,
|
|
108
|
+
UnhandledExceptionBus,
|
|
109
|
+
EventPublisher,
|
|
110
|
+
ExplorerService,
|
|
115
111
|
],
|
|
116
112
|
exports: [
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
113
|
+
CommandBus,
|
|
114
|
+
QueryBus,
|
|
115
|
+
EventBus,
|
|
116
|
+
UnhandledExceptionBus,
|
|
117
|
+
EventPublisher,
|
|
122
118
|
],
|
|
123
119
|
}),
|
|
124
|
-
__metadata("design:paramtypes", [
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
120
|
+
__metadata("design:paramtypes", [ExplorerService,
|
|
121
|
+
EventBus,
|
|
122
|
+
CommandBus,
|
|
123
|
+
QueryBus])
|
|
128
124
|
], CqrsModule);
|
|
125
|
+
export { CqrsModule };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { InjectableOptions } from '@nestjs/common';
|
|
2
2
|
import 'reflect-metadata';
|
|
3
|
-
import { ICommand } from '../index';
|
|
3
|
+
import { ICommand } from '../index.js';
|
|
4
4
|
/**
|
|
5
5
|
* Decorator that marks a class as a Nest command handler. A command handler
|
|
6
6
|
* handles commands (actions) executed by your application code.
|
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const crypto_1 = require("crypto");
|
|
6
|
-
require("reflect-metadata");
|
|
7
|
-
const constants_1 = require("./constants");
|
|
1
|
+
import { Injectable } from '@nestjs/common';
|
|
2
|
+
import { randomUUID } from 'crypto';
|
|
3
|
+
import 'reflect-metadata';
|
|
4
|
+
import { COMMAND_HANDLER_METADATA, COMMAND_METADATA } from './constants.js';
|
|
8
5
|
/**
|
|
9
6
|
* Decorator that marks a class as a Nest command handler. A command handler
|
|
10
7
|
* handles commands (actions) executed by your application code.
|
|
@@ -18,15 +15,14 @@ const constants_1 = require("./constants");
|
|
|
18
15
|
*
|
|
19
16
|
* @publicApi
|
|
20
17
|
*/
|
|
21
|
-
const CommandHandler = (command, options) => {
|
|
18
|
+
export const CommandHandler = (command, options) => {
|
|
22
19
|
return (target) => {
|
|
23
|
-
if (!Reflect.hasOwnMetadata(
|
|
24
|
-
Reflect.defineMetadata(
|
|
20
|
+
if (!Reflect.hasOwnMetadata(COMMAND_METADATA, command)) {
|
|
21
|
+
Reflect.defineMetadata(COMMAND_METADATA, { id: randomUUID() }, command);
|
|
25
22
|
}
|
|
26
|
-
Reflect.defineMetadata(
|
|
23
|
+
Reflect.defineMetadata(COMMAND_HANDLER_METADATA, command, target);
|
|
27
24
|
if (options) {
|
|
28
|
-
|
|
25
|
+
Injectable(options)(target);
|
|
29
26
|
}
|
|
30
27
|
};
|
|
31
28
|
};
|
|
32
|
-
exports.CommandHandler = CommandHandler;
|
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
exports.EVENT_METADATA = '__event__';
|
|
9
|
-
exports.EVENTS_HANDLER_METADATA = '__eventsHandler__';
|
|
10
|
-
exports.SAGA_METADATA = '__saga__';
|
|
1
|
+
export const COMMAND_METADATA = '__command__';
|
|
2
|
+
export const COMMAND_HANDLER_METADATA = '__commandHandler__';
|
|
3
|
+
export const QUERY_METADATA = '__query__';
|
|
4
|
+
export const QUERY_HANDLER_METADATA = '__queryHandler__';
|
|
5
|
+
export const EVENT_METADATA = '__event__';
|
|
6
|
+
export const EVENTS_HANDLER_METADATA = '__eventsHandler__';
|
|
7
|
+
export const SAGA_METADATA = '__saga__';
|