@nestjs/cqrs 11.0.3 → 12.1.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 (149) hide show
  1. package/.circleci/config.yml +3 -3
  2. package/.nvmrc +1 -0
  3. package/dist/aggregate-root.d.ts +3 -67
  4. package/dist/aggregate-root.js +6 -83
  5. package/dist/classes/command.d.ts +2 -2
  6. package/dist/classes/command.js +2 -6
  7. package/dist/classes/constants.js +1 -4
  8. package/dist/classes/index.d.ts +2 -2
  9. package/dist/classes/index.js +2 -18
  10. package/dist/classes/query.d.ts +2 -2
  11. package/dist/classes/query.js +2 -6
  12. package/dist/command-bus.d.ts +5 -5
  13. package/dist/command-bus.js +29 -32
  14. package/dist/constants.js +1 -4
  15. package/dist/cqrs.module.d.ts +5 -5
  16. package/dist/cqrs.module.js +34 -37
  17. package/dist/decorators/command-handler.decorator.d.ts +1 -1
  18. package/dist/decorators/command-handler.decorator.js +9 -13
  19. package/dist/decorators/constants.js +7 -10
  20. package/dist/decorators/events-handler.decorator.d.ts +1 -1
  21. package/dist/decorators/events-handler.decorator.js +9 -13
  22. package/dist/decorators/index.d.ts +5 -5
  23. package/dist/decorators/index.js +5 -21
  24. package/dist/decorators/publishable.decorator.js +3 -6
  25. package/dist/decorators/query-handler.decorator.d.ts +1 -1
  26. package/dist/decorators/query-handler.decorator.js +9 -13
  27. package/dist/decorators/saga.decorator.js +5 -9
  28. package/dist/event-bus.d.ts +10 -8
  29. package/dist/event-bus.js +52 -52
  30. package/dist/event-publisher.d.ts +8 -7
  31. package/dist/event-publisher.js +17 -18
  32. package/dist/exceptions/command-not-found.exception.js +1 -5
  33. package/dist/exceptions/index.d.ts +7 -7
  34. package/dist/exceptions/index.js +7 -23
  35. package/dist/exceptions/invalid-command-handler.exception.js +1 -5
  36. package/dist/exceptions/invalid-events-handler.exception.js +1 -5
  37. package/dist/exceptions/invalid-query-handler.exception.js +1 -5
  38. package/dist/exceptions/invalid-saga.exception.js +1 -5
  39. package/dist/exceptions/query-not-found.exception.js +1 -5
  40. package/dist/exceptions/unsupported-saga-scope.exception.js +1 -5
  41. package/dist/helpers/default-command-pubsub.d.ts +1 -1
  42. package/dist/helpers/default-command-pubsub.js +1 -5
  43. package/dist/helpers/default-event-id-provider.d.ts +1 -1
  44. package/dist/helpers/default-event-id-provider.js +3 -6
  45. package/dist/helpers/default-pubsub.d.ts +1 -1
  46. package/dist/helpers/default-pubsub.js +1 -5
  47. package/dist/helpers/default-query-pubsub.d.ts +1 -1
  48. package/dist/helpers/default-query-pubsub.js +1 -5
  49. package/dist/helpers/default-unhandled-exception-pubsub.d.ts +1 -1
  50. package/dist/helpers/default-unhandled-exception-pubsub.js +1 -5
  51. package/dist/index.d.ts +15 -14
  52. package/dist/index.js +15 -30
  53. package/dist/interfaces/aggregate-root.interface.d.ts +63 -0
  54. package/dist/interfaces/aggregate-root.interface.js +1 -0
  55. package/dist/interfaces/commands/command-bus.interface.d.ts +3 -3
  56. package/dist/interfaces/commands/command-bus.interface.js +1 -2
  57. package/dist/interfaces/commands/command-handler.interface.d.ts +2 -2
  58. package/dist/interfaces/commands/command-handler.interface.js +1 -2
  59. package/dist/interfaces/commands/command-metadata.interface.js +1 -2
  60. package/dist/interfaces/commands/command-publisher.interface.d.ts +1 -1
  61. package/dist/interfaces/commands/command-publisher.interface.js +1 -2
  62. package/dist/interfaces/commands/command.interface.js +1 -2
  63. package/dist/interfaces/cqrs-module-options.interface.d.ts +5 -5
  64. package/dist/interfaces/cqrs-module-options.interface.js +1 -2
  65. package/dist/interfaces/events/event-bus.interface.d.ts +2 -2
  66. package/dist/interfaces/events/event-bus.interface.js +1 -2
  67. package/dist/interfaces/events/event-handler.interface.d.ts +1 -1
  68. package/dist/interfaces/events/event-handler.interface.js +1 -2
  69. package/dist/interfaces/events/event-id-provider.interface.d.ts +1 -1
  70. package/dist/interfaces/events/event-id-provider.interface.js +1 -2
  71. package/dist/interfaces/events/event-publisher.interface.d.ts +2 -2
  72. package/dist/interfaces/events/event-publisher.interface.js +1 -2
  73. package/dist/interfaces/events/event.interface.js +1 -2
  74. package/dist/interfaces/events/message-source.interface.d.ts +1 -1
  75. package/dist/interfaces/events/message-source.interface.js +1 -2
  76. package/dist/interfaces/exceptions/unhandled-exception-info.interface.d.ts +2 -2
  77. package/dist/interfaces/exceptions/unhandled-exception-info.interface.js +1 -2
  78. package/dist/interfaces/exceptions/unhandled-exception-publisher.interface.d.ts +3 -3
  79. package/dist/interfaces/exceptions/unhandled-exception-publisher.interface.js +1 -2
  80. package/dist/interfaces/index.d.ts +20 -19
  81. package/dist/interfaces/index.js +20 -35
  82. package/dist/interfaces/providers-introspection-result.interface.d.ts +4 -4
  83. package/dist/interfaces/providers-introspection-result.interface.js +1 -2
  84. package/dist/interfaces/queries/query-bus.interface.d.ts +3 -3
  85. package/dist/interfaces/queries/query-bus.interface.js +1 -2
  86. package/dist/interfaces/queries/query-handler.interface.d.ts +2 -2
  87. package/dist/interfaces/queries/query-handler.interface.js +1 -2
  88. package/dist/interfaces/queries/query-metadata.interface.js +1 -2
  89. package/dist/interfaces/queries/query-publisher.interface.d.ts +1 -1
  90. package/dist/interfaces/queries/query-publisher.interface.js +1 -2
  91. package/dist/interfaces/queries/query-result.interface.js +1 -2
  92. package/dist/interfaces/queries/query.interface.js +1 -2
  93. package/dist/interfaces/saga.type.d.ts +2 -2
  94. package/dist/interfaces/saga.type.js +1 -2
  95. package/dist/mixins/index.d.ts +1 -0
  96. package/dist/mixins/index.js +1 -0
  97. package/dist/mixins/with-aggregate-root.mixin.d.ts +11 -0
  98. package/dist/mixins/with-aggregate-root.mixin.js +75 -0
  99. package/dist/operators/index.d.ts +1 -1
  100. package/dist/operators/index.js +1 -17
  101. package/dist/operators/of-type.d.ts +1 -1
  102. package/dist/operators/of-type.js +3 -6
  103. package/dist/query-bus.d.ts +5 -5
  104. package/dist/query-bus.js +28 -31
  105. package/dist/scopes/async.context.js +9 -13
  106. package/dist/scopes/index.d.ts +1 -1
  107. package/dist/scopes/index.js +1 -17
  108. package/dist/services/explorer.service.d.ts +5 -5
  109. package/dist/services/explorer.service.js +14 -20
  110. package/dist/storages/aggregate-root.storage.d.ts +3 -3
  111. package/dist/storages/aggregate-root.storage.js +9 -9
  112. package/dist/unhandled-exception-bus.d.ts +2 -2
  113. package/dist/unhandled-exception-bus.js +14 -17
  114. package/dist/utils/index.d.ts +1 -1
  115. package/dist/utils/index.js +1 -17
  116. package/dist/utils/observable-bus.js +3 -7
  117. package/package.json +34 -26
  118. package/test/e2e/aggregate-commit-context.spec.ts +243 -0
  119. package/test/e2e/basic-flows.spec.ts +46 -26
  120. package/test/e2e/generics.spec.ts +3 -3
  121. package/test/e2e/request-scope.spec.ts +23 -22
  122. package/test/e2e/sync-event-handlers-app.spec.ts +123 -0
  123. package/test/e2e/unhandled-exceptions.spec.ts +55 -20
  124. package/tsconfig.build.json +3 -2
  125. package/vitest.config.e2e.ts +13 -0
  126. package/vitest.config.ts +8 -0
  127. package/dist/exceptions/invalid-module-ref.exception.d.ts +0 -3
  128. package/dist/exceptions/invalid-module-ref.exception.js +0 -8
  129. package/dist/helpers/default-get-event-id.d.ts +0 -9
  130. package/dist/helpers/default-get-event-id.js +0 -18
  131. package/dist/helpers/default-get-event-name.d.ts +0 -2
  132. package/dist/helpers/default-get-event-name.js +0 -8
  133. package/dist/interfaces/cqrs-options.interface.d.ts +0 -10
  134. package/dist/interfaces/cqrs-options.interface.js +0 -2
  135. package/dist/interfaces/event-observable.interface.d.ts +0 -4
  136. package/dist/interfaces/event-observable.interface.js +0 -2
  137. package/dist/utils/command-handler.decorator.d.ts +0 -3
  138. package/dist/utils/command-handler.decorator.js +0 -9
  139. package/dist/utils/constants.d.ts +0 -2
  140. package/dist/utils/constants.js +0 -4
  141. package/dist/utils/default-pubsub.d.ts +0 -9
  142. package/dist/utils/default-pubsub.js +0 -14
  143. package/dist/utils/events-handler.decorator.d.ts +0 -3
  144. package/dist/utils/events-handler.decorator.js +0 -9
  145. package/eslint.config.mjs +0 -42
  146. package/index.d.ts +0 -1
  147. package/index.js +0 -6
  148. package/jest.config.js +0 -9
  149. package/test/jest-e2e.json +0 -9
