@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.
- 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 +52 -52
- package/dist/event-publisher.d.ts +8 -7
- package/dist/event-publisher.js +17 -18
- 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 +63 -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 +75 -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 +9 -9
- 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/aggregate-commit-context.spec.ts +243 -0
- 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 +55 -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
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Query } from '../../classes/query';
|
|
2
|
-
import { AsyncContext } from '../../scopes/async.context';
|
|
3
|
-
import { IQuery } from './query.interface';
|
|
1
|
+
import { Query } from '../../classes/query.js';
|
|
2
|
+
import { AsyncContext } from '../../scopes/async.context.js';
|
|
3
|
+
import { IQuery } from './query.interface.js';
|
|
4
4
|
/**
|
|
5
5
|
* Represents a query bus.
|
|
6
6
|
*
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { Observable } from 'rxjs';
|
|
2
|
-
import { ICommand } from './commands/command.interface';
|
|
3
|
-
import { IEvent } from './events/event.interface';
|
|
2
|
+
import { ICommand } from './commands/command.interface.js';
|
|
3
|
+
import { IEvent } from './events/event.interface.js';
|
|
4
4
|
export type ISaga<EventBase extends IEvent = IEvent, CommandBase extends ICommand = ICommand> = (events$: Observable<EventBase>) => Observable<CommandBase>;
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './with-aggregate-root.mixin.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './with-aggregate-root.mixin.js';
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { IEvent, IAggregateRoot } from '../interfaces/index.js';
|
|
2
|
+
export type AbstractConstructor<T = {}> = abstract new (...args: any[]) => T;
|
|
3
|
+
/**
|
|
4
|
+
* Adds the Aggregate Root functionality to a base class.
|
|
5
|
+
* An aggregate root is an entity that represents a meaningful concept in the domain.
|
|
6
|
+
* It is the root of an aggregate, which is a cluster of domain objects that can be treated as a single unit.
|
|
7
|
+
*
|
|
8
|
+
* @template EventBase The base type of the events.
|
|
9
|
+
* @template TBase The base class type.
|
|
10
|
+
*/
|
|
11
|
+
export declare function WithAggregateRoot<EventBase extends IEvent = IEvent, TBase extends AbstractConstructor = AbstractConstructor>(Base: TBase): abstract new <T extends IEvent = EventBase>(...args: any[]) => IAggregateRoot<T> & InstanceType<TBase>;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
const INTERNAL_EVENTS = Symbol();
|
|
2
|
+
const IS_AUTO_COMMIT_ENABLED = Symbol();
|
|
3
|
+
/**
|
|
4
|
+
* Adds the Aggregate Root functionality to a base class.
|
|
5
|
+
* An aggregate root is an entity that represents a meaningful concept in the domain.
|
|
6
|
+
* It is the root of an aggregate, which is a cluster of domain objects that can be treated as a single unit.
|
|
7
|
+
*
|
|
8
|
+
* @template EventBase The base type of the events.
|
|
9
|
+
* @template TBase The base class type.
|
|
10
|
+
*/
|
|
11
|
+
export function WithAggregateRoot(Base) {
|
|
12
|
+
var _a, _b;
|
|
13
|
+
class AggregateRoot extends Base {
|
|
14
|
+
constructor() {
|
|
15
|
+
super(...arguments);
|
|
16
|
+
this[_a] = false;
|
|
17
|
+
this[_b] = [];
|
|
18
|
+
}
|
|
19
|
+
set autoCommit(value) {
|
|
20
|
+
this[IS_AUTO_COMMIT_ENABLED] = value;
|
|
21
|
+
}
|
|
22
|
+
get autoCommit() {
|
|
23
|
+
return this[IS_AUTO_COMMIT_ENABLED];
|
|
24
|
+
}
|
|
25
|
+
publish(event, dispatcherContext) { }
|
|
26
|
+
publishAll(events, dispatcherContext) { }
|
|
27
|
+
commit(dispatcherContext) {
|
|
28
|
+
const events = this[INTERNAL_EVENTS];
|
|
29
|
+
// With a dispatcher context, the caller awaits the publisher, which may read
|
|
30
|
+
// the events after it returns: hand it a copy, as the events are cleared below.
|
|
31
|
+
const result = dispatcherContext === undefined
|
|
32
|
+
? this.publishAll(events)
|
|
33
|
+
: this.publishAll([...events], dispatcherContext);
|
|
34
|
+
// Cleared once the publisher has been called, not once it settles (as before):
|
|
35
|
+
// a caller that doesn't await must not publish the same events twice.
|
|
36
|
+
events.length = 0;
|
|
37
|
+
return result;
|
|
38
|
+
}
|
|
39
|
+
uncommit() {
|
|
40
|
+
this[INTERNAL_EVENTS].length = 0;
|
|
41
|
+
}
|
|
42
|
+
getUncommittedEvents() {
|
|
43
|
+
return this[INTERNAL_EVENTS];
|
|
44
|
+
}
|
|
45
|
+
loadFromHistory(history) {
|
|
46
|
+
history.forEach((event) => this.apply(event, true));
|
|
47
|
+
}
|
|
48
|
+
apply(event, optionsOrIsFromHistory = {}) {
|
|
49
|
+
const isFromHistory = (typeof optionsOrIsFromHistory === 'boolean'
|
|
50
|
+
? optionsOrIsFromHistory
|
|
51
|
+
: optionsOrIsFromHistory.fromHistory) ?? false;
|
|
52
|
+
const skipHandler = typeof optionsOrIsFromHistory === 'boolean'
|
|
53
|
+
? false
|
|
54
|
+
: optionsOrIsFromHistory.skipHandler;
|
|
55
|
+
if (!isFromHistory && !this.autoCommit) {
|
|
56
|
+
this[INTERNAL_EVENTS].push(event);
|
|
57
|
+
}
|
|
58
|
+
this.autoCommit && this.publish(event);
|
|
59
|
+
if (!skipHandler) {
|
|
60
|
+
const handler = this.getEventHandler(event);
|
|
61
|
+
handler && handler.call(this, event);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
getEventHandler(event) {
|
|
65
|
+
const handler = `on${this.getEventName(event)}`;
|
|
66
|
+
return this[handler];
|
|
67
|
+
}
|
|
68
|
+
getEventName(event) {
|
|
69
|
+
const { constructor } = Object.getPrototypeOf(event);
|
|
70
|
+
return constructor.name;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
_a = IS_AUTO_COMMIT_ENABLED, _b = INTERNAL_EVENTS;
|
|
74
|
+
return AggregateRoot;
|
|
75
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from './of-type';
|
|
1
|
+
export * from './of-type.js';
|
package/dist/operators/index.js
CHANGED
|
@@ -1,17 +1 @@
|
|
|
1
|
-
|
|
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("./of-type"), exports);
|
|
1
|
+
export * from './of-type.js';
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Type } from '@nestjs/common';
|
|
2
2
|
import { Observable } from 'rxjs';
|
|
3
|
-
import { IEvent } from '../interfaces';
|
|
3
|
+
import { IEvent } from '../interfaces/index.js';
|
|
4
4
|
/**
|
|
5
5
|
* Filter values depending on their instance type (comparison is made
|
|
6
6
|
* using native `instanceof`).
|
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ofType = ofType;
|
|
4
|
-
const operators_1 = require("rxjs/operators");
|
|
1
|
+
import { filter } from 'rxjs/operators';
|
|
5
2
|
/**
|
|
6
3
|
* Filter values depending on their instance type (comparison is made
|
|
7
4
|
* using native `instanceof`).
|
|
@@ -10,7 +7,7 @@ const operators_1 = require("rxjs/operators");
|
|
|
10
7
|
*
|
|
11
8
|
* @return A stream only emitting the filtered instances.
|
|
12
9
|
*/
|
|
13
|
-
function ofType(...types) {
|
|
10
|
+
export function ofType(...types) {
|
|
14
11
|
const isInstanceOf = (event) => !!types.find((classType) => event instanceof classType);
|
|
15
|
-
return (source) => source.pipe(
|
|
12
|
+
return (source) => source.pipe(filter(isInstanceOf));
|
|
16
13
|
}
|
package/dist/query-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 { Query } from './classes/query';
|
|
6
|
-
import { CqrsModuleOptions, IQuery, IQueryBus, IQueryHandler, IQueryPublisher, IQueryResult } from './interfaces';
|
|
7
|
-
import { AsyncContext } from './scopes';
|
|
8
|
-
import { ObservableBus } from './utils/observable-bus';
|
|
5
|
+
import { Query } from './classes/query.js';
|
|
6
|
+
import { CqrsModuleOptions, IQuery, IQueryBus, IQueryHandler, IQueryPublisher, IQueryResult } from './interfaces/index.js';
|
|
7
|
+
import { AsyncContext } from './scopes/index.js';
|
|
8
|
+
import { ObservableBus } from './utils/observable-bus.js';
|
|
9
9
|
export type QueryHandlerType<QueryBase extends IQuery = IQuery, QueryResultBase extends IQueryResult = IQueryResult> = Type<IQueryHandler<QueryBase, QueryResultBase>>;
|
|
10
10
|
/**
|
|
11
11
|
* @publicApi
|
package/dist/query-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 QueryBus_1;
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
const scopes_1 = require("./scopes");
|
|
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 { QUERY_HANDLER_METADATA, QUERY_METADATA, } from './decorators/constants.js';
|
|
19
|
+
import { QueryHandlerNotFoundException } from './exceptions/index.js';
|
|
20
|
+
import { InvalidQueryHandlerException } from './exceptions/invalid-query-handler.exception.js';
|
|
21
|
+
import { DefaultQueryPubSub } from './helpers/default-query-pubsub.js';
|
|
22
|
+
import { AsyncContext } from './scopes/index.js';
|
|
23
|
+
import { ObservableBus } from './utils/observable-bus.js';
|
|
27
24
|
/**
|
|
28
25
|
* @publicApi
|
|
29
26
|
*/
|
|
30
|
-
let QueryBus = QueryBus_1 = class QueryBus extends
|
|
27
|
+
let QueryBus = QueryBus_1 = class QueryBus 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(QueryBus_1.name);
|
|
36
33
|
this.handlers = new Map();
|
|
37
34
|
if (this.options?.queryPublisher) {
|
|
38
35
|
this._publisher = this.options.queryPublisher;
|
|
@@ -64,7 +61,7 @@ let QueryBus = QueryBus_1 = class QueryBus extends observable_bus_1.ObservableBu
|
|
|
64
61
|
const handler = this.handlers.get(queryId);
|
|
65
62
|
if (!handler) {
|
|
66
63
|
const queryName = this.getQueryName(query);
|
|
67
|
-
throw new
|
|
64
|
+
throw new QueryHandlerNotFoundException(queryName);
|
|
68
65
|
}
|
|
69
66
|
this._publisher.publish(query);
|
|
70
67
|
return (await handler(query, asyncContext));
|
|
@@ -73,13 +70,13 @@ let QueryBus = QueryBus_1 = class QueryBus extends observable_bus_1.ObservableBu
|
|
|
73
70
|
if (handler.isDependencyTreeStatic()) {
|
|
74
71
|
const instance = handler.instance;
|
|
75
72
|
if (!instance.execute) {
|
|
76
|
-
throw new
|
|
73
|
+
throw new InvalidQueryHandlerException();
|
|
77
74
|
}
|
|
78
75
|
this.handlers.set(queryId, (query) => instance.execute(query));
|
|
79
76
|
return;
|
|
80
77
|
}
|
|
81
78
|
this.handlers.set(queryId, async (query, context) => {
|
|
82
|
-
context ??=
|
|
79
|
+
context ??= AsyncContext.of(query) ?? new AsyncContext();
|
|
83
80
|
this.moduleRef.registerRequestByContextId(context, context.id);
|
|
84
81
|
const instance = await this.moduleRef.resolve(handler.metatype, context.id, {
|
|
85
82
|
strict: false,
|
|
@@ -91,10 +88,10 @@ let QueryBus = QueryBus_1 = class QueryBus extends observable_bus_1.ObservableBu
|
|
|
91
88
|
handlers.forEach((handler) => this.registerHandler(handler));
|
|
92
89
|
}
|
|
93
90
|
registerHandler(handler) {
|
|
94
|
-
const typeRef = handler.metatype;
|
|
91
|
+
const typeRef = (handler.inject ? handler.instance?.constructor : handler.metatype);
|
|
95
92
|
const target = this.reflectQueryId(typeRef);
|
|
96
93
|
if (!target) {
|
|
97
|
-
throw new
|
|
94
|
+
throw new InvalidQueryHandlerException();
|
|
98
95
|
}
|
|
99
96
|
if (this.handlers.has(target)) {
|
|
100
97
|
this.logger.warn(`Query handler [${typeRef.name}] is already registered. Overriding previously registered handler.`);
|
|
@@ -103,29 +100,29 @@ let QueryBus = QueryBus_1 = class QueryBus extends observable_bus_1.ObservableBu
|
|
|
103
100
|
}
|
|
104
101
|
getQueryId(query) {
|
|
105
102
|
const { constructor: queryType } = Object.getPrototypeOf(query);
|
|
106
|
-
const queryMetadata = Reflect.getMetadata(
|
|
103
|
+
const queryMetadata = Reflect.getMetadata(QUERY_METADATA, queryType);
|
|
107
104
|
if (!queryMetadata) {
|
|
108
|
-
throw new
|
|
105
|
+
throw new QueryHandlerNotFoundException(queryType.name);
|
|
109
106
|
}
|
|
110
107
|
return queryMetadata.id;
|
|
111
108
|
}
|
|
112
109
|
reflectQueryId(handler) {
|
|
113
|
-
const query = Reflect.getMetadata(
|
|
114
|
-
const queryMetadata = Reflect.getMetadata(
|
|
110
|
+
const query = Reflect.getMetadata(QUERY_HANDLER_METADATA, handler);
|
|
111
|
+
const queryMetadata = Reflect.getMetadata(QUERY_METADATA, query);
|
|
115
112
|
return queryMetadata.id;
|
|
116
113
|
}
|
|
117
114
|
useDefaultPublisher() {
|
|
118
|
-
this._publisher = new
|
|
115
|
+
this._publisher = new DefaultQueryPubSub(this.subject$);
|
|
119
116
|
}
|
|
120
117
|
getQueryName(query) {
|
|
121
118
|
const { constructor } = Object.getPrototypeOf(query);
|
|
122
119
|
return constructor.name;
|
|
123
120
|
}
|
|
124
121
|
};
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
(
|
|
128
|
-
__param(1, (
|
|
129
|
-
|
|
130
|
-
__metadata("design:paramtypes", [core_1.ModuleRef, Object])
|
|
122
|
+
QueryBus = QueryBus_1 = __decorate([
|
|
123
|
+
Injectable(),
|
|
124
|
+
__param(1, Optional()),
|
|
125
|
+
__param(1, Inject(CQRS_MODULE_OPTIONS)),
|
|
126
|
+
__metadata("design:paramtypes", [ModuleRef, Object])
|
|
131
127
|
], QueryBus);
|
|
128
|
+
export { QueryBus };
|
|
@@ -1,21 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.AsyncContext = exports.ASYNC_CONTEXT_ATTRIBUTE = void 0;
|
|
4
|
-
const core_1 = require("@nestjs/core");
|
|
5
|
-
exports.ASYNC_CONTEXT_ATTRIBUTE = Symbol('ASYNC_CONTEXT_ATTRIBUTE');
|
|
1
|
+
import { ContextIdFactory } from '@nestjs/core';
|
|
2
|
+
export const ASYNC_CONTEXT_ATTRIBUTE = Symbol('ASYNC_CONTEXT_ATTRIBUTE');
|
|
6
3
|
/**
|
|
7
4
|
* Represents the context of an asynchronous operation.
|
|
8
5
|
*/
|
|
9
|
-
class AsyncContext {
|
|
6
|
+
export class AsyncContext {
|
|
10
7
|
constructor() {
|
|
11
|
-
this.id =
|
|
8
|
+
this.id = ContextIdFactory.create();
|
|
12
9
|
}
|
|
13
10
|
/**
|
|
14
11
|
* Attaches the context to an object.
|
|
15
12
|
* @param target The object to attach the context to.
|
|
16
13
|
*/
|
|
17
14
|
attachTo(target) {
|
|
18
|
-
Object.defineProperty(target,
|
|
15
|
+
Object.defineProperty(target, ASYNC_CONTEXT_ATTRIBUTE, {
|
|
19
16
|
value: this,
|
|
20
17
|
enumerable: false,
|
|
21
18
|
});
|
|
@@ -26,7 +23,7 @@ class AsyncContext {
|
|
|
26
23
|
* @returns "true" if the target is attached to an async context, "false" otherwise.
|
|
27
24
|
*/
|
|
28
25
|
static isAttached(target) {
|
|
29
|
-
return !!target[
|
|
26
|
+
return !!target[ASYNC_CONTEXT_ATTRIBUTE];
|
|
30
27
|
}
|
|
31
28
|
/**
|
|
32
29
|
* Merges the context of an asynchronous operation from a given command, query, or event to another object.
|
|
@@ -37,11 +34,11 @@ class AsyncContext {
|
|
|
37
34
|
if (!from || !to) {
|
|
38
35
|
return;
|
|
39
36
|
}
|
|
40
|
-
const fromContext = from[
|
|
37
|
+
const fromContext = from[ASYNC_CONTEXT_ATTRIBUTE];
|
|
41
38
|
if (!fromContext) {
|
|
42
39
|
return;
|
|
43
40
|
}
|
|
44
|
-
Object.defineProperty(to,
|
|
41
|
+
Object.defineProperty(to, ASYNC_CONTEXT_ATTRIBUTE, {
|
|
45
42
|
value: fromContext,
|
|
46
43
|
enumerable: false,
|
|
47
44
|
});
|
|
@@ -52,7 +49,6 @@ class AsyncContext {
|
|
|
52
49
|
* @returns An "AsyncContext" instance or "undefined" if the context is not found.
|
|
53
50
|
*/
|
|
54
51
|
static of(target) {
|
|
55
|
-
return target[
|
|
52
|
+
return target[ASYNC_CONTEXT_ATTRIBUTE];
|
|
56
53
|
}
|
|
57
54
|
}
|
|
58
|
-
exports.AsyncContext = AsyncContext;
|
package/dist/scopes/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from './async.context';
|
|
1
|
+
export * from './async.context.js';
|
package/dist/scopes/index.js
CHANGED
|
@@ -1,17 +1 @@
|
|
|
1
|
-
|
|
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("./async.context"), exports);
|
|
1
|
+
export * from './async.context.js';
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper';
|
|
2
|
-
import { Module } from '@nestjs/core/injector/module';
|
|
3
|
-
import { ModulesContainer } from '@nestjs/core/injector/modules-container';
|
|
4
|
-
import { IEvent } from '../interfaces';
|
|
5
|
-
import { ProvidersIntrospectionResult } from '../interfaces/providers-introspection-result.interface';
|
|
1
|
+
import { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper.js';
|
|
2
|
+
import { Module } from '@nestjs/core/injector/module.js';
|
|
3
|
+
import { ModulesContainer } from '@nestjs/core/injector/modules-container.js';
|
|
4
|
+
import { IEvent } from '../interfaces/index.js';
|
|
5
|
+
import { ProvidersIntrospectionResult } from '../interfaces/providers-introspection-result.interface.js';
|
|
6
6
|
export declare class ExplorerService<EventBase extends IEvent = IEvent> {
|
|
7
7
|
private readonly modulesContainer;
|
|
8
8
|
constructor(modulesContainer: ModulesContainer);
|
|
@@ -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);
|
|
@@ -8,21 +7,19 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
8
7
|
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
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const modules_container_1 = require("@nestjs/core/injector/modules-container");
|
|
15
|
-
const constants_1 = require("../decorators/constants");
|
|
10
|
+
import { Injectable } from '@nestjs/common';
|
|
11
|
+
import { ModulesContainer } from '@nestjs/core/injector/modules-container.js';
|
|
12
|
+
import { COMMAND_HANDLER_METADATA, EVENTS_HANDLER_METADATA, QUERY_HANDLER_METADATA, SAGA_METADATA, } from '../decorators/constants.js';
|
|
16
13
|
let ExplorerService = class ExplorerService {
|
|
17
14
|
constructor(modulesContainer) {
|
|
18
15
|
this.modulesContainer = modulesContainer;
|
|
19
16
|
}
|
|
20
17
|
explore() {
|
|
21
18
|
const modules = [...this.modulesContainer.values()];
|
|
22
|
-
const commands = this.flatMap(modules, (instance) => this.filterByMetadataKey(instance,
|
|
23
|
-
const queries = this.flatMap(modules, (instance) => this.filterByMetadataKey(instance,
|
|
24
|
-
const events = this.flatMap(modules, (instance) => this.filterByMetadataKey(instance,
|
|
25
|
-
const sagas = this.flatMap(modules, (instance) => this.filterByMetadataKey(instance,
|
|
19
|
+
const commands = this.flatMap(modules, (instance) => this.filterByMetadataKey(instance, COMMAND_HANDLER_METADATA));
|
|
20
|
+
const queries = this.flatMap(modules, (instance) => this.filterByMetadataKey(instance, QUERY_HANDLER_METADATA));
|
|
21
|
+
const events = this.flatMap(modules, (instance) => this.filterByMetadataKey(instance, EVENTS_HANDLER_METADATA));
|
|
22
|
+
const sagas = this.flatMap(modules, (instance) => this.filterByMetadataKey(instance, SAGA_METADATA));
|
|
26
23
|
return { commands, queries, events, sagas };
|
|
27
24
|
}
|
|
28
25
|
flatMap(modules, callback) {
|
|
@@ -32,22 +29,19 @@ let ExplorerService = class ExplorerService {
|
|
|
32
29
|
return items.filter((item) => !!item);
|
|
33
30
|
}
|
|
34
31
|
filterByMetadataKey(wrapper, metadataKey) {
|
|
35
|
-
const
|
|
36
|
-
if (!
|
|
32
|
+
const clsRef = wrapper.instance?.constructor ?? wrapper.metatype;
|
|
33
|
+
if (!clsRef) {
|
|
37
34
|
return;
|
|
38
35
|
}
|
|
39
|
-
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
const metadata = Reflect.getMetadata(metadataKey, instance.constructor);
|
|
36
|
+
const metadata = Reflect.getMetadata(metadataKey, clsRef);
|
|
43
37
|
if (!metadata) {
|
|
44
38
|
return;
|
|
45
39
|
}
|
|
46
40
|
return wrapper;
|
|
47
41
|
}
|
|
48
42
|
};
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
(
|
|
52
|
-
__metadata("design:paramtypes", [modules_container_1.ModulesContainer])
|
|
43
|
+
ExplorerService = __decorate([
|
|
44
|
+
Injectable(),
|
|
45
|
+
__metadata("design:paramtypes", [ModulesContainer])
|
|
53
46
|
], ExplorerService);
|
|
47
|
+
export { ExplorerService };
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Type } from '@nestjs/common';
|
|
2
|
-
import {
|
|
3
|
-
import { EventBus } from '../event-bus';
|
|
2
|
+
import { IAggregateRoot } from '../interfaces/index.js';
|
|
3
|
+
import { EventBus } from '../event-bus.js';
|
|
4
4
|
export declare class AggregateRootStorage {
|
|
5
5
|
private static storage;
|
|
6
|
-
static add(type: Type<
|
|
6
|
+
static add(type: Type<IAggregateRoot>): void;
|
|
7
7
|
static mergeContext(eventBus: EventBus): void;
|
|
8
8
|
}
|
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.AggregateRootStorage = void 0;
|
|
4
|
-
class AggregateRootStorage {
|
|
1
|
+
export class AggregateRootStorage {
|
|
5
2
|
static add(type) {
|
|
6
3
|
this.storage.push(type);
|
|
7
4
|
}
|
|
8
5
|
static mergeContext(eventBus) {
|
|
9
6
|
for (const item of this.storage) {
|
|
10
|
-
item.prototype.publish = function (event) {
|
|
11
|
-
|
|
7
|
+
item.prototype.publish = function (event, dispatcherContext) {
|
|
8
|
+
return dispatcherContext === undefined
|
|
9
|
+
? eventBus.publish(event)
|
|
10
|
+
: eventBus.publish(event, dispatcherContext);
|
|
12
11
|
};
|
|
13
|
-
item.prototype.publishAll = function (events) {
|
|
14
|
-
|
|
12
|
+
item.prototype.publishAll = function (events, dispatcherContext) {
|
|
13
|
+
return dispatcherContext === undefined
|
|
14
|
+
? eventBus.publishAll(events)
|
|
15
|
+
: eventBus.publishAll(events, dispatcherContext);
|
|
15
16
|
};
|
|
16
17
|
}
|
|
17
18
|
this.storage = [];
|
|
18
19
|
}
|
|
19
20
|
}
|
|
20
|
-
exports.AggregateRootStorage = AggregateRootStorage;
|
|
21
21
|
AggregateRootStorage.storage = [];
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Type } from '@nestjs/common';
|
|
2
2
|
import 'reflect-metadata';
|
|
3
3
|
import { Observable } from 'rxjs';
|
|
4
|
-
import { CqrsModuleOptions, ICommand, IEvent, IUnhandledExceptionPublisher, UnhandledExceptionInfo } from './interfaces';
|
|
5
|
-
import { ObservableBus } from './utils/observable-bus';
|
|
4
|
+
import { CqrsModuleOptions, ICommand, IEvent, IUnhandledExceptionPublisher, UnhandledExceptionInfo } from './interfaces/index.js';
|
|
5
|
+
import { ObservableBus } from './utils/observable-bus.js';
|
|
6
6
|
/**
|
|
7
7
|
* A bus that publishes unhandled exceptions.
|
|
8
8
|
* @template Cause The type of the cause of the exception.
|