@rolandsall24/nest-mediator 0.5.2 → 0.7.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/README.md +597 -40
- package/dist/lib/decorators/event-criticality.decorator.d.ts +61 -0
- package/dist/lib/decorators/event-criticality.decorator.d.ts.map +1 -0
- package/dist/lib/decorators/event-criticality.decorator.js +71 -0
- package/dist/lib/decorators/event-criticality.decorator.js.map +1 -0
- package/dist/lib/decorators/event-handler.decorator.d.ts +21 -0
- package/dist/lib/decorators/event-handler.decorator.d.ts.map +1 -0
- package/dist/lib/decorators/event-handler.decorator.js +27 -0
- package/dist/lib/decorators/event-handler.decorator.js.map +1 -0
- package/dist/lib/decorators/index.d.ts +2 -0
- package/dist/lib/decorators/index.d.ts.map +1 -1
- package/dist/lib/decorators/index.js +2 -0
- package/dist/lib/decorators/index.js.map +1 -1
- package/dist/lib/decorators/pipeline-behavior.decorator.d.ts +49 -0
- package/dist/lib/decorators/pipeline-behavior.decorator.d.ts.map +1 -1
- package/dist/lib/decorators/pipeline-behavior.decorator.js +56 -1
- package/dist/lib/decorators/pipeline-behavior.decorator.js.map +1 -1
- package/dist/lib/interfaces/command-bus.interface.d.ts +25 -0
- package/dist/lib/interfaces/command-bus.interface.d.ts.map +1 -0
- package/dist/lib/interfaces/command-bus.interface.js +3 -0
- package/dist/lib/interfaces/command-bus.interface.js.map +1 -0
- package/dist/lib/interfaces/event-bus.interface.d.ts +35 -0
- package/dist/lib/interfaces/event-bus.interface.d.ts.map +1 -0
- package/dist/lib/interfaces/event-bus.interface.js +3 -0
- package/dist/lib/interfaces/event-bus.interface.js.map +1 -0
- package/dist/lib/interfaces/event-consumer.interface.d.ts +64 -0
- package/dist/lib/interfaces/event-consumer.interface.d.ts.map +1 -0
- package/dist/lib/interfaces/event-consumer.interface.js +3 -0
- package/dist/lib/interfaces/event-consumer.interface.js.map +1 -0
- package/dist/lib/interfaces/event-criticality.interface.d.ts +28 -0
- package/dist/lib/interfaces/event-criticality.interface.d.ts.map +1 -0
- package/dist/lib/interfaces/event-criticality.interface.js +25 -0
- package/dist/lib/interfaces/event-criticality.interface.js.map +1 -0
- package/dist/lib/interfaces/event-handler.interface.d.ts +24 -0
- package/dist/lib/interfaces/event-handler.interface.d.ts.map +1 -0
- package/dist/lib/interfaces/event-handler.interface.js +3 -0
- package/dist/lib/interfaces/event-handler.interface.js.map +1 -0
- package/dist/lib/interfaces/event.interface.d.ts +30 -0
- package/dist/lib/interfaces/event.interface.d.ts.map +1 -0
- package/dist/lib/interfaces/event.interface.js +3 -0
- package/dist/lib/interfaces/event.interface.js.map +1 -0
- package/dist/lib/interfaces/index.d.ts +7 -0
- package/dist/lib/interfaces/index.d.ts.map +1 -1
- package/dist/lib/interfaces/index.js +7 -0
- package/dist/lib/interfaces/index.js.map +1 -1
- package/dist/lib/interfaces/mediator.interface.d.ts +14 -0
- package/dist/lib/interfaces/mediator.interface.d.ts.map +1 -0
- package/dist/lib/interfaces/mediator.interface.js +3 -0
- package/dist/lib/interfaces/mediator.interface.js.map +1 -0
- package/dist/lib/interfaces/query-bus.interface.d.ts +26 -0
- package/dist/lib/interfaces/query-bus.interface.d.ts.map +1 -0
- package/dist/lib/interfaces/query-bus.interface.js +3 -0
- package/dist/lib/interfaces/query-bus.interface.js.map +1 -0
- package/dist/lib/nest-mediator.module.d.ts.map +1 -1
- package/dist/lib/nest-mediator.module.js +37 -2
- package/dist/lib/nest-mediator.module.js.map +1 -1
- package/dist/lib/services/command.bus.d.ts +30 -0
- package/dist/lib/services/command.bus.d.ts.map +1 -0
- package/dist/lib/services/command.bus.js +69 -0
- package/dist/lib/services/command.bus.js.map +1 -0
- package/dist/lib/services/event.bus.d.ts +61 -0
- package/dist/lib/services/event.bus.d.ts.map +1 -0
- package/dist/lib/services/event.bus.js +176 -0
- package/dist/lib/services/event.bus.js.map +1 -0
- package/dist/lib/services/mediator.bus.d.ts +50 -31
- package/dist/lib/services/mediator.bus.d.ts.map +1 -1
- package/dist/lib/services/mediator.bus.js +60 -97
- package/dist/lib/services/mediator.bus.js.map +1 -1
- package/dist/lib/services/pipeline.orchestrator.d.ts +46 -0
- package/dist/lib/services/pipeline.orchestrator.d.ts.map +1 -0
- package/dist/lib/services/pipeline.orchestrator.js +87 -0
- package/dist/lib/services/pipeline.orchestrator.js.map +1 -0
- package/dist/lib/services/query.bus.d.ts +31 -0
- package/dist/lib/services/query.bus.d.ts.map +1 -0
- package/dist/lib/services/query.bus.js +68 -0
- package/dist/lib/services/query.bus.js.map +1 -0
- package/package.json +1 -1
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
import { Type } from '@nestjs/common';
|
|
2
|
-
import {
|
|
3
|
-
import { ICommand, ICommandHandler, IQuery, IQueryHandler } from '../interfaces/index.js';
|
|
2
|
+
import { ICommand, ICommandHandler, IQuery, IQueryHandler, IEvent, IEventConsumer, IMediator, EventPublishResult, EventCriticalityMetadata } from '../interfaces/index.js';
|
|
4
3
|
import { IPipelineBehavior, PipelineBehaviorOptions } from '../interfaces/pipeline-behavior.interface.js';
|
|
4
|
+
import { CommandBus } from './command.bus.js';
|
|
5
|
+
import { QueryBus } from './query.bus.js';
|
|
6
|
+
import { EventBus } from './event.bus.js';
|
|
7
|
+
import { PipelineOrchestrator } from './pipeline.orchestrator.js';
|
|
5
8
|
/**
|
|
6
|
-
* Central mediator bus for dispatching commands and
|
|
7
|
-
*
|
|
9
|
+
* Central mediator bus for dispatching commands, queries, and events.
|
|
10
|
+
* Acts as a facade delegating to specialized buses.
|
|
8
11
|
*
|
|
9
12
|
* @example
|
|
10
13
|
* ```typescript
|
|
11
|
-
* // In a controller
|
|
12
14
|
* @Controller('users')
|
|
13
15
|
* export class UserController {
|
|
14
16
|
* constructor(private readonly mediator: MediatorBus) {}
|
|
@@ -25,14 +27,29 @@ import { IPipelineBehavior, PipelineBehaviorOptions } from '../interfaces/pipeli
|
|
|
25
27
|
* }
|
|
26
28
|
* ```
|
|
27
29
|
*/
|
|
28
|
-
export declare class MediatorBus {
|
|
29
|
-
private readonly
|
|
30
|
-
private readonly
|
|
31
|
-
private readonly
|
|
32
|
-
private
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
30
|
+
export declare class MediatorBus implements IMediator {
|
|
31
|
+
private readonly commandBus;
|
|
32
|
+
private readonly queryBus;
|
|
33
|
+
private readonly eventBus;
|
|
34
|
+
private readonly pipelineOrchestrator;
|
|
35
|
+
constructor(commandBus: CommandBus, queryBus: QueryBus, eventBus: EventBus, pipelineOrchestrator: PipelineOrchestrator);
|
|
36
|
+
/**
|
|
37
|
+
* Send a command to its handler through the pipeline
|
|
38
|
+
* @param command - The command instance
|
|
39
|
+
*/
|
|
40
|
+
send<TCommand extends ICommand>(command: TCommand): Promise<void>;
|
|
41
|
+
/**
|
|
42
|
+
* Execute a query through its handler and the pipeline
|
|
43
|
+
* @param query - The query instance
|
|
44
|
+
* @returns Promise with the result
|
|
45
|
+
*/
|
|
46
|
+
query<TQuery extends IQuery, TResult = any>(query: TQuery): Promise<TResult>;
|
|
47
|
+
/**
|
|
48
|
+
* Publish an event to all its consumers
|
|
49
|
+
* @param event - The event instance
|
|
50
|
+
* @returns Promise with the publish result
|
|
51
|
+
*/
|
|
52
|
+
publish<TEvent extends IEvent>(event: TEvent): Promise<EventPublishResult>;
|
|
36
53
|
/**
|
|
37
54
|
* Register a command handler
|
|
38
55
|
* @param command - The command class
|
|
@@ -49,36 +66,38 @@ export declare class MediatorBus {
|
|
|
49
66
|
* Register a pipeline behavior
|
|
50
67
|
* @param behaviorType - The behavior class
|
|
51
68
|
* @param options - Behavior options (priority, scope)
|
|
69
|
+
* @param requestType - Optional specific request type this behavior applies to
|
|
52
70
|
*/
|
|
53
|
-
registerPipelineBehavior(behaviorType: Type<IPipelineBehavior<any, any>>, options: PipelineBehaviorOptions): void;
|
|
71
|
+
registerPipelineBehavior(behaviorType: Type<IPipelineBehavior<any, any>>, options: PipelineBehaviorOptions, requestType?: Function): void;
|
|
54
72
|
/**
|
|
55
|
-
*
|
|
56
|
-
* @param
|
|
57
|
-
* @
|
|
73
|
+
* Register an event consumer
|
|
74
|
+
* @param event - The event class
|
|
75
|
+
* @param handler - The consumer class
|
|
76
|
+
* @param criticalityMetadata - Criticality metadata
|
|
58
77
|
*/
|
|
59
|
-
|
|
78
|
+
registerEventHandler(event: Type<IEvent>, handler: Type<IEventConsumer<any>>, criticalityMetadata?: EventCriticalityMetadata): void;
|
|
60
79
|
/**
|
|
61
|
-
*
|
|
62
|
-
* @param query - The query instance
|
|
63
|
-
* @returns Promise with the result
|
|
64
|
-
*/
|
|
65
|
-
query<TQuery extends IQuery, TResult = any>(query: TQuery): Promise<TResult>;
|
|
66
|
-
/**
|
|
67
|
-
* Build a pipeline of behaviors around the handler
|
|
68
|
-
* Uses reduceRight to create a chain where the first behavior wraps all others
|
|
69
|
-
*/
|
|
70
|
-
private buildPipeline;
|
|
71
|
-
/**
|
|
72
|
-
* Get registered command handler names (for debugging)
|
|
80
|
+
* Get registered command names (for debugging)
|
|
73
81
|
*/
|
|
74
82
|
getRegisteredCommands(): string[];
|
|
75
83
|
/**
|
|
76
|
-
* Get registered query
|
|
84
|
+
* Get registered query names (for debugging)
|
|
77
85
|
*/
|
|
78
86
|
getRegisteredQueries(): string[];
|
|
79
87
|
/**
|
|
80
88
|
* Get registered behavior names (for debugging)
|
|
81
89
|
*/
|
|
82
90
|
getRegisteredBehaviors(): string[];
|
|
91
|
+
/**
|
|
92
|
+
* Get registered events and their consumers (for debugging)
|
|
93
|
+
*/
|
|
94
|
+
getRegisteredEvents(): {
|
|
95
|
+
event: string;
|
|
96
|
+
handlers: {
|
|
97
|
+
name: string;
|
|
98
|
+
criticality: string;
|
|
99
|
+
order: number;
|
|
100
|
+
}[];
|
|
101
|
+
}[];
|
|
83
102
|
}
|
|
84
103
|
//# sourceMappingURL=mediator.bus.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mediator.bus.d.ts","sourceRoot":"","sources":["../../../src/lib/services/mediator.bus.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"mediator.bus.d.ts","sourceRoot":"","sources":["../../../src/lib/services/mediator.bus.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAClD,OAAO,EACL,QAAQ,EACR,eAAe,EACf,MAAM,EACN,aAAa,EACb,MAAM,EACN,cAAc,EACd,SAAS,EACT,kBAAkB,EAClB,wBAAwB,EACzB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EACL,iBAAiB,EACjB,uBAAuB,EACxB,MAAM,8CAA8C,CAAC;AACtD,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAE,oBAAoB,EAAE,MAAM,4BAA4B,CAAC;AAElE;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,qBACa,WAAY,YAAW,SAAS;IAEzC,OAAO,CAAC,QAAQ,CAAC,UAAU;IAC3B,OAAO,CAAC,QAAQ,CAAC,QAAQ;IACzB,OAAO,CAAC,QAAQ,CAAC,QAAQ;IACzB,OAAO,CAAC,QAAQ,CAAC,oBAAoB;gBAHpB,UAAU,EAAE,UAAU,EACtB,QAAQ,EAAE,QAAQ,EAClB,QAAQ,EAAE,QAAQ,EAClB,oBAAoB,EAAE,oBAAoB;IAG7D;;;OAGG;IACG,IAAI,CAAC,QAAQ,SAAS,QAAQ,EAAE,OAAO,EAAE,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC;IAIvE;;;;OAIG;IACG,KAAK,CAAC,MAAM,SAAS,MAAM,EAAE,OAAO,GAAG,GAAG,EAC9C,KAAK,EAAE,MAAM,GACZ,OAAO,CAAC,OAAO,CAAC;IAInB;;;;OAIG;IACG,OAAO,CAAC,MAAM,SAAS,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,kBAAkB,CAAC;IAIhF;;;;OAIG;IACH,sBAAsB,CACpB,OAAO,EAAE,IAAI,CAAC,QAAQ,CAAC,EACvB,OAAO,EAAE,IAAI,CAAC,eAAe,CAAC,GAAG,CAAC,CAAC,GAClC,IAAI;IAIP;;;;OAIG;IACH,oBAAoB,CAClB,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,EACnB,OAAO,EAAE,IAAI,CAAC,aAAa,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,GACrC,IAAI;IAIP;;;;;OAKG;IACH,wBAAwB,CACtB,YAAY,EAAE,IAAI,CAAC,iBAAiB,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,EAC/C,OAAO,EAAE,uBAAuB,EAChC,WAAW,CAAC,EAAE,QAAQ,GACrB,IAAI;IAIP;;;;;OAKG;IACH,oBAAoB,CAClB,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,EACnB,OAAO,EAAE,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,CAAC,EAClC,mBAAmB,CAAC,EAAE,wBAAwB,GAC7C,IAAI;IAIP;;OAEG;IACH,qBAAqB,IAAI,MAAM,EAAE;IAIjC;;OAEG;IACH,oBAAoB,IAAI,MAAM,EAAE;IAIhC;;OAEG;IACH,sBAAsB,IAAI,MAAM,EAAE;IAIlC;;OAEG;IACH,mBAAmB,IAAI;QACrB,KAAK,EAAE,MAAM,CAAC;QACd,QAAQ,EAAE;YAAE,IAAI,EAAE,MAAM,CAAC;YAAC,WAAW,EAAE,MAAM,CAAC;YAAC,KAAK,EAAE,MAAM,CAAA;SAAE,EAAE,CAAC;KAClE,EAAE;CAGJ"}
|
|
@@ -11,16 +11,16 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.MediatorBus = void 0;
|
|
13
13
|
const common_1 = require("@nestjs/common");
|
|
14
|
-
const
|
|
15
|
-
const
|
|
16
|
-
const
|
|
14
|
+
const command_bus_js_1 = require("./command.bus.js");
|
|
15
|
+
const query_bus_js_1 = require("./query.bus.js");
|
|
16
|
+
const event_bus_js_1 = require("./event.bus.js");
|
|
17
|
+
const pipeline_orchestrator_js_1 = require("./pipeline.orchestrator.js");
|
|
17
18
|
/**
|
|
18
|
-
* Central mediator bus for dispatching commands and
|
|
19
|
-
*
|
|
19
|
+
* Central mediator bus for dispatching commands, queries, and events.
|
|
20
|
+
* Acts as a facade delegating to specialized buses.
|
|
20
21
|
*
|
|
21
22
|
* @example
|
|
22
23
|
* ```typescript
|
|
23
|
-
* // In a controller
|
|
24
24
|
* @Controller('users')
|
|
25
25
|
* export class UserController {
|
|
26
26
|
* constructor(private readonly mediator: MediatorBus) {}
|
|
@@ -38,13 +38,34 @@ const skip_behavior_decorator_js_1 = require("../decorators/skip-behavior.decora
|
|
|
38
38
|
* ```
|
|
39
39
|
*/
|
|
40
40
|
let MediatorBus = class MediatorBus {
|
|
41
|
-
constructor(
|
|
42
|
-
this.
|
|
43
|
-
this.
|
|
44
|
-
this.
|
|
45
|
-
this.
|
|
46
|
-
|
|
47
|
-
|
|
41
|
+
constructor(commandBus, queryBus, eventBus, pipelineOrchestrator) {
|
|
42
|
+
this.commandBus = commandBus;
|
|
43
|
+
this.queryBus = queryBus;
|
|
44
|
+
this.eventBus = eventBus;
|
|
45
|
+
this.pipelineOrchestrator = pipelineOrchestrator;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Send a command to its handler through the pipeline
|
|
49
|
+
* @param command - The command instance
|
|
50
|
+
*/
|
|
51
|
+
async send(command) {
|
|
52
|
+
return this.commandBus.send(command);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Execute a query through its handler and the pipeline
|
|
56
|
+
* @param query - The query instance
|
|
57
|
+
* @returns Promise with the result
|
|
58
|
+
*/
|
|
59
|
+
async query(query) {
|
|
60
|
+
return this.queryBus.query(query);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Publish an event to all its consumers
|
|
64
|
+
* @param event - The event instance
|
|
65
|
+
* @returns Promise with the publish result
|
|
66
|
+
*/
|
|
67
|
+
async publish(event) {
|
|
68
|
+
return this.eventBus.publish(event);
|
|
48
69
|
}
|
|
49
70
|
/**
|
|
50
71
|
* Register a command handler
|
|
@@ -52,11 +73,7 @@ let MediatorBus = class MediatorBus {
|
|
|
52
73
|
* @param handler - The handler class
|
|
53
74
|
*/
|
|
54
75
|
registerCommandHandler(command, handler) {
|
|
55
|
-
|
|
56
|
-
if (this.commandHandlers.has(commandName)) {
|
|
57
|
-
throw new Error(`Command handler for ${commandName} is already registered`);
|
|
58
|
-
}
|
|
59
|
-
this.commandHandlers.set(commandName, handler);
|
|
76
|
+
this.commandBus.registerCommandHandler(command, handler);
|
|
60
77
|
}
|
|
61
78
|
/**
|
|
62
79
|
* Register a query handler
|
|
@@ -64,111 +81,57 @@ let MediatorBus = class MediatorBus {
|
|
|
64
81
|
* @param handler - The handler class
|
|
65
82
|
*/
|
|
66
83
|
registerQueryHandler(query, handler) {
|
|
67
|
-
|
|
68
|
-
if (this.queryHandlers.has(queryName)) {
|
|
69
|
-
throw new Error(`Query handler for ${queryName} is already registered`);
|
|
70
|
-
}
|
|
71
|
-
this.queryHandlers.set(queryName, handler);
|
|
84
|
+
this.queryBus.registerQueryHandler(query, handler);
|
|
72
85
|
}
|
|
73
86
|
/**
|
|
74
87
|
* Register a pipeline behavior
|
|
75
88
|
* @param behaviorType - The behavior class
|
|
76
89
|
* @param options - Behavior options (priority, scope)
|
|
90
|
+
* @param requestType - Optional specific request type this behavior applies to
|
|
77
91
|
*/
|
|
78
|
-
registerPipelineBehavior(behaviorType, options) {
|
|
79
|
-
this.
|
|
80
|
-
// Keep behaviors sorted by priority (lower first)
|
|
81
|
-
this.pipelineBehaviors.sort((a, b) => (a.options.priority ?? 0) - (b.options.priority ?? 0));
|
|
82
|
-
this.logger.log(`Registered pipeline behavior: ${behaviorType.name} (priority: ${options.priority ?? 0}, scope: ${options.scope ?? 'all'})`);
|
|
92
|
+
registerPipelineBehavior(behaviorType, options, requestType) {
|
|
93
|
+
this.pipelineOrchestrator.registerBehavior(behaviorType, options, requestType);
|
|
83
94
|
}
|
|
84
95
|
/**
|
|
85
|
-
*
|
|
86
|
-
* @param
|
|
87
|
-
* @
|
|
88
|
-
|
|
89
|
-
async send(command) {
|
|
90
|
-
const commandName = command.constructor.name;
|
|
91
|
-
const handlerType = this.commandHandlers.get(commandName);
|
|
92
|
-
if (!handlerType) {
|
|
93
|
-
throw new handler_not_found_exception_js_1.HandlerNotFoundException(commandName, 'command');
|
|
94
|
-
}
|
|
95
|
-
const handler = this.moduleRef.get(handlerType, {
|
|
96
|
-
strict: false,
|
|
97
|
-
});
|
|
98
|
-
// Build and execute pipeline
|
|
99
|
-
const pipeline = this.buildPipeline(command, 'command', () => handler.execute(command));
|
|
100
|
-
await pipeline();
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* Execute a query through its handler and the pipeline
|
|
104
|
-
* @param query - The query instance
|
|
105
|
-
* @returns Promise with the result
|
|
106
|
-
*/
|
|
107
|
-
async query(query) {
|
|
108
|
-
const queryName = query.constructor.name;
|
|
109
|
-
const handlerType = this.queryHandlers.get(queryName);
|
|
110
|
-
if (!handlerType) {
|
|
111
|
-
throw new handler_not_found_exception_js_1.HandlerNotFoundException(queryName, 'query');
|
|
112
|
-
}
|
|
113
|
-
const handler = this.moduleRef.get(handlerType, { strict: false });
|
|
114
|
-
// Build and execute pipeline
|
|
115
|
-
const pipeline = this.buildPipeline(query, 'query', () => handler.execute(query));
|
|
116
|
-
return pipeline();
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* Build a pipeline of behaviors around the handler
|
|
120
|
-
* Uses reduceRight to create a chain where the first behavior wraps all others
|
|
96
|
+
* Register an event consumer
|
|
97
|
+
* @param event - The event class
|
|
98
|
+
* @param handler - The consumer class
|
|
99
|
+
* @param criticalityMetadata - Criticality metadata
|
|
121
100
|
*/
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
const requestClass = request.constructor;
|
|
125
|
-
const behaviorsToSkip = this.reflector.get(skip_behavior_decorator_js_1.SKIP_BEHAVIORS_METADATA, requestClass) ?? [];
|
|
126
|
-
// Filter behaviors by scope and skip list
|
|
127
|
-
const applicableBehaviors = this.pipelineBehaviors.filter((b) => {
|
|
128
|
-
// Check scope
|
|
129
|
-
const behaviorScope = b.options.scope ?? 'all';
|
|
130
|
-
const scopeMatches = behaviorScope === 'all' || behaviorScope === scope;
|
|
131
|
-
// Check if this behavior should be skipped
|
|
132
|
-
const shouldSkip = behaviorsToSkip.some((skipType) => skipType === b.type);
|
|
133
|
-
return scopeMatches && !shouldSkip;
|
|
134
|
-
});
|
|
135
|
-
// If no behaviors, just return the handler
|
|
136
|
-
if (applicableBehaviors.length === 0) {
|
|
137
|
-
return handler;
|
|
138
|
-
}
|
|
139
|
-
// Build the pipeline from right to left (innermost to outermost)
|
|
140
|
-
// The last behavior in the array wraps the handler directly
|
|
141
|
-
// The first behavior in the array is the outermost wrapper
|
|
142
|
-
return applicableBehaviors.reduceRight((next, registeredBehavior) => {
|
|
143
|
-
return async () => {
|
|
144
|
-
const behavior = this.moduleRef.get(registeredBehavior.type, { strict: false });
|
|
145
|
-
return behavior.handle(request, next);
|
|
146
|
-
};
|
|
147
|
-
}, handler);
|
|
101
|
+
registerEventHandler(event, handler, criticalityMetadata) {
|
|
102
|
+
this.eventBus.registerEventHandler(event, handler, criticalityMetadata);
|
|
148
103
|
}
|
|
149
104
|
/**
|
|
150
|
-
* Get registered command
|
|
105
|
+
* Get registered command names (for debugging)
|
|
151
106
|
*/
|
|
152
107
|
getRegisteredCommands() {
|
|
153
|
-
return
|
|
108
|
+
return this.commandBus.getRegisteredCommands();
|
|
154
109
|
}
|
|
155
110
|
/**
|
|
156
|
-
* Get registered query
|
|
111
|
+
* Get registered query names (for debugging)
|
|
157
112
|
*/
|
|
158
113
|
getRegisteredQueries() {
|
|
159
|
-
return
|
|
114
|
+
return this.queryBus.getRegisteredQueries();
|
|
160
115
|
}
|
|
161
116
|
/**
|
|
162
117
|
* Get registered behavior names (for debugging)
|
|
163
118
|
*/
|
|
164
119
|
getRegisteredBehaviors() {
|
|
165
|
-
return this.
|
|
120
|
+
return this.pipelineOrchestrator.getRegisteredBehaviors();
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Get registered events and their consumers (for debugging)
|
|
124
|
+
*/
|
|
125
|
+
getRegisteredEvents() {
|
|
126
|
+
return this.eventBus.getRegisteredEvents();
|
|
166
127
|
}
|
|
167
128
|
};
|
|
168
129
|
exports.MediatorBus = MediatorBus;
|
|
169
130
|
exports.MediatorBus = MediatorBus = __decorate([
|
|
170
131
|
(0, common_1.Injectable)(),
|
|
171
|
-
__metadata("design:paramtypes", [
|
|
172
|
-
|
|
132
|
+
__metadata("design:paramtypes", [command_bus_js_1.CommandBus,
|
|
133
|
+
query_bus_js_1.QueryBus,
|
|
134
|
+
event_bus_js_1.EventBus,
|
|
135
|
+
pipeline_orchestrator_js_1.PipelineOrchestrator])
|
|
173
136
|
], MediatorBus);
|
|
174
137
|
//# sourceMappingURL=mediator.bus.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mediator.bus.js","sourceRoot":"","sources":["../../../src/lib/services/mediator.bus.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"mediator.bus.js","sourceRoot":"","sources":["../../../src/lib/services/mediator.bus.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2CAAkD;AAgBlD,qDAA8C;AAC9C,iDAA0C;AAC1C,iDAA0C;AAC1C,yEAAkE;AAElE;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEI,IAAM,WAAW,GAAjB,MAAM,WAAW;IACtB,YACmB,UAAsB,EACtB,QAAkB,EAClB,QAAkB,EAClB,oB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C,qBAAqB,EAAE,CAAC;IACjD,CAAC;IAED;;OAEG;IACH,oBAAoB;QAClB,OAAO,IAAI,CAAC,QAAQ,CAAC,oBAAoB,EAAE,CAAC;IAC9C,CAAC;IAED;;OAEG;IACH,sBAAsB;QACpB,OAAO,IAAI,CAAC,oBAAoB,CAAC,sBAAsB,EAAE,CAAC;IAC5D,CAAC;IAED;;OAEG;IACH,mBAAmB;QAIjB,OAAO,IAAI,CAAC,QAAQ,CAAC,mBAAmB,EAAE,CAAC;IAC7C,CAAC;CACF,CAAA;AAtHY,kCAAW;sBAAX,WAAW;IADvB,IAAA,mBAAU,GAAE;qCAGoB,2BAAU;QACZ,uBAAQ;QACR,uBAAQ;QACI,+CAAoB;GALlD,WAAW,CAsHvB"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { Type } from '@nestjs/common';
|
|
2
|
+
import { ModuleRef, Reflector } from '@nestjs/core';
|
|
3
|
+
import { IPipelineBehavior, PipelineBehaviorOptions } from '../interfaces/pipeline-behavior.interface.js';
|
|
4
|
+
/**
|
|
5
|
+
* Registered behavior with its metadata
|
|
6
|
+
*/
|
|
7
|
+
export interface RegisteredBehavior {
|
|
8
|
+
type: Type<IPipelineBehavior<any, any>>;
|
|
9
|
+
options: PipelineBehaviorOptions;
|
|
10
|
+
/**
|
|
11
|
+
* The specific request type this behavior applies to.
|
|
12
|
+
* When undefined, behavior applies to all requests.
|
|
13
|
+
*/
|
|
14
|
+
requestType?: Function;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Orchestrates pipeline behaviors for commands and queries.
|
|
18
|
+
* Manages behavior registration and builds execution pipelines.
|
|
19
|
+
*/
|
|
20
|
+
export declare class PipelineOrchestrator {
|
|
21
|
+
private readonly moduleRef;
|
|
22
|
+
private readonly reflector;
|
|
23
|
+
private readonly logger;
|
|
24
|
+
private readonly behaviors;
|
|
25
|
+
constructor(moduleRef: ModuleRef, reflector: Reflector);
|
|
26
|
+
/**
|
|
27
|
+
* Register a pipeline behavior
|
|
28
|
+
* @param behaviorType - The behavior class
|
|
29
|
+
* @param options - Behavior options (priority, scope)
|
|
30
|
+
* @param requestType - Optional specific request type this behavior applies to
|
|
31
|
+
*/
|
|
32
|
+
registerBehavior(behaviorType: Type<IPipelineBehavior<any, any>>, options: PipelineBehaviorOptions, requestType?: Function): void;
|
|
33
|
+
/**
|
|
34
|
+
* Build a pipeline of behaviors around the handler
|
|
35
|
+
* @param request - The request object (command or query)
|
|
36
|
+
* @param scope - The scope ('command' or 'query')
|
|
37
|
+
* @param handler - The innermost handler function
|
|
38
|
+
* @returns A function that executes the full pipeline
|
|
39
|
+
*/
|
|
40
|
+
buildPipeline<TRequest, TResponse>(request: TRequest, scope: 'command' | 'query', handler: () => Promise<TResponse>): () => Promise<TResponse>;
|
|
41
|
+
/**
|
|
42
|
+
* Get registered behavior names (for debugging)
|
|
43
|
+
*/
|
|
44
|
+
getRegisteredBehaviors(): string[];
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=pipeline.orchestrator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pipeline.orchestrator.d.ts","sourceRoot":"","sources":["../../../src/lib/services/pipeline.orchestrator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAC1D,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACpD,OAAO,EACL,iBAAiB,EACjB,uBAAuB,EACxB,MAAM,8CAA8C,CAAC;AAGtD;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,IAAI,EAAE,IAAI,CAAC,iBAAiB,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC;IACxC,OAAO,EAAE,uBAAuB,CAAC;IACjC;;;OAGG;IACH,WAAW,CAAC,EAAE,QAAQ,CAAC;CACxB;AAED;;;GAGG;AACH,qBACa,oBAAoB;IAK7B,OAAO,CAAC,QAAQ,CAAC,SAAS;IAC1B,OAAO,CAAC,QAAQ,CAAC,SAAS;IAL5B,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAsC;IAC7D,OAAO,CAAC,QAAQ,CAAC,SAAS,CAA4B;gBAGnC,SAAS,EAAE,SAAS,EACpB,SAAS,EAAE,SAAS;IAGvC;;;;;OAKG;IACH,gBAAgB,CACd,YAAY,EAAE,IAAI,CAAC,iBAAiB,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,EAC/C,OAAO,EAAE,uBAAuB,EAChC,WAAW,CAAC,EAAE,QAAQ,GACrB,IAAI;IAcP;;;;;;OAMG;IACH,aAAa,CAAC,QAAQ,EAAE,SAAS,EAC/B,OAAO,EAAE,QAAQ,EACjB,KAAK,EAAE,SAAS,GAAG,OAAO,EAC1B,OAAO,EAAE,MAAM,OAAO,CAAC,SAAS,CAAC,GAChC,MAAM,OAAO,CAAC,SAAS,CAAC;IA8C3B;;OAEG;IACH,sBAAsB,IAAI,MAAM,EAAE;CAGnC"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.PipelineOrchestrator = void 0;
|
|
13
|
+
const common_1 = require("@nestjs/common");
|
|
14
|
+
const core_1 = require("@nestjs/core");
|
|
15
|
+
const skip_behavior_decorator_js_1 = require("../decorators/skip-behavior.decorator.js");
|
|
16
|
+
/**
|
|
17
|
+
* Orchestrates pipeline behaviors for commands and queries.
|
|
18
|
+
* Manages behavior registration and builds execution pipelines.
|
|
19
|
+
*/
|
|
20
|
+
let PipelineOrchestrator = class PipelineOrchestrator {
|
|
21
|
+
constructor(moduleRef, reflector) {
|
|
22
|
+
this.moduleRef = moduleRef;
|
|
23
|
+
this.reflector = reflector;
|
|
24
|
+
this.logger = new common_1.Logger('PipelineOrchestrator');
|
|
25
|
+
this.behaviors = [];
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Register a pipeline behavior
|
|
29
|
+
* @param behaviorType - The behavior class
|
|
30
|
+
* @param options - Behavior options (priority, scope)
|
|
31
|
+
* @param requestType - Optional specific request type this behavior applies to
|
|
32
|
+
*/
|
|
33
|
+
registerBehavior(behaviorType, options, requestType) {
|
|
34
|
+
this.behaviors.push({ type: behaviorType, options, requestType });
|
|
35
|
+
// Keep behaviors sorted by priority (lower first)
|
|
36
|
+
this.behaviors.sort((a, b) => (a.options.priority ?? 0) - (b.options.priority ?? 0));
|
|
37
|
+
const requestTypeInfo = requestType ? `, requestType: ${requestType.name}` : '';
|
|
38
|
+
this.logger.log(`Registered pipeline behavior: ${behaviorType.name} (priority: ${options.priority ?? 0}, scope: ${options.scope ?? 'all'}${requestTypeInfo})`);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Build a pipeline of behaviors around the handler
|
|
42
|
+
* @param request - The request object (command or query)
|
|
43
|
+
* @param scope - The scope ('command' or 'query')
|
|
44
|
+
* @param handler - The innermost handler function
|
|
45
|
+
* @returns A function that executes the full pipeline
|
|
46
|
+
*/
|
|
47
|
+
buildPipeline(request, scope, handler) {
|
|
48
|
+
// Get behaviors to skip from request metadata
|
|
49
|
+
const requestClass = request.constructor;
|
|
50
|
+
const behaviorsToSkip = this.reflector.get(skip_behavior_decorator_js_1.SKIP_BEHAVIORS_METADATA, requestClass) ?? [];
|
|
51
|
+
// Filter behaviors by scope, skip list, and request type
|
|
52
|
+
const applicableBehaviors = this.behaviors.filter((b) => {
|
|
53
|
+
// Check scope
|
|
54
|
+
const behaviorScope = b.options.scope ?? 'all';
|
|
55
|
+
const scopeMatches = behaviorScope === 'all' || behaviorScope === scope;
|
|
56
|
+
// Check if this behavior should be skipped
|
|
57
|
+
const shouldSkip = behaviorsToSkip.some((skipType) => skipType === b.type);
|
|
58
|
+
// Check request type match
|
|
59
|
+
const requestTypeMatches = !b.requestType || request instanceof b.requestType;
|
|
60
|
+
return scopeMatches && !shouldSkip && requestTypeMatches;
|
|
61
|
+
});
|
|
62
|
+
// If no behaviors, just return the handler
|
|
63
|
+
if (applicableBehaviors.length === 0) {
|
|
64
|
+
return handler;
|
|
65
|
+
}
|
|
66
|
+
// Build the pipeline from right to left (innermost to outermost)
|
|
67
|
+
return applicableBehaviors.reduceRight((next, registeredBehavior) => {
|
|
68
|
+
return async () => {
|
|
69
|
+
const behavior = this.moduleRef.get(registeredBehavior.type, { strict: false });
|
|
70
|
+
return behavior.handle(request, next);
|
|
71
|
+
};
|
|
72
|
+
}, handler);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Get registered behavior names (for debugging)
|
|
76
|
+
*/
|
|
77
|
+
getRegisteredBehaviors() {
|
|
78
|
+
return this.behaviors.map((b) => b.type.name);
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
exports.PipelineOrchestrator = PipelineOrchestrator;
|
|
82
|
+
exports.PipelineOrchestrator = PipelineOrchestrator = __decorate([
|
|
83
|
+
(0, common_1.Injectable)(),
|
|
84
|
+
__metadata("design:paramtypes", [core_1.ModuleRef,
|
|
85
|
+
core_1.Reflector])
|
|
86
|
+
], PipelineOrchestrator);
|
|
87
|
+
//# sourceMappingURL=pipeline.orchestrator.js.map
|
|
@@ -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,sBAAsB;QACpB,OAAO,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAChD,CAAC;CACF,CAAA;AAhGY,oDAAoB;+BAApB,oBAAoB;IADhC,IAAA,mBAAU,GAAE;qCAMmB,gBAAS;QACT,gBAAS;GAN5B,oBAAoB,CAgGhC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Type } from '@nestjs/common';
|
|
2
|
+
import { ModuleRef } from '@nestjs/core';
|
|
3
|
+
import { IQuery, IQueryHandler, IQueryBus } from '../interfaces/index.js';
|
|
4
|
+
import { PipelineOrchestrator } from './pipeline.orchestrator.js';
|
|
5
|
+
/**
|
|
6
|
+
* Query bus implementation.
|
|
7
|
+
* Dispatches queries to their handlers through the pipeline.
|
|
8
|
+
*/
|
|
9
|
+
export declare class QueryBus implements IQueryBus {
|
|
10
|
+
private readonly moduleRef;
|
|
11
|
+
private readonly pipelineOrchestrator;
|
|
12
|
+
private readonly handlers;
|
|
13
|
+
constructor(moduleRef: ModuleRef, pipelineOrchestrator: PipelineOrchestrator);
|
|
14
|
+
/**
|
|
15
|
+
* Execute a query through its handler and the pipeline
|
|
16
|
+
* @param query - The query instance
|
|
17
|
+
* @returns Promise with the result
|
|
18
|
+
*/
|
|
19
|
+
query<TQuery extends IQuery, TResult = any>(query: TQuery): Promise<TResult>;
|
|
20
|
+
/**
|
|
21
|
+
* Register a query handler
|
|
22
|
+
* @param query - The query class
|
|
23
|
+
* @param handler - The handler class
|
|
24
|
+
*/
|
|
25
|
+
registerQueryHandler(query: Type<IQuery>, handler: Type<IQueryHandler<any, any>>): void;
|
|
26
|
+
/**
|
|
27
|
+
* Get registered query names (for debugging)
|
|
28
|
+
*/
|
|
29
|
+
getRegisteredQueries(): string[];
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=query.bus.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"query.bus.d.ts","sourceRoot":"","sources":["../../../src/lib/services/query.bus.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAClD,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAE1E,OAAO,EAAE,oBAAoB,EAAE,MAAM,4BAA4B,CAAC;AAElE;;;GAGG;AACH,qBACa,QAAS,YAAW,SAAS;IAItC,OAAO,CAAC,QAAQ,CAAC,SAAS;IAC1B,OAAO,CAAC,QAAQ,CAAC,oBAAoB;IAJvC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAoD;gBAG1D,SAAS,EAAE,SAAS,EACpB,oBAAoB,EAAE,oBAAoB;IAG7D;;;;OAIG;IACG,KAAK,CAAC,MAAM,SAAS,MAAM,EAAE,OAAO,GAAG,GAAG,EAC9C,KAAK,EAAE,MAAM,GACZ,OAAO,CAAC,OAAO,CAAC;IAuBnB;;;;OAIG;IACH,oBAAoB,CAClB,KAAK,EAAE,IAAI,CAAC,MAAM,CAAC,EACnB,OAAO,EAAE,IAAI,CAAC,aAAa,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,GACrC,IAAI;IAQP;;OAEG;IACH,oBAAoB,IAAI,MAAM,EAAE;CAGjC"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.QueryBus = void 0;
|
|
13
|
+
const common_1 = require("@nestjs/common");
|
|
14
|
+
const core_1 = require("@nestjs/core");
|
|
15
|
+
const handler_not_found_exception_js_1 = require("../exceptions/handler-not-found.exception.js");
|
|
16
|
+
const pipeline_orchestrator_js_1 = require("./pipeline.orchestrator.js");
|
|
17
|
+
/**
|
|
18
|
+
* Query bus implementation.
|
|
19
|
+
* Dispatches queries to their handlers through the pipeline.
|
|
20
|
+
*/
|
|
21
|
+
let QueryBus = class QueryBus {
|
|
22
|
+
constructor(moduleRef, pipelineOrchestrator) {
|
|
23
|
+
this.moduleRef = moduleRef;
|
|
24
|
+
this.pipelineOrchestrator = pipelineOrchestrator;
|
|
25
|
+
this.handlers = new Map();
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Execute a query through its handler and the pipeline
|
|
29
|
+
* @param query - The query instance
|
|
30
|
+
* @returns Promise with the result
|
|
31
|
+
*/
|
|
32
|
+
async query(query) {
|
|
33
|
+
const queryName = query.constructor.name;
|
|
34
|
+
const handlerType = this.handlers.get(queryName);
|
|
35
|
+
if (!handlerType) {
|
|
36
|
+
throw new handler_not_found_exception_js_1.HandlerNotFoundException(queryName, 'query');
|
|
37
|
+
}
|
|
38
|
+
const handler = this.moduleRef.get(handlerType, { strict: false });
|
|
39
|
+
// Build and execute pipeline
|
|
40
|
+
const pipeline = this.pipelineOrchestrator.buildPipeline(query, 'query', () => handler.execute(query));
|
|
41
|
+
return pipeline();
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Register a query handler
|
|
45
|
+
* @param query - The query class
|
|
46
|
+
* @param handler - The handler class
|
|
47
|
+
*/
|
|
48
|
+
registerQueryHandler(query, handler) {
|
|
49
|
+
const queryName = query.name;
|
|
50
|
+
if (this.handlers.has(queryName)) {
|
|
51
|
+
throw new Error(`Query handler for ${queryName} is already registered`);
|
|
52
|
+
}
|
|
53
|
+
this.handlers.set(queryName, handler);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Get registered query names (for debugging)
|
|
57
|
+
*/
|
|
58
|
+
getRegisteredQueries() {
|
|
59
|
+
return Array.from(this.handlers.keys());
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
exports.QueryBus = QueryBus;
|
|
63
|
+
exports.QueryBus = QueryBus = __decorate([
|
|
64
|
+
(0, common_1.Injectable)(),
|
|
65
|
+
__metadata("design:paramtypes", [core_1.ModuleRef,
|
|
66
|
+
pipeline_orchestrator_js_1.PipelineOrchestrator])
|
|
67
|
+
], QueryBus);
|
|
68
|
+
//# sourceMappingURL=query.bus.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"query.bus.js","sourceRoot":"","sources":["../../../src/lib/services/query.bus.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2CAAkD;AAClD,uCAAyC;AAEzC,iGAAwF;AACxF,yEAAkE;AAElE;;;GAGG;AAEI,IAAM,QAAQ,GAAd,MAAM,QAAQ;IAGnB,YACmB,SAAoB,EACpB,oBAA0C;QAD1C,cAAS,GAAT,SAAS,CAAW;QACpB,yBAAoB,GAApB,oBAAoB,CAAsB;QAJ5C,aAAQ,GAAG,IAAI,GAAG,EAAyC,CAAC;IAK1E,CAAC;IAEJ;;;;OAIG;IACH,KAAK,CAAC,KAAK,CACT,KAAa;QAEb,MAAM,SAAS,GAAG,KAAK,CAAC,WAAW,CAAC,IAAI,CAAC;QACzC,MAAM,WAAW,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;QAEjD,IAAI,CAAC,WAAW,EAAE,CAAC;YACjB,MAAM,IAAI,yDAAwB,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;QACzD,CAAC;QAED,MAAM,OAAO,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAChC,WAAW,EACX,EAAE,MAAM,EAAE,KAAK,EAAE,CAClB,CAAC;QAEF,6BAA6B;QAC7B,MAAM,QAAQ,GAAG,IAAI,CAAC,oBAAoB,CAAC,aAAa,CACtD,KAAK,EACL,OAAO,EACP,GAAG,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,CAC7B,CAAC;QAEF,OAAO,QAAQ,EAAE,CAAC;IACpB,CAAC;IAED;;;;OAIG;IACH,oBAAoB,CAClB,KAAmB,EACnB,OAAsC;QAEtC,MAAM,SAAS,GAAG,KAAK,CAAC,IAAI,CAAC;QAC7B,IAAI,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,SAAS,CAAC,EAAE,CAAC;YACjC,MAAM,IAAI,KAAK,CAAC,qBAAqB,SAAS,wBAAwB,CAAC,CAAC;QAC1E,CAAC;QACD,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;IACxC,CAAC;IAED;;OAEG;IACH,oBAAoB;QAClB,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAC;IAC1C,CAAC;CACF,CAAA;AA5DY,4BAAQ;mBAAR,QAAQ;IADpB,IAAA,mBAAU,GAAE;qCAKmB,gBAAS;QACE,+CAAoB;GALlD,QAAQ,CA4DpB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rolandsall24/nest-mediator",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "A lightweight CQRS (Command Query Responsibility Segregation) mediator pattern implementation for NestJS applications",
|
|
5
5
|
"author": "Roland Salloum",
|
|
6
6
|
"license": "MIT",
|