@@ -17,7 +17,7 @@ jobs:
17
17
  build:
18
18
  working_directory: ~/nest
19
19
  docker:
20
- - image: cimg/node:22.3
20
+ - image: cimg/node:24.21.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:22.3
38
+ - image: cimg/node:24.21.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:22.3
49
+ - image: cimg/node:24.21.0
50
50
  steps:
51
51
  - checkout
52
52
  - *restore-cache
package/.nvmrc ADDED
@@ -0,0 +1 @@
1
+ 24
@@ -1,7 +1,6 @@
1
1
  import { Type } from '@nestjs/common';
2
- import { IEvent, IEventHandler } from './interfaces';
3
- declare const INTERNAL_EVENTS: unique symbol;
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
  }
@@ -1,10 +1,4 @@
1
- "use strict";
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
- constructor() {
17
- this[_a] = false;
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
- const handler = `on${this.getEventName(event)}`;
87
- return this[handler];
13
+ return super['getEventHandler'](event);
88
14
  }
89
15
  getEventName(event) {
90
- const { constructor } = Object.getPrototypeOf(event);
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
  */
@@ -1,7 +1,3 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
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;
@@ -1,4 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RESULT_TYPE_SYMBOL = void 0;
4
- exports.RESULT_TYPE_SYMBOL = Symbol('RESULT_TYPE');
1
+ export const RESULT_TYPE_SYMBOL = Symbol('RESULT_TYPE');
@@ -1,2 +1,2 @@
1
- export * from './command';
2
- export * from './query';
1
+ export * from './command.js';
2
+ export * from './query.js';
@@ -1,18 +1,2 @@
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("./command"), exports);
18
- __exportStar(require("./query"), exports);
1
+ export * from './command.js';
2
+ export * from './query.js';
@@ -1,5 +1,5 @@
1
- import { IQuery } from '../interfaces';
2
- import { RESULT_TYPE_SYMBOL } from './constants';
1
+ import { IQuery } 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 query.
5
5
  */
