@nestjs/cqrs 8.0.0 → 8.0.3

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.
@@ -17,7 +17,7 @@ jobs:
17
17
  build:
18
18
  working_directory: ~/nest
19
19
  docker:
20
- - image: circleci/node:16
20
+ - image: circleci/node:17
21
21
  steps:
22
22
  - checkout
23
23
  - run:
@@ -0,0 +1,106 @@
1
+ name: "\U0001F41B Bug Report"
2
+ description: "If something isn't working as expected \U0001F914"
3
+ labels: ["needs triage", "bug"]
4
+ body:
5
+ - type: markdown
6
+ attributes:
7
+ value: |
8
+ ## :warning: We use GitHub Issues to track bug reports, feature requests and regressions
9
+
10
+ If you are not sure that your issue is a bug, you could:
11
+
12
+ - use our [Discord community](https://discord.gg/NestJS)
13
+ - use [StackOverflow using the tag `nestjs`](https://stackoverflow.com/questions/tagged/nestjs)
14
+ - If it's just a quick question you can ping [our Twitter](https://twitter.com/nestframework)
15
+
16
+ **NOTE:** You don't need to answer questions that you know that aren't relevant.
17
+
18
+ ---
19
+
20
+ - type: checkboxes
21
+ attributes:
22
+ label: "Is there an existing issue for this?"
23
+ description: "Please search [here](./?q=is%3Aissue) to see if an issue already exists for the bug you encountered"
24
+ options:
25
+ - label: "I have searched the existing issues"
26
+ required: true
27
+
28
+ - type: textarea
29
+ validations:
30
+ required: true
31
+ attributes:
32
+ label: "Current behavior"
33
+ description: "How the issue manifests?"
34
+
35
+ - type: input
36
+ validations:
37
+ required: true
38
+ attributes:
39
+ label: "Minimum reproduction code"
40
+ description: "An URL to some git repository or gist that reproduces this issue. [Wtf is a minimum reproduction?](https://jmcdo29.github.io/wtf-is-a-minimum-reproduction)"
41
+ placeholder: "https://github.com/..."
42
+
43
+ - type: textarea
44
+ attributes:
45
+ label: "Steps to reproduce"
46
+ description: |
47
+ How the issue manifests?
48
+ You could leave this blank if you alread write this in your reproduction code/repo
49
+ placeholder: |
50
+ 1. `npm i`
51
+ 2. `npm start:dev`
52
+ 3. See error...
53
+
54
+ - type: textarea
55
+ validations:
56
+ required: true
57
+ attributes:
58
+ label: "Expected behavior"
59
+ description: "A clear and concise description of what you expected to happend (or code)"
60
+
61
+ - type: markdown
62
+ attributes:
63
+ value: |
64
+ ---
65
+
66
+ - type: input
67
+ validations:
68
+ required: true
69
+ attributes:
70
+ label: "Package version"
71
+ description: |
72
+ Which version of `@nestjs/cqrs` are you using?
73
+ **Tip**: Make sure that all of yours `@nestjs/*` dependencies are in sync!
74
+ placeholder: "8.1.3"
75
+
76
+ - type: input
77
+ attributes:
78
+ label: "NestJS version"
79
+ description: "Which version of `@nestjs/core` are you using?"
80
+ placeholder: "8.1.3"
81
+
82
+ - type: input
83
+ attributes:
84
+ label: "Node.js version"
85
+ description: "Which version of Node.js are you using?"
86
+ placeholder: "14.17.6"
87
+
88
+ - type: checkboxes
89
+ attributes:
90
+ label: "In which operating systems have you tested?"
91
+ options:
92
+ - label: macOS
93
+ - label: Windows
94
+ - label: Linux
95
+
96
+ - type: markdown
97
+ attributes:
98
+ value: |
99
+ ---
100
+
101
+ - type: textarea
102
+ attributes:
103
+ label: "Other"
104
+ description: |
105
+ Anything else relevant? eg: Logs, OS version, IDE, package manager, etc.
106
+ **Tip:** You can attach images, recordings or log files by clicking this area to highlight it and then dragging files in
@@ -0,0 +1,52 @@
1
+ name: "\U0001F680 Feature Request"
2
+ description: "I have a suggestion \U0001F63B!"
3
+ labels: ["feature"]
4
+ body:
5
+ - type: markdown
6
+ attributes:
7
+ value: |
8
+ ## :warning: We use GitHub Issues to track bug reports, feature requests and regressions
9
+
10
+ If you are not sure that your issue is a bug, you could:
11
+
12
+ - use our [Discord community](https://discord.gg/NestJS)
13
+ - use [StackOverflow using the tag `nestjs`](https://stackoverflow.com/questions/tagged/nestjs)
14
+ - If it's just a quick question you can ping [our Twitter](https://twitter.com/nestframework)
15
+
16
+ ---
17
+
18
+ - type: checkboxes
19
+ attributes:
20
+ label: "Is there an existing issue that is already proposing this?"
21
+ description: "Please search [here](./?q=is%3Aissue) to see if an issue already exists for the feature you are requesting"
22
+ options:
23
+ - label: "I have searched the existing issues"
24
+ required: true
25
+
26
+ - type: textarea
27
+ validations:
28
+ required: true
29
+ attributes:
30
+ label: "Is your feature request related to a problem? Please describe it"
31
+ description: "A clear and concise description of what the problem is"
32
+ placeholder: |
33
+ I have an issue when ...
34
+
35
+ - type: textarea
36
+ validations:
37
+ required: true
38
+ attributes:
39
+ label: "Describe the solution you'd like"
40
+ description: "A clear and concise description of what you want to happen. Add any considered drawbacks"
41
+
42
+ - type: textarea
43
+ attributes:
44
+ label: "Teachability, documentation, adoption, migration strategy"
45
+ description: "If you can, explain how users will be able to use this and possibly write out a version the docs. Maybe a screenshot or design?"
46
+
47
+ - type: textarea
48
+ validations:
49
+ required: true
50
+ attributes:
51
+ label: "What is the motivation / use case for changing the behavior?"
52
+ description: "Describe the motivation or the concrete use case"
@@ -0,0 +1,78 @@
1
+ name: "\U0001F4A5 Regression"
2
+ description: "Report an unexpected behavior while upgrading your Nest application!"
3
+ labels: ["needs triage"]
4
+ body:
5
+ - type: markdown
6
+ attributes:
7
+ value: |
8
+ ## :warning: We use GitHub Issues to track bug reports, feature requests and regressions
9
+
10
+ If you are not sure that your issue is a bug, you could:
11
+
12
+ - use our [Discord community](https://discord.gg/NestJS)
13
+ - use [StackOverflow using the tag `nestjs`](https://stackoverflow.com/questions/tagged/nestjs)
14
+ - If it's just a quick question you can ping [our Twitter](https://twitter.com/nestframework)
15
+
16
+ **NOTE:** You don't need to answer questions that you know that aren't relevant.
17
+
18
+ ---
19
+
20
+ - type: checkboxes
21
+ attributes:
22
+ label: "Did you read the migration guide?"
23
+ description: "Check out the [migration guide here](https://docs.nestjs.com/migration-guide)!"
24
+ options:
25
+ - label: "I have read the whole migration guide"
26
+ required: false
27
+
28
+ - type: checkboxes
29
+ attributes:
30
+ label: "Is there an existing issue that is already proposing this?"
31
+ description: "Please search [here](./?q=is%3Aissue) to see if an issue already exists for the feature you are requesting"
32
+ options:
33
+ - label: "I have searched the existing issues"
34
+ required: true
35
+
36
+ - type: input
37
+ attributes:
38
+ label: "Potential Commit/PR that introduced the regression"
39
+ description: "If you have time to investigate, what PR/date/version introduced this issue"
40
+ placeholder: "PR #123 or commit 5b3c4a4"
41
+
42
+ - type: input
43
+ attributes:
44
+ label: "Versions"
45
+ description: "From which version of `@nestjs/cqrs` to which version you are upgrading"
46
+ placeholder: "8.1.0 -> 8.1.3"
47
+
48
+ - type: textarea
49
+ validations:
50
+ required: true
51
+ attributes:
52
+ label: "Describe the regression"
53
+ description: "A clear and concise description of what the regression is"
54
+
55
+ - type: textarea
56
+ attributes:
57
+ label: "Minimum reproduction code"
58
+ description: |
59
+ Please share a git repo, a gist, or step-by-step instructions. [Wtf is a minimum reproduction?](https://jmcdo29.github.io/wtf-is-a-minimum-reproduction)
60
+ **Tip:** If you leave a minimum repository, we will understand your issue faster!
61
+ value: |
62
+ ```ts
63
+
64
+ ```
65
+
66
+ - type: textarea
67
+ validations:
68
+ required: true
69
+ attributes:
70
+ label: "Expected behavior"
71
+ description: "A clear and concise description of what you expected to happend (or code)"
72
+
73
+ - type: textarea
74
+ attributes:
75
+ label: "Other"
76
+ description: |
77
+ Anything else relevant? eg: Logs, OS version, IDE, package manager, etc.
78
+ **Tip:** You can attach images, recordings or log files by clicking this area to highlight it and then dragging files in
@@ -0,0 +1,7 @@
1
+ ## To encourage contributors to use issue templates, we don't allow blank issues
2
+ blank_issues_enabled: false
3
+
4
+ contact_links:
5
+ - name: "\u2753 Discord Community of NestJS"
6
+ url: "https://discord.gg/NestJS"
7
+ about: "Please ask support questions or discuss suggestions/enhancements here."
@@ -10,15 +10,13 @@ Please check if your PR fulfills the following requirements:
10
10
  What kind of change does this PR introduce?
11
11
 
12
12
  <!-- Please check the one that applies to this PR using "x". -->
13
- ```
14
- [ ] Bugfix
15
- [ ] Feature
16
- [ ] Code style update (formatting, local variables)
17
- [ ] Refactoring (no functional changes, no api changes)
18
- [ ] Build related changes
19
- [ ] CI related changes
20
- [ ] Other... Please describe:
21
- ```
13
+ - [ ] Bugfix
14
+ - [ ] Feature
15
+ - [ ] Code style update (formatting, local variables)
16
+ - [ ] Refactoring (no functional changes, no api changes)
17
+ - [ ] Build related changes
18
+ - [ ] CI related changes
19
+ - [ ] Other... Please describe:
22
20
 
23
21
  ## What is the current behavior?
24
22
  <!-- Please describe the current behavior that you are modifying, or link to a relevant issue. -->
@@ -30,12 +28,10 @@ Issue Number: N/A
30
28
 
31
29
 
32
30
  ## Does this PR introduce a breaking change?
33
- ```
34
- [ ] Yes
35
- [ ] No
36
- ```
31
+ - [ ] Yes
32
+ - [ ] No
37
33
 
38
34
  <!-- If this PR contains a breaking change, please describe the impact and migration path for existing applications below. -->
39
35
 
40
36
 
41
- ## Other information
37
+ ## Other information
@@ -1,4 +1,5 @@
1
- import { IEvent } from './interfaces';
1
+ import { IEvent, IEventHandler } from './interfaces';
2
+ import { Type } from '@nestjs/common';
2
3
  declare const INTERNAL_EVENTS: unique symbol;
3
4
  declare const IS_AUTO_COMMIT_ENABLED: unique symbol;
4
5
  export declare abstract class AggregateRoot<EventBase extends IEvent = IEvent> {
@@ -13,7 +14,7 @@ export declare abstract class AggregateRoot<EventBase extends IEvent = IEvent> {
13
14
  getUncommittedEvents(): EventBase[];
14
15
  loadFromHistory(history: EventBase[]): void;
15
16
  apply<T extends EventBase = EventBase>(event: T, isFromHistory?: boolean): void;
16
- protected getEventHandler<T extends EventBase = EventBase>(event: T): Function | undefined;
17
+ protected getEventHandler<T extends EventBase = EventBase>(event: T): Type<IEventHandler> | undefined;
17
18
  protected getEventName(event: any): string;
18
19
  }
19
20
  export {};
@@ -12,10 +12,10 @@ export declare class CommandBus<CommandBase extends ICommand = ICommand> extends
12
12
  get publisher(): ICommandPublisher<CommandBase>;
13
13
  set publisher(_publisher: ICommandPublisher<CommandBase>);
14
14
  execute<T extends CommandBase, R = any>(command: T): Promise<R>;
15
- bind<T extends CommandBase>(handler: ICommandHandler<T>, name: string): void;
15
+ bind<T extends CommandBase>(handler: ICommandHandler<T>, id: string): void;
16
16
  register(handlers?: CommandHandlerType[]): void;
17
17
  protected registerHandler(handler: CommandHandlerType): void;
18
- private getCommandName;
19
- private reflectCommandName;
18
+ private getCommandId;
19
+ private reflectCommandId;
20
20
  private useDefaultPublisher;
21
21
  }
@@ -32,16 +32,16 @@ let CommandBus = class CommandBus extends observable_bus_1.ObservableBus {
32
32
  this._publisher = _publisher;
33
33
  }
34
34
  execute(command) {
35
- const commandName = this.getCommandName(command);
36
- const handler = this.handlers.get(commandName);
35
+ const commandId = this.getCommandId(command);
36
+ const handler = this.handlers.get(commandId);
37
37
  if (!handler) {
38
- throw new command_not_found_exception_1.CommandHandlerNotFoundException(commandName);
38
+ throw new command_not_found_exception_1.CommandHandlerNotFoundException(commandId);
39
39
  }
40
40
  this.subject$.next(command);
41
41
  return handler.execute(command);
42
42
  }
43
- bind(handler, name) {
44
- this.handlers.set(name, handler);
43
+ bind(handler, id) {
44
+ this.handlers.set(id, handler);
45
45
  }
46
46
  register(handlers = []) {
47
47
  handlers.forEach((handler) => this.registerHandler(handler));
@@ -51,25 +51,31 @@ let CommandBus = class CommandBus extends observable_bus_1.ObservableBus {
51
51
  if (!instance) {
52
52
  return;
53
53
  }
54
- const target = this.reflectCommandName(handler);
54
+ const target = this.reflectCommandId(handler);
55
55
  if (!target) {
56
56
  throw new index_1.InvalidCommandHandlerException();
57
57
  }
58
- this.bind(instance, target.name);
58
+ this.bind(instance, target);
59
59
  }
60
- getCommandName(command) {
61
- const { constructor } = Object.getPrototypeOf(command);
62
- return constructor.name;
60
+ getCommandId(command) {
61
+ const { constructor: commandType } = Object.getPrototypeOf(command);
62
+ const commandMetadata = Reflect.getMetadata(constants_1.COMMAND_METADATA, commandType);
63
+ if (!commandMetadata) {
64
+ throw new command_not_found_exception_1.CommandHandlerNotFoundException(commandType.name);
65
+ }
66
+ return commandMetadata.id;
63
67
  }
64
- reflectCommandName(handler) {
65
- return Reflect.getMetadata(constants_1.COMMAND_HANDLER_METADATA, handler);
68
+ reflectCommandId(handler) {
69
+ const command = Reflect.getMetadata(constants_1.COMMAND_HANDLER_METADATA, handler);
70
+ const commandMetadata = Reflect.getMetadata(constants_1.COMMAND_METADATA, command);
71
+ return commandMetadata.id;
66
72
  }
67
73
  useDefaultPublisher() {
68
74
  this._publisher = new default_command_pubsub_1.DefaultCommandPubSub(this.subject$);
69
75
  }
70
76
  };
71
77
  CommandBus = __decorate([
72
- common_1.Injectable(),
78
+ (0, common_1.Injectable)(),
73
79
  __metadata("design:paramtypes", [core_1.ModuleRef])
74
80
  ], CommandBus);
75
81
  exports.CommandBus = CommandBus;
@@ -32,7 +32,7 @@ let CqrsModule = class CqrsModule {
32
32
  }
33
33
  };
34
34
  CqrsModule = __decorate([
35
- common_1.Module({
35
+ (0, common_1.Module)({
36
36
  providers: [command_bus_1.CommandBus, query_bus_1.QueryBus, event_bus_1.EventBus, event_publisher_1.EventPublisher, explorer_service_1.ExplorerService],
37
37
  exports: [command_bus_1.CommandBus, query_bus_1.QueryBus, event_bus_1.EventBus, event_publisher_1.EventPublisher],
38
38
  }),
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CommandHandler = void 0;
4
4
  require("reflect-metadata");
5
5
  const constants_1 = require("./constants");
6
+ const uuid_1 = require("uuid");
6
7
  /**
7
8
  * Decorator that marks a class as a Nest command handler. A command handler
8
9
  * handles commands (actions) executed by your application code.
@@ -15,6 +16,9 @@ const constants_1 = require("./constants");
15
16
  */
16
17
  const CommandHandler = (command) => {
17
18
  return (target) => {
19
+ if (!Reflect.hasMetadata(constants_1.COMMAND_METADATA, command)) {
20
+ Reflect.defineMetadata(constants_1.COMMAND_METADATA, { id: (0, uuid_1.v4)() }, command);
21
+ }
18
22
  Reflect.defineMetadata(constants_1.COMMAND_HANDLER_METADATA, command, target);
19
23
  };
20
24
  };
@@ -1,4 +1,7 @@
1
+ export declare const COMMAND_METADATA = "__command__";
1
2
  export declare const COMMAND_HANDLER_METADATA = "__commandHandler__";
3
+ export declare const QUERY_METADATA = "__query__";
2
4
  export declare const QUERY_HANDLER_METADATA = "__queryHandler__";
3
- export declare const EVENTS_HANDLER_METADATA = "__eventsHandler";
5
+ export declare const EVENT_METADATA = "__event__";
6
+ export declare const EVENTS_HANDLER_METADATA = "__eventsHandler__";
4
7
  export declare const SAGA_METADATA = "__saga__";
@@ -1,7 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SAGA_METADATA = exports.EVENTS_HANDLER_METADATA = exports.QUERY_HANDLER_METADATA = exports.COMMAND_HANDLER_METADATA = void 0;
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__';
4
5
  exports.COMMAND_HANDLER_METADATA = '__commandHandler__';
6
+ exports.QUERY_METADATA = '__query__';
5
7
  exports.QUERY_HANDLER_METADATA = '__queryHandler__';
6
- exports.EVENTS_HANDLER_METADATA = '__eventsHandler';
8
+ exports.EVENT_METADATA = '__event__';
9
+ exports.EVENTS_HANDLER_METADATA = '__eventsHandler__';
7
10
  exports.SAGA_METADATA = '__saga__';
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.EventsHandler = void 0;
4
4
  require("reflect-metadata");
5
5
  const constants_1 = require("./constants");
6
+ const uuid_1 = require("uuid");
6
7
  /**
7
8
  * Decorator that marks a class as a Nest event handler. An event handler
8
9
  * handles events executed by your application code.
@@ -15,6 +16,11 @@ const constants_1 = require("./constants");
15
16
  */
16
17
  const EventsHandler = (...events) => {
17
18
  return (target) => {
19
+ events.forEach((event) => {
20
+ if (!Reflect.hasMetadata(constants_1.EVENT_METADATA, event)) {
21
+ Reflect.defineMetadata(constants_1.EVENT_METADATA, { id: (0, uuid_1.v4)() }, event);
22
+ }
23
+ });
18
24
  Reflect.defineMetadata(constants_1.EVENTS_HANDLER_METADATA, events, target);
19
25
  };
20
26
  };
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.QueryHandler = void 0;
4
4
  require("reflect-metadata");
5
5
  const constants_1 = require("./constants");
6
+ const uuid_1 = require("uuid");
6
7
  /**
7
8
  * Decorator that marks a class as a Nest query handler. A query handler
8
9
  * handles queries executed by your application code.
@@ -15,6 +16,9 @@ const constants_1 = require("./constants");
15
16
  */
16
17
  const QueryHandler = (query) => {
17
18
  return (target) => {
19
+ if (!Reflect.hasMetadata(constants_1.QUERY_METADATA, query)) {
20
+ Reflect.defineMetadata(constants_1.QUERY_METADATA, { id: (0, uuid_1.v4)() }, query);
21
+ }
18
22
  Reflect.defineMetadata(constants_1.QUERY_HANDLER_METADATA, query, target);
19
23
  };
20
24
  };
@@ -8,7 +8,7 @@ export declare type EventHandlerType<EventBase extends IEvent = IEvent> = Type<I
8
8
  export declare class EventBus<EventBase extends IEvent = IEvent> extends ObservableBus<EventBase> implements IEventBus<EventBase>, OnModuleDestroy {
9
9
  private readonly commandBus;
10
10
  private readonly moduleRef;
11
- protected getEventName: (event: EventBase) => string;
11
+ protected getEventId: (event: EventBase) => string | null;
12
12
  protected readonly subscriptions: Subscription[];
13
13
  private _publisher;
14
14
  constructor(commandBus: CommandBus, moduleRef: ModuleRef);
@@ -17,12 +17,12 @@ export declare class EventBus<EventBase extends IEvent = IEvent> extends Observa
17
17
  onModuleDestroy(): void;
18
18
  publish<T extends EventBase>(event: T): any;
19
19
  publishAll<T extends EventBase>(events: T[]): any;
20
- bind(handler: IEventHandler<EventBase>, name: string): void;
20
+ bind(handler: IEventHandler<EventBase>, id: string): void;
21
21
  registerSagas(types?: Type<unknown>[]): void;
22
22
  register(handlers?: EventHandlerType<EventBase>[]): void;
23
23
  protected registerHandler(handler: EventHandlerType<EventBase>): void;
24
- protected ofEventName(name: string): Observable<EventBase>;
24
+ protected ofEventId(id: string): Observable<EventBase>;
25
25
  protected registerSaga(saga: ISaga<EventBase>): void;
26
- private reflectEventsNames;
26
+ private reflectEvents;
27
27
  private useDefaultPublisher;
28
28
  }
package/dist/event-bus.js CHANGED
@@ -18,7 +18,7 @@ const util_1 = require("util");
18
18
  const command_bus_1 = require("./command-bus");
19
19
  const constants_1 = require("./decorators/constants");
20
20
  const exceptions_1 = require("./exceptions");
21
- const default_get_event_name_1 = require("./helpers/default-get-event-name");
21
+ const default_get_event_id_1 = require("./helpers/default-get-event-id");
22
22
  const default_pubsub_1 = require("./helpers/default-pubsub");
23
23
  const utils_1 = require("./utils");
24
24
  let EventBus = class EventBus extends utils_1.ObservableBus {
@@ -27,7 +27,7 @@ let EventBus = class EventBus extends utils_1.ObservableBus {
27
27
  this.commandBus = commandBus;
28
28
  this.moduleRef = moduleRef;
29
29
  this.subscriptions = [];
30
- this.getEventName = default_get_event_name_1.defaultGetEventName;
30
+ this.getEventId = default_get_event_id_1.defaultGetEventId;
31
31
  this.useDefaultPublisher();
32
32
  }
33
33
  get publisher() {
@@ -48,8 +48,8 @@ let EventBus = class EventBus extends utils_1.ObservableBus {
48
48
  }
49
49
  return (events || []).map((event) => this._publisher.publish(event));
50
50
  }
51
- bind(handler, name) {
52
- const stream$ = name ? this.ofEventName(name) : this.subject$;
51
+ bind(handler, id) {
52
+ const stream$ = id ? this.ofEventId(id) : this.subject$;
53
53
  const subscription = stream$.subscribe((event) => handler.handle(event));
54
54
  this.subscriptions.push(subscription);
55
55
  }
@@ -74,14 +74,14 @@ let EventBus = class EventBus extends utils_1.ObservableBus {
74
74
  if (!instance) {
75
75
  return;
76
76
  }
77
- const eventsNames = this.reflectEventsNames(handler);
78
- eventsNames.map((event) => this.bind(instance, event.name));
77
+ const events = this.reflectEvents(handler);
78
+ events.map((event) => this.bind(instance, (0, default_get_event_id_1.defaultReflectEventId)(event)));
79
79
  }
80
- ofEventName(name) {
81
- return this.subject$.pipe(operators_1.filter((event) => this.getEventName(event) === name));
80
+ ofEventId(id) {
81
+ return this.subject$.pipe((0, operators_1.filter)((event) => this.getEventId(event) === id));
82
82
  }
83
83
  registerSaga(saga) {
84
- if (!util_1.isFunction(saga)) {
84
+ if (!(0, util_1.isFunction)(saga)) {
85
85
  throw new exceptions_1.InvalidSagaException();
86
86
  }
87
87
  const stream$ = saga(this);
@@ -89,11 +89,11 @@ let EventBus = class EventBus extends utils_1.ObservableBus {
89
89
  throw new exceptions_1.InvalidSagaException();
90
90
  }
91
91
  const subscription = stream$
92
- .pipe(operators_1.filter((e) => !!e))
92
+ .pipe((0, operators_1.filter)((e) => !!e))
93
93
  .subscribe((command) => this.commandBus.execute(command));
94
94
  this.subscriptions.push(subscription);
95
95
  }
96
- reflectEventsNames(handler) {
96
+ reflectEvents(handler) {
97
97
  return Reflect.getMetadata(constants_1.EVENTS_HANDLER_METADATA, handler);
98
98
  }
99
99
  useDefaultPublisher() {
@@ -101,7 +101,7 @@ let EventBus = class EventBus extends utils_1.ObservableBus {
101
101
  }
102
102
  };
103
103
  EventBus = __decorate([
104
- common_1.Injectable(),
104
+ (0, common_1.Injectable)(),
105
105
  __metadata("design:paramtypes", [command_bus_1.CommandBus,
106
106
  core_1.ModuleRef])
107
107
  ], EventBus);
@@ -39,7 +39,7 @@ let EventPublisher = class EventPublisher {
39
39
  }
40
40
  };
41
41
  EventPublisher = __decorate([
42
- common_1.Injectable(),
42
+ (0, common_1.Injectable)(),
43
43
  __metadata("design:paramtypes", [event_bus_1.EventBus])
44
44
  ], EventPublisher);
45
45
  exports.EventPublisher = EventPublisher;
@@ -0,0 +1,9 @@
1
+ import { IEvent } from '../interfaces';
2
+ import { Type } from '@nestjs/common';
3
+ /**
4
+ * Null if the published class is not connected to any handler
5
+ * @param event
6
+ * @returns
7
+ */
8
+ export declare const defaultGetEventId: <EventBase extends IEvent = IEvent>(event: EventBase) => string;
9
+ export declare const defaultReflectEventId: <EventBase extends Type<IEvent> = Type<IEvent>>(event: EventBase) => string;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.defaultReflectEventId = exports.defaultGetEventId = void 0;
4
+ const constants_1 = require("../decorators/constants");
5
+ /**
6
+ * Null if the published class is not connected to any handler
7
+ * @param event
8
+ * @returns
9
+ */
10
+ const defaultGetEventId = (event) => {
11
+ var _a, _b;
12
+ const { constructor } = Object.getPrototypeOf(event);
13
+ return (_b = (_a = Reflect.getMetadata(constants_1.EVENT_METADATA, constructor)) === null || _a === void 0 ? void 0 : _a.id) !== null && _b !== void 0 ? _b : null;
14
+ };
15
+ exports.defaultGetEventId = defaultGetEventId;
16
+ const defaultReflectEventId = (event) => {
17
+ return Reflect.getMetadata(constants_1.EVENT_METADATA, event).id;
18
+ };
19
+ exports.defaultReflectEventId = defaultReflectEventId;
@@ -0,0 +1,3 @@
1
+ export interface CommandMetadata {
2
+ id: string;
3
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,3 @@
1
+ export interface QueryMetadata {
2
+ id: string;
3
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -12,6 +12,6 @@ const operators_1 = require("rxjs/operators");
12
12
  */
13
13
  function ofType(...types) {
14
14
  const isInstanceOf = (event) => !!types.find((classType) => event instanceof classType);
15
- return (source) => source.pipe(operators_1.filter(isInstanceOf));
15
+ return (source) => source.pipe((0, operators_1.filter)(isInstanceOf));
16
16
  }
17
17
  exports.ofType = ofType;
@@ -12,10 +12,10 @@ export declare class QueryBus<QueryBase extends IQuery = IQuery> extends Observa
12
12
  get publisher(): IQueryPublisher<QueryBase>;
13
13
  set publisher(_publisher: IQueryPublisher<QueryBase>);
14
14
  execute<T extends QueryBase, TResult = any>(query: T): Promise<TResult>;
15
- bind<T extends QueryBase, TResult = any>(handler: IQueryHandler<T, TResult>, name: string): void;
15
+ bind<T extends QueryBase, TResult = any>(handler: IQueryHandler<T, TResult>, queryId: string): void;
16
16
  register(handlers?: QueryHandlerType<QueryBase>[]): void;
17
17
  protected registerHandler(handler: QueryHandlerType<QueryBase>): void;
18
- private getQueryName;
19
- private reflectQueryName;
18
+ private getQueryId;
19
+ private reflectQueryId;
20
20
  private useDefaultPublisher;
21
21
  }
package/dist/query-bus.js CHANGED
@@ -42,18 +42,18 @@ let QueryBus = class QueryBus extends observable_bus_1.ObservableBus {
42
42
  }
43
43
  execute(query) {
44
44
  return __awaiter(this, void 0, void 0, function* () {
45
- const queryName = this.getQueryName(query);
46
- const handler = this.handlers.get(queryName);
45
+ const queryId = this.getQueryId(query);
46
+ const handler = this.handlers.get(queryId);
47
47
  if (!handler) {
48
- throw new exceptions_1.QueryHandlerNotFoundException(queryName);
48
+ throw new exceptions_1.QueryHandlerNotFoundException(queryId);
49
49
  }
50
50
  this.subject$.next(query);
51
51
  const result = yield handler.execute(query);
52
52
  return result;
53
53
  });
54
54
  }
55
- bind(handler, name) {
56
- this.handlers.set(name, handler);
55
+ bind(handler, queryId) {
56
+ this.handlers.set(queryId, handler);
57
57
  }
58
58
  register(handlers = []) {
59
59
  handlers.forEach((handler) => this.registerHandler(handler));
@@ -63,25 +63,31 @@ let QueryBus = class QueryBus extends observable_bus_1.ObservableBus {
63
63
  if (!instance) {
64
64
  return;
65
65
  }
66
- const target = this.reflectQueryName(handler);
66
+ const target = this.reflectQueryId(handler);
67
67
  if (!target) {
68
68
  throw new invalid_query_handler_exception_1.InvalidQueryHandlerException();
69
69
  }
70
- this.bind(instance, target.name);
70
+ this.bind(instance, target);
71
71
  }
72
- getQueryName(query) {
73
- const { constructor } = Object.getPrototypeOf(query);
74
- return constructor.name;
72
+ getQueryId(query) {
73
+ const { constructor: queryType } = Object.getPrototypeOf(query);
74
+ const queryMetadata = Reflect.getMetadata(constants_1.QUERY_METADATA, queryType);
75
+ if (!queryMetadata) {
76
+ throw new exceptions_1.QueryHandlerNotFoundException(queryType.name);
77
+ }
78
+ return queryMetadata.id;
75
79
  }
76
- reflectQueryName(handler) {
77
- return Reflect.getMetadata(constants_1.QUERY_HANDLER_METADATA, handler);
80
+ reflectQueryId(handler) {
81
+ const query = Reflect.getMetadata(constants_1.QUERY_HANDLER_METADATA, handler);
82
+ const queryMetadata = Reflect.getMetadata(constants_1.QUERY_METADATA, query);
83
+ return queryMetadata.id;
78
84
  }
79
85
  useDefaultPublisher() {
80
86
  this._publisher = new default_query_pubsub_1.DefaultQueryPubSub(this.subject$);
81
87
  }
82
88
  };
83
89
  QueryBus = __decorate([
84
- common_1.Injectable(),
90
+ (0, common_1.Injectable)(),
85
91
  __metadata("design:paramtypes", [core_1.ModuleRef])
86
92
  ], QueryBus);
87
93
  exports.QueryBus = QueryBus;
@@ -47,7 +47,7 @@ let ExplorerService = class ExplorerService {
47
47
  }
48
48
  };
49
49
  ExplorerService = __decorate([
50
- common_1.Injectable(),
50
+ (0, common_1.Injectable)(),
51
51
  __metadata("design:paramtypes", [modules_container_1.ModulesContainer])
52
52
  ], ExplorerService);
53
53
  exports.ExplorerService = ExplorerService;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nestjs/cqrs",
3
- "version": "8.0.0",
3
+ "version": "8.0.3",
4
4
  "description": "A lightweight CQRS module for Nest framework (node.js)",
5
5
  "license": "MIT",
6
6
  "url": "https://github.com/nestjs/cqrs#readme",
@@ -16,27 +16,30 @@
16
16
  "prerelease": "npm run build",
17
17
  "release": "release-it"
18
18
  },
19
+ "dependencies": {
20
+ "uuid": "8.3.2"
21
+ },
19
22
  "devDependencies": {
20
- "@commitlint/cli": "12.1.4",
21
- "@commitlint/config-angular": "12.1.4",
22
- "@nestjs/common": "7.6.18",
23
- "@nestjs/core": "7.6.18",
24
- "@types/node": "16.0.1",
25
- "@types/jest": "26.0.24",
26
- "@typescript-eslint/eslint-plugin": "4.28.2",
27
- "@typescript-eslint/parser": "4.28.2",
28
- "eslint": "7.30.0",
29
- "eslint-config-prettier": "8.3.0",
30
- "eslint-plugin-import": "2.23.4",
31
- "husky": "7.0.1",
32
- "jest": "27.0.6",
33
- "lint-staged": "11.0.0",
34
- "prettier": "2.3.2",
23
+ "@commitlint/cli": "16.2.1",
24
+ "@commitlint/config-angular": "16.2.1",
25
+ "@nestjs/common": "8.4.0",
26
+ "@nestjs/core": "8.4.0",
27
+ "@types/node": "16.11.26",
28
+ "@types/jest": "27.4.1",
29
+ "@typescript-eslint/eslint-plugin": "5.13.0",
30
+ "@typescript-eslint/parser": "5.13.0",
31
+ "eslint": "8.10.0",
32
+ "eslint-config-prettier": "8.4.0",
33
+ "eslint-plugin-import": "2.25.4",
34
+ "husky": "7.0.4",
35
+ "jest": "27.5.1",
36
+ "lint-staged": "12.3.4",
37
+ "prettier": "2.5.1",
35
38
  "reflect-metadata": "0.1.13",
36
- "release-it": "14.10.0",
37
- "rxjs": "6.6.7",
38
- "ts-jest": "27.0.3",
39
- "typescript": "4.3.5"
39
+ "release-it": "14.12.5",
40
+ "rxjs": "7.5.4",
41
+ "ts-jest": "27.1.3",
42
+ "typescript": "4.6.2"
40
43
  },
41
44
  "peerDependencies": {
42
45
  "@nestjs/common": "^8.0.0",
@@ -1,46 +0,0 @@
1
- <!--
2
- PLEASE HELP US PROCESS GITHUB ISSUES FASTER BY PROVIDING THE FOLLOWING INFORMATION.
3
-
4
- ISSUES MISSING IMPORTANT INFORMATION MAY BE CLOSED WITHOUT INVESTIGATION.
5
- -->
6
-
7
- ## I'm submitting a...
8
- <!--
9
- Please search GitHub for a similar issue or PR before submitting.
10
- Check one of the following options with "x" -->
11
- <pre><code>
12
- [ ] Regression <!--(a behavior that used to work and stopped working in a new release)-->
13
- [ ] Bug report
14
- [ ] Feature request
15
- [ ] Documentation issue or request
16
- [ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
17
- </code></pre>
18
-
19
- ## Current behavior
20
- <!-- Describe how the issue manifests. -->
21
-
22
-
23
- ## Expected behavior
24
- <!-- Describe what the desired behavior would be. -->
25
-
26
-
27
- ## Minimal reproduction of the problem with instructions
28
- <!-- Please share a repo, a gist, or step-by-step instructions. -->
29
-
30
- ## What is the motivation / use case for changing the behavior?
31
- <!-- Describe the motivation or the concrete use case. -->
32
-
33
-
34
- ## Environment
35
-
36
- <pre><code>
37
- Nest version: X.Y.Z
38
- <!-- Check whether this is still an issue in the most recent Nest version -->
39
-
40
- For Tooling issues:
41
- - Node version: XX <!-- run `node --version` -->
42
- - Platform: <!-- Mac, Linux, Windows -->
43
-
44
- Others:
45
- <!-- Anything else relevant? Operating system version, IDE, package manager, ... -->
46
- </code></pre>