@@ -1,7 +1,3 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
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;
@@ -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
@@ -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
- Object.defineProperty(exports, "__esModule", { value: true });
16
- exports.CommandBus = void 0;
17
- const common_1 = require("@nestjs/common");
18
- const core_1 = require("@nestjs/core");
19
- require("reflect-metadata");
20
- const constants_1 = require("./constants");
21
- const constants_2 = require("./decorators/constants");
22
- const command_not_found_exception_1 = require("./exceptions/command-not-found.exception");
23
- const default_command_pubsub_1 = require("./helpers/default-command-pubsub");
24
- const index_1 = require("./index");
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 observable_bus_1.ObservableBus {
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 common_1.Logger(CommandBus_1.name);
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 command_not_found_exception_1.CommandHandlerNotFoundException(commandName);
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 index_1.InvalidCommandHandlerException();
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 ??= async_context_1.AsyncContext.of(command) ?? new async_context_1.AsyncContext();
86
- if (!async_context_1.AsyncContext.isAttached(context)) {
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 index_1.InvalidCommandHandlerException();
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(constants_2.COMMAND_METADATA, commandType);
111
+ const commandMetadata = Reflect.getMetadata(COMMAND_METADATA, commandType);
115
112
  if (!commandMetadata) {
116
- throw new command_not_found_exception_1.CommandHandlerNotFoundException(commandType.name);
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(constants_2.COMMAND_HANDLER_METADATA, handler);
126
- const commandMetadata = Reflect.getMetadata(constants_2.COMMAND_METADATA, command);
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 default_command_pubsub_1.DefaultCommandPubSub(this.subject$);
127
+ this._publisher = new DefaultCommandPubSub(this.subject$);
131
128
  }
132
129
  };
133
- exports.CommandBus = CommandBus;
134
- exports.CommandBus = CommandBus = CommandBus_1 = __decorate([
135
- (0, common_1.Injectable)(),
136
- __param(1, (0, common_1.Optional)()),
137
- __param(1, (0, common_1.Inject)(constants_1.CQRS_MODULE_OPTIONS)),
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
@@ -1,4 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CQRS_MODULE_OPTIONS = void 0;
4
- exports.CQRS_MODULE_OPTIONS = 'CQRS_MODULE_OPTIONS';
1
+ export const CQRS_MODULE_OPTIONS = 'CQRS_MODULE_OPTIONS';
@@ -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
  */
@@ -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
- Object.defineProperty(exports, "__esModule", { value: true });
13
- exports.CqrsModule = void 0;
14
- const common_1 = require("@nestjs/common");
15
- const command_bus_1 = require("./command-bus");
16
- const constants_1 = require("./constants");
17
- const event_bus_1 = require("./event-bus");
18
- const event_publisher_1 = require("./event-publisher");
19
- const query_bus_1 = require("./query-bus");
20
- const explorer_service_1 = require("./services/explorer.service");
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: constants_1.CQRS_MODULE_OPTIONS,
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: [constants_1.CQRS_MODULE_OPTIONS],
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: constants_1.CQRS_MODULE_OPTIONS,
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: constants_1.CQRS_MODULE_OPTIONS,
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: constants_1.CQRS_MODULE_OPTIONS,
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: constants_1.CQRS_MODULE_OPTIONS,
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
- aggregate_root_storage_1.AggregateRootStorage.mergeContext(this.eventBus);
99
+ AggregateRootStorage.mergeContext(this.eventBus);
103
100
  }
104
101
  };
105
- exports.CqrsModule = CqrsModule;
106
- exports.CqrsModule = CqrsModule = CqrsModule_1 = __decorate([
107
- (0, common_1.Module)({
102
+ CqrsModule = CqrsModule_1 = __decorate([
103
+ Module({
108
104
  providers: [
109
- command_bus_1.CommandBus,
110
- query_bus_1.QueryBus,
111
- event_bus_1.EventBus,
112
- unhandled_exception_bus_1.UnhandledExceptionBus,
113
- event_publisher_1.EventPublisher,
114
- explorer_service_1.ExplorerService,
105
+ CommandBus,
106
+ QueryBus,
107
+ EventBus,
108
+ UnhandledExceptionBus,
109
+ EventPublisher,
110
+ ExplorerService,
115
111
  ],
116
112
  exports: [
117
- command_bus_1.CommandBus,
118
- query_bus_1.QueryBus,
119
- event_bus_1.EventBus,
120
- unhandled_exception_bus_1.UnhandledExceptionBus,
121
- event_publisher_1.EventPublisher,
113
+ CommandBus,
114
+ QueryBus,
115
+ EventBus,
116
+ UnhandledExceptionBus,
117
+ EventPublisher,
122
118
  ],
123
119
  }),
124
- __metadata("design:paramtypes", [explorer_service_1.ExplorerService,
125
- event_bus_1.EventBus,
126
- command_bus_1.CommandBus,
127
- query_bus_1.QueryBus])
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
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CommandHandler = void 0;
4
- const common_1 = require("@nestjs/common");
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(constants_1.COMMAND_METADATA, command)) {
24
- Reflect.defineMetadata(constants_1.COMMAND_METADATA, { id: (0, crypto_1.randomUUID)() }, command);
20
+ if (!Reflect.hasOwnMetadata(COMMAND_METADATA, command)) {
21
+ Reflect.defineMetadata(COMMAND_METADATA, { id: randomUUID() }, command);
25
22
  }
26
- Reflect.defineMetadata(constants_1.COMMAND_HANDLER_METADATA, command, target);
23
+ Reflect.defineMetadata(COMMAND_HANDLER_METADATA, command, target);
27
24
  if (options) {
28
- (0, common_1.Injectable)(options)(target);
25
+ Injectable(options)(target);
29
26
  }
30
27
  };
31
28
  };
32
- exports.CommandHandler = CommandHandler;
@@ -1,10 +1,7 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SAGA_METADATA = exports.EVENTS_HANDLER_METADATA = exports.EVENT_METADATA = exports.QUERY_HANDLER_METADATA = exports.QUERY_METADATA = exports.COMMAND_HANDLER_METADATA = exports.COMMAND_METADATA = void 0;
4
- exports.COMMAND_METADATA = '__command__';
5
- exports.COMMAND_HANDLER_METADATA = '__commandHandler__';
6
- exports.QUERY_METADATA = '__query__';
7
- exports.QUERY_HANDLER_METADATA = '__queryHandler__';
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__';