@nestjs/cqrs
Advanced tools
Comparing version 7.0.1 to 8.0.0
@@ -189,3 +189,3 @@ # Contributing to Nest | ||
``` | ||
bugfix(@nestjs/core) need to depend on latest rxjs and zone.js | ||
fix(@nestjs/core) need to depend on latest rxjs and zone.js | ||
@@ -202,6 +202,7 @@ The version in our package.json gets copied to the one we publish, and users need the latest of these. | ||
* **build**: Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm) | ||
* **chore**: Updating tasks etc; no production code change | ||
* **ci**: Changes to our CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs) | ||
* **docs**: Documentation only changes | ||
* **feature**: A new feature | ||
* **bugfix**: A bug fix | ||
* **feat**: A new feature | ||
* **fix**: A bug fix | ||
* **perf**: A code change that improves performance | ||
@@ -208,0 +209,0 @@ * **refactor**: A code change that neither fixes a bug nor adds a feature |
@@ -10,2 +10,3 @@ import { IEvent } from './interfaces'; | ||
publish<T extends EventBase = EventBase>(event: T): void; | ||
publishAll<T extends EventBase = EventBase>(event: T[]): void; | ||
commit(): void; | ||
@@ -12,0 +13,0 @@ uncommit(): void; |
@@ -19,4 +19,5 @@ "use strict"; | ||
publish(event) { } | ||
publishAll(event) { } | ||
commit() { | ||
this[INTERNAL_EVENTS].forEach((event) => this.publish(event)); | ||
this.publishAll(this[INTERNAL_EVENTS]); | ||
this[INTERNAL_EVENTS].length = 0; | ||
@@ -23,0 +24,0 @@ } |
@@ -14,3 +14,3 @@ import { Type } from '@nestjs/common'; | ||
set publisher(_publisher: ICommandPublisher<CommandBase>); | ||
execute<T extends CommandBase>(command: T): Promise<any>; | ||
execute<T extends CommandBase, R = any>(command: T): Promise<R>; | ||
bind<T extends CommandBase>(handler: ICommandHandler<T>, name: string): void; | ||
@@ -17,0 +17,0 @@ register(handlers?: CommandHandlerType[]): void; |
@@ -21,59 +21,56 @@ "use strict"; | ||
const observable_bus_1 = require("./utils/observable-bus"); | ||
let CommandBus = (() => { | ||
let CommandBus = class CommandBus extends observable_bus_1.ObservableBus { | ||
constructor(moduleRef) { | ||
super(); | ||
this.moduleRef = moduleRef; | ||
this.handlers = new Map(); | ||
this.useDefaultPublisher(); | ||
let CommandBus = class CommandBus extends observable_bus_1.ObservableBus { | ||
constructor(moduleRef) { | ||
super(); | ||
this.moduleRef = moduleRef; | ||
this.handlers = new Map(); | ||
this.useDefaultPublisher(); | ||
} | ||
get publisher() { | ||
return this._publisher; | ||
} | ||
set publisher(_publisher) { | ||
this._publisher = _publisher; | ||
} | ||
execute(command) { | ||
const commandName = this.getCommandName(command); | ||
const handler = this.handlers.get(commandName); | ||
if (!handler) { | ||
throw new command_not_found_exception_1.CommandHandlerNotFoundException(commandName); | ||
} | ||
get publisher() { | ||
return this._publisher; | ||
this.subject$.next(command); | ||
return handler.execute(command); | ||
} | ||
bind(handler, name) { | ||
this.handlers.set(name, handler); | ||
} | ||
register(handlers = []) { | ||
handlers.forEach((handler) => this.registerHandler(handler)); | ||
} | ||
registerHandler(handler) { | ||
const instance = this.moduleRef.get(handler, { strict: false }); | ||
if (!instance) { | ||
return; | ||
} | ||
set publisher(_publisher) { | ||
this._publisher = _publisher; | ||
const target = this.reflectCommandName(handler); | ||
if (!target) { | ||
throw new index_1.InvalidCommandHandlerException(); | ||
} | ||
execute(command) { | ||
const commandName = this.getCommandName(command); | ||
const handler = this.handlers.get(commandName); | ||
if (!handler) { | ||
throw new command_not_found_exception_1.CommandHandlerNotFoundException(commandName); | ||
} | ||
this.subject$.next(command); | ||
return handler.execute(command); | ||
} | ||
bind(handler, name) { | ||
this.handlers.set(name, handler); | ||
} | ||
register(handlers = []) { | ||
handlers.forEach((handler) => this.registerHandler(handler)); | ||
} | ||
registerHandler(handler) { | ||
const instance = this.moduleRef.get(handler, { strict: false }); | ||
if (!instance) { | ||
return; | ||
} | ||
const target = this.reflectCommandName(handler); | ||
if (!target) { | ||
throw new index_1.InvalidCommandHandlerException(); | ||
} | ||
this.bind(instance, target.name); | ||
} | ||
getCommandName(command) { | ||
const { constructor } = Object.getPrototypeOf(command); | ||
return constructor.name; | ||
} | ||
reflectCommandName(handler) { | ||
return Reflect.getMetadata(constants_1.COMMAND_HANDLER_METADATA, handler); | ||
} | ||
useDefaultPublisher() { | ||
this._publisher = new default_command_pubsub_1.DefaultCommandPubSub(this.subject$); | ||
} | ||
}; | ||
CommandBus = __decorate([ | ||
common_1.Injectable(), | ||
__metadata("design:paramtypes", [core_1.ModuleRef]) | ||
], CommandBus); | ||
return CommandBus; | ||
})(); | ||
this.bind(instance, target.name); | ||
} | ||
getCommandName(command) { | ||
const { constructor } = Object.getPrototypeOf(command); | ||
return constructor.name; | ||
} | ||
reflectCommandName(handler) { | ||
return Reflect.getMetadata(constants_1.COMMAND_HANDLER_METADATA, handler); | ||
} | ||
useDefaultPublisher() { | ||
this._publisher = new default_command_pubsub_1.DefaultCommandPubSub(this.subject$); | ||
} | ||
}; | ||
CommandBus = __decorate([ | ||
common_1.Injectable(), | ||
__metadata("design:paramtypes", [core_1.ModuleRef]) | ||
], CommandBus); | ||
exports.CommandBus = CommandBus; |
@@ -19,30 +19,27 @@ "use strict"; | ||
const explorer_service_1 = require("./services/explorer.service"); | ||
let CqrsModule = (() => { | ||
let CqrsModule = class CqrsModule { | ||
constructor(explorerService, eventsBus, commandsBus, queryBus) { | ||
this.explorerService = explorerService; | ||
this.eventsBus = eventsBus; | ||
this.commandsBus = commandsBus; | ||
this.queryBus = queryBus; | ||
} | ||
onApplicationBootstrap() { | ||
const { events, queries, sagas, commands } = this.explorerService.explore(); | ||
this.eventsBus.register(events); | ||
this.commandsBus.register(commands); | ||
this.queryBus.register(queries); | ||
this.eventsBus.registerSagas(sagas); | ||
} | ||
}; | ||
CqrsModule = __decorate([ | ||
common_1.Module({ | ||
providers: [command_bus_1.CommandBus, query_bus_1.QueryBus, event_bus_1.EventBus, event_publisher_1.EventPublisher, explorer_service_1.ExplorerService], | ||
exports: [command_bus_1.CommandBus, query_bus_1.QueryBus, event_bus_1.EventBus, event_publisher_1.EventPublisher], | ||
}), | ||
__metadata("design:paramtypes", [explorer_service_1.ExplorerService, | ||
event_bus_1.EventBus, | ||
command_bus_1.CommandBus, | ||
query_bus_1.QueryBus]) | ||
], CqrsModule); | ||
return CqrsModule; | ||
})(); | ||
let CqrsModule = class CqrsModule { | ||
constructor(explorerService, eventsBus, commandsBus, queryBus) { | ||
this.explorerService = explorerService; | ||
this.eventsBus = eventsBus; | ||
this.commandsBus = commandsBus; | ||
this.queryBus = queryBus; | ||
} | ||
onApplicationBootstrap() { | ||
const { events, queries, sagas, commands } = this.explorerService.explore(); | ||
this.eventsBus.register(events); | ||
this.commandsBus.register(commands); | ||
this.queryBus.register(queries); | ||
this.eventsBus.registerSagas(sagas); | ||
} | ||
}; | ||
CqrsModule = __decorate([ | ||
common_1.Module({ | ||
providers: [command_bus_1.CommandBus, query_bus_1.QueryBus, event_bus_1.EventBus, event_publisher_1.EventPublisher, explorer_service_1.ExplorerService], | ||
exports: [command_bus_1.CommandBus, query_bus_1.QueryBus, event_bus_1.EventBus, event_publisher_1.EventPublisher], | ||
}), | ||
__metadata("design:paramtypes", [explorer_service_1.ExplorerService, | ||
event_bus_1.EventBus, | ||
command_bus_1.CommandBus, | ||
query_bus_1.QueryBus]) | ||
], CqrsModule); | ||
exports.CqrsModule = CqrsModule; |
import 'reflect-metadata'; | ||
import { ICommand } from '../index'; | ||
/** | ||
* Decorator that marks a class as a Nest command handler. A command handler | ||
* handles commands (actions) executed by your application code. | ||
* | ||
* The decorated class must implement the `ICommandHandler` interface. | ||
* | ||
* @param command command *type* to be handled by this handler. | ||
* | ||
* @see https://docs.nestjs.com/recipes/cqrs#commands | ||
*/ | ||
export declare const CommandHandler: (command: ICommand) => ClassDecorator; |
@@ -6,3 +6,13 @@ "use strict"; | ||
const constants_1 = require("./constants"); | ||
exports.CommandHandler = (command) => { | ||
/** | ||
* Decorator that marks a class as a Nest command handler. A command handler | ||
* handles commands (actions) executed by your application code. | ||
* | ||
* The decorated class must implement the `ICommandHandler` interface. | ||
* | ||
* @param command command *type* to be handled by this handler. | ||
* | ||
* @see https://docs.nestjs.com/recipes/cqrs#commands | ||
*/ | ||
const CommandHandler = (command) => { | ||
return (target) => { | ||
@@ -12,1 +22,2 @@ Reflect.defineMetadata(constants_1.COMMAND_HANDLER_METADATA, command, target); | ||
}; | ||
exports.CommandHandler = CommandHandler; |
import 'reflect-metadata'; | ||
import { IEvent } from '../index'; | ||
/** | ||
* Decorator that marks a class as a Nest event handler. An event handler | ||
* handles events executed by your application code. | ||
* | ||
* The decorated class must implement the `IEventHandler` interface. | ||
* | ||
* @param events one or more event *types* to be handled by this handler. | ||
* | ||
* @see https://docs.nestjs.com/recipes/cqrs#events | ||
*/ | ||
export declare const EventsHandler: (...events: IEvent[]) => ClassDecorator; |
@@ -6,3 +6,13 @@ "use strict"; | ||
const constants_1 = require("./constants"); | ||
exports.EventsHandler = (...events) => { | ||
/** | ||
* Decorator that marks a class as a Nest event handler. An event handler | ||
* handles events executed by your application code. | ||
* | ||
* The decorated class must implement the `IEventHandler` interface. | ||
* | ||
* @param events one or more event *types* to be handled by this handler. | ||
* | ||
* @see https://docs.nestjs.com/recipes/cqrs#events | ||
*/ | ||
const EventsHandler = (...events) => { | ||
return (target) => { | ||
@@ -12,1 +22,2 @@ Reflect.defineMetadata(constants_1.EVENTS_HANDLER_METADATA, events, target); | ||
}; | ||
exports.EventsHandler = EventsHandler; |
@@ -10,4 +10,4 @@ "use strict"; | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); | ||
} | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -14,0 +14,0 @@ __exportStar(require("./command-handler.decorator"), exports); |
import 'reflect-metadata'; | ||
import { IQuery } from '../interfaces'; | ||
/** | ||
* Decorator that marks a class as a Nest query handler. A query handler | ||
* handles queries executed by your application code. | ||
* | ||
* The decorated class must implement the `IQueryHandler` interface. | ||
* | ||
* @param query query *type* to be handled by this handler. | ||
* | ||
* @see https://docs.nestjs.com/recipes/cqrs#queries | ||
*/ | ||
export declare const QueryHandler: (query: IQuery) => ClassDecorator; |
@@ -6,3 +6,13 @@ "use strict"; | ||
const constants_1 = require("./constants"); | ||
exports.QueryHandler = (query) => { | ||
/** | ||
* Decorator that marks a class as a Nest query handler. A query handler | ||
* handles queries executed by your application code. | ||
* | ||
* The decorated class must implement the `IQueryHandler` interface. | ||
* | ||
* @param query query *type* to be handled by this handler. | ||
* | ||
* @see https://docs.nestjs.com/recipes/cqrs#queries | ||
*/ | ||
const QueryHandler = (query) => { | ||
return (target) => { | ||
@@ -12,1 +22,2 @@ Reflect.defineMetadata(constants_1.QUERY_HANDLER_METADATA, query, target); | ||
}; | ||
exports.QueryHandler = QueryHandler; |
import 'reflect-metadata'; | ||
/** | ||
* Decorator that marks a class as a Nest saga. Sagas may listen and react to 1..N events. | ||
* | ||
* @see https://docs.nestjs.com/recipes/cqrs#sagas | ||
*/ | ||
export declare const Saga: () => PropertyDecorator; |
@@ -6,3 +6,8 @@ "use strict"; | ||
const constants_1 = require("./constants"); | ||
exports.Saga = () => { | ||
/** | ||
* Decorator that marks a class as a Nest saga. Sagas may listen and react to 1..N events. | ||
* | ||
* @see https://docs.nestjs.com/recipes/cqrs#sagas | ||
*/ | ||
const Saga = () => { | ||
return (target, propertyKey) => { | ||
@@ -13,1 +18,2 @@ const properties = Reflect.getMetadata(constants_1.SAGA_METADATA, target.constructor) || []; | ||
}; | ||
exports.Saga = Saga; |
@@ -24,89 +24,86 @@ "use strict"; | ||
const utils_1 = require("./utils"); | ||
let EventBus = (() => { | ||
let EventBus = class EventBus extends utils_1.ObservableBus { | ||
constructor(commandBus, moduleRef) { | ||
super(); | ||
this.commandBus = commandBus; | ||
this.moduleRef = moduleRef; | ||
this.subscriptions = []; | ||
this.getEventName = default_get_event_name_1.defaultGetEventName; | ||
this.useDefaultPublisher(); | ||
let EventBus = class EventBus extends utils_1.ObservableBus { | ||
constructor(commandBus, moduleRef) { | ||
super(); | ||
this.commandBus = commandBus; | ||
this.moduleRef = moduleRef; | ||
this.subscriptions = []; | ||
this.getEventName = default_get_event_name_1.defaultGetEventName; | ||
this.useDefaultPublisher(); | ||
} | ||
get publisher() { | ||
return this._publisher; | ||
} | ||
set publisher(_publisher) { | ||
this._publisher = _publisher; | ||
} | ||
onModuleDestroy() { | ||
this.subscriptions.forEach((subscription) => subscription.unsubscribe()); | ||
} | ||
publish(event) { | ||
return this._publisher.publish(event); | ||
} | ||
publishAll(events) { | ||
if (this._publisher.publishAll) { | ||
return this._publisher.publishAll(events); | ||
} | ||
get publisher() { | ||
return this._publisher; | ||
} | ||
set publisher(_publisher) { | ||
this._publisher = _publisher; | ||
} | ||
onModuleDestroy() { | ||
this.subscriptions.forEach((subscription) => subscription.unsubscribe()); | ||
} | ||
publish(event) { | ||
return this._publisher.publish(event); | ||
} | ||
publishAll(events) { | ||
if (this._publisher.publishAll) { | ||
return this._publisher.publishAll(events); | ||
} | ||
return (events || []).map((event) => this._publisher.publish(event)); | ||
} | ||
bind(handler, name) { | ||
const stream$ = name ? this.ofEventName(name) : this.subject$; | ||
const subscription = stream$.subscribe((event) => handler.handle(event)); | ||
this.subscriptions.push(subscription); | ||
} | ||
registerSagas(types = []) { | ||
const sagas = types | ||
.map((target) => { | ||
const metadata = Reflect.getMetadata(constants_1.SAGA_METADATA, target) || []; | ||
const instance = this.moduleRef.get(target, { strict: false }); | ||
if (!instance) { | ||
throw new exceptions_1.InvalidSagaException(); | ||
} | ||
return metadata.map((key) => instance[key]); | ||
}) | ||
.reduce((a, b) => a.concat(b), []); | ||
sagas.forEach((saga) => this.registerSaga(saga)); | ||
} | ||
register(handlers = []) { | ||
handlers.forEach((handler) => this.registerHandler(handler)); | ||
} | ||
registerHandler(handler) { | ||
const instance = this.moduleRef.get(handler, { strict: false }); | ||
return (events || []).map((event) => this._publisher.publish(event)); | ||
} | ||
bind(handler, name) { | ||
const stream$ = name ? this.ofEventName(name) : this.subject$; | ||
const subscription = stream$.subscribe((event) => handler.handle(event)); | ||
this.subscriptions.push(subscription); | ||
} | ||
registerSagas(types = []) { | ||
const sagas = types | ||
.map((target) => { | ||
const metadata = Reflect.getMetadata(constants_1.SAGA_METADATA, target) || []; | ||
const instance = this.moduleRef.get(target, { strict: false }); | ||
if (!instance) { | ||
return; | ||
} | ||
const eventsNames = this.reflectEventsNames(handler); | ||
eventsNames.map((event) => this.bind(instance, event.name)); | ||
} | ||
ofEventName(name) { | ||
return this.subject$.pipe(operators_1.filter((event) => this.getEventName(event) === name)); | ||
} | ||
registerSaga(saga) { | ||
if (!util_1.isFunction(saga)) { | ||
throw new exceptions_1.InvalidSagaException(); | ||
} | ||
const stream$ = saga(this); | ||
if (!(stream$ instanceof rxjs_1.Observable)) { | ||
throw new exceptions_1.InvalidSagaException(); | ||
} | ||
const subscription = stream$ | ||
.pipe(operators_1.filter((e) => !!e)) | ||
.subscribe((command) => this.commandBus.execute(command)); | ||
this.subscriptions.push(subscription); | ||
return metadata.map((key) => instance[key].bind(instance)); | ||
}) | ||
.reduce((a, b) => a.concat(b), []); | ||
sagas.forEach((saga) => this.registerSaga(saga)); | ||
} | ||
register(handlers = []) { | ||
handlers.forEach((handler) => this.registerHandler(handler)); | ||
} | ||
registerHandler(handler) { | ||
const instance = this.moduleRef.get(handler, { strict: false }); | ||
if (!instance) { | ||
return; | ||
} | ||
reflectEventsNames(handler) { | ||
return Reflect.getMetadata(constants_1.EVENTS_HANDLER_METADATA, handler); | ||
const eventsNames = this.reflectEventsNames(handler); | ||
eventsNames.map((event) => this.bind(instance, event.name)); | ||
} | ||
ofEventName(name) { | ||
return this.subject$.pipe(operators_1.filter((event) => this.getEventName(event) === name)); | ||
} | ||
registerSaga(saga) { | ||
if (!util_1.isFunction(saga)) { | ||
throw new exceptions_1.InvalidSagaException(); | ||
} | ||
useDefaultPublisher() { | ||
this._publisher = new default_pubsub_1.DefaultPubSub(this.subject$); | ||
const stream$ = saga(this); | ||
if (!(stream$ instanceof rxjs_1.Observable)) { | ||
throw new exceptions_1.InvalidSagaException(); | ||
} | ||
}; | ||
EventBus = __decorate([ | ||
common_1.Injectable(), | ||
__metadata("design:paramtypes", [command_bus_1.CommandBus, | ||
core_1.ModuleRef]) | ||
], EventBus); | ||
return EventBus; | ||
})(); | ||
const subscription = stream$ | ||
.pipe(operators_1.filter((e) => !!e)) | ||
.subscribe((command) => this.commandBus.execute(command)); | ||
this.subscriptions.push(subscription); | ||
} | ||
reflectEventsNames(handler) { | ||
return Reflect.getMetadata(constants_1.EVENTS_HANDLER_METADATA, handler); | ||
} | ||
useDefaultPublisher() { | ||
this._publisher = new default_pubsub_1.DefaultPubSub(this.subject$); | ||
} | ||
}; | ||
EventBus = __decorate([ | ||
common_1.Injectable(), | ||
__metadata("design:paramtypes", [command_bus_1.CommandBus, | ||
core_1.ModuleRef]) | ||
], EventBus); | ||
exports.EventBus = EventBus; |
@@ -15,29 +15,32 @@ "use strict"; | ||
const event_bus_1 = require("./event-bus"); | ||
let EventPublisher = (() => { | ||
let EventPublisher = class EventPublisher { | ||
constructor(eventBus) { | ||
this.eventBus = eventBus; | ||
} | ||
mergeClassContext(metatype) { | ||
const eventBus = this.eventBus; | ||
return class extends metatype { | ||
publish(event) { | ||
eventBus.publish(event); | ||
} | ||
}; | ||
} | ||
mergeObjectContext(object) { | ||
const eventBus = this.eventBus; | ||
object.publish = (event) => { | ||
let EventPublisher = class EventPublisher { | ||
constructor(eventBus) { | ||
this.eventBus = eventBus; | ||
} | ||
mergeClassContext(metatype) { | ||
const eventBus = this.eventBus; | ||
return class extends metatype { | ||
publish(event) { | ||
eventBus.publish(event); | ||
}; | ||
return object; | ||
} | ||
}; | ||
EventPublisher = __decorate([ | ||
common_1.Injectable(), | ||
__metadata("design:paramtypes", [event_bus_1.EventBus]) | ||
], EventPublisher); | ||
return EventPublisher; | ||
})(); | ||
} | ||
publishAll(events) { | ||
eventBus.publishAll(events); | ||
} | ||
}; | ||
} | ||
mergeObjectContext(object) { | ||
const eventBus = this.eventBus; | ||
object.publish = (event) => { | ||
eventBus.publish(event); | ||
}; | ||
object.publishAll = (events) => { | ||
eventBus.publishAll(events); | ||
}; | ||
return object; | ||
} | ||
}; | ||
EventPublisher = __decorate([ | ||
common_1.Injectable(), | ||
__metadata("design:paramtypes", [event_bus_1.EventBus]) | ||
], EventPublisher); | ||
exports.EventPublisher = EventPublisher; |
@@ -10,4 +10,4 @@ "use strict"; | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); | ||
} | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -14,0 +14,0 @@ __exportStar(require("./command-not-found.exception"), exports); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.defaultGetEventName = void 0; | ||
exports.defaultGetEventName = (event) => { | ||
const defaultGetEventName = (event) => { | ||
const { constructor } = Object.getPrototypeOf(event); | ||
return constructor.name; | ||
}; | ||
exports.defaultGetEventName = defaultGetEventName; |
"use strict"; | ||
/* | ||
* Nest CQRS Module | ||
* Copyright(c) 2017-... Kamil Mysliwiec | ||
* www.kamilmysliwiec.com | ||
* MIT Licensed | ||
*/ | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
@@ -10,4 +16,4 @@ if (k2 === undefined) k2 = k; | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); | ||
} | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -14,0 +20,0 @@ __exportStar(require("./aggregate-root"), exports); |
import { ICommand } from './command.interface'; | ||
export interface ICommandBus<CommandBase extends ICommand = ICommand> { | ||
execute<T extends CommandBase>(command: T): Promise<any>; | ||
execute<T extends CommandBase, R = any>(command: T): Promise<R>; | ||
} |
@@ -10,4 +10,4 @@ "use strict"; | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); | ||
} | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -14,0 +14,0 @@ __exportStar(require("./commands/command-bus.interface"), exports); |
@@ -10,5 +10,5 @@ "use strict"; | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); | ||
} | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__exportStar(require("./of-type"), exports); |
import { Type } from '@nestjs/common'; | ||
import { Observable } from 'rxjs'; | ||
import { IEvent } from '../interfaces'; | ||
/** | ||
* Filter values depending on their instance type (comparison is made | ||
* using native `instanceof`). | ||
* | ||
* @param types List of types implementing `IEvent`. | ||
* | ||
* @return A stream only emitting the filtered instances. | ||
*/ | ||
export declare function ofType<TInput extends IEvent, TOutput extends IEvent>(...types: Type<TOutput>[]): (source: Observable<TInput>) => Observable<TOutput>; |
@@ -5,2 +5,10 @@ "use strict"; | ||
const operators_1 = require("rxjs/operators"); | ||
/** | ||
* Filter values depending on their instance type (comparison is made | ||
* using native `instanceof`). | ||
* | ||
* @param types List of types implementing `IEvent`. | ||
* | ||
* @return A stream only emitting the filtered instances. | ||
*/ | ||
function ofType(...types) { | ||
@@ -7,0 +15,0 @@ const isInstanceOf = (event) => !!types.find((classType) => event instanceof classType); |
@@ -30,62 +30,59 @@ "use strict"; | ||
const observable_bus_1 = require("./utils/observable-bus"); | ||
let QueryBus = (() => { | ||
let QueryBus = class QueryBus extends observable_bus_1.ObservableBus { | ||
constructor(moduleRef) { | ||
super(); | ||
this.moduleRef = moduleRef; | ||
this.handlers = new Map(); | ||
this.useDefaultPublisher(); | ||
} | ||
get publisher() { | ||
return this._publisher; | ||
} | ||
set publisher(_publisher) { | ||
this._publisher = _publisher; | ||
} | ||
execute(query) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const queryName = this.getQueryName(query); | ||
const handler = this.handlers.get(queryName); | ||
if (!handler) { | ||
throw new exceptions_1.QueryHandlerNotFoundException(queryName); | ||
} | ||
this.subject$.next(query); | ||
const result = yield handler.execute(query); | ||
return result; | ||
}); | ||
} | ||
bind(handler, name) { | ||
this.handlers.set(name, handler); | ||
} | ||
register(handlers = []) { | ||
handlers.forEach((handler) => this.registerHandler(handler)); | ||
} | ||
registerHandler(handler) { | ||
const instance = this.moduleRef.get(handler, { strict: false }); | ||
if (!instance) { | ||
return; | ||
let QueryBus = class QueryBus extends observable_bus_1.ObservableBus { | ||
constructor(moduleRef) { | ||
super(); | ||
this.moduleRef = moduleRef; | ||
this.handlers = new Map(); | ||
this.useDefaultPublisher(); | ||
} | ||
get publisher() { | ||
return this._publisher; | ||
} | ||
set publisher(_publisher) { | ||
this._publisher = _publisher; | ||
} | ||
execute(query) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const queryName = this.getQueryName(query); | ||
const handler = this.handlers.get(queryName); | ||
if (!handler) { | ||
throw new exceptions_1.QueryHandlerNotFoundException(queryName); | ||
} | ||
const target = this.reflectQueryName(handler); | ||
if (!target) { | ||
throw new invalid_query_handler_exception_1.InvalidQueryHandlerException(); | ||
} | ||
this.bind(instance, target.name); | ||
this.subject$.next(query); | ||
const result = yield handler.execute(query); | ||
return result; | ||
}); | ||
} | ||
bind(handler, name) { | ||
this.handlers.set(name, handler); | ||
} | ||
register(handlers = []) { | ||
handlers.forEach((handler) => this.registerHandler(handler)); | ||
} | ||
registerHandler(handler) { | ||
const instance = this.moduleRef.get(handler, { strict: false }); | ||
if (!instance) { | ||
return; | ||
} | ||
getQueryName(query) { | ||
const { constructor } = Object.getPrototypeOf(query); | ||
return constructor.name; | ||
const target = this.reflectQueryName(handler); | ||
if (!target) { | ||
throw new invalid_query_handler_exception_1.InvalidQueryHandlerException(); | ||
} | ||
reflectQueryName(handler) { | ||
return Reflect.getMetadata(constants_1.QUERY_HANDLER_METADATA, handler); | ||
} | ||
useDefaultPublisher() { | ||
this._publisher = new default_query_pubsub_1.DefaultQueryPubSub(this.subject$); | ||
} | ||
}; | ||
QueryBus = __decorate([ | ||
common_1.Injectable(), | ||
__metadata("design:paramtypes", [core_1.ModuleRef]) | ||
], QueryBus); | ||
return QueryBus; | ||
})(); | ||
this.bind(instance, target.name); | ||
} | ||
getQueryName(query) { | ||
const { constructor } = Object.getPrototypeOf(query); | ||
return constructor.name; | ||
} | ||
reflectQueryName(handler) { | ||
return Reflect.getMetadata(constants_1.QUERY_HANDLER_METADATA, handler); | ||
} | ||
useDefaultPublisher() { | ||
this._publisher = new default_query_pubsub_1.DefaultQueryPubSub(this.subject$); | ||
} | ||
}; | ||
QueryBus = __decorate([ | ||
common_1.Injectable(), | ||
__metadata("design:paramtypes", [core_1.ModuleRef]) | ||
], QueryBus); | ||
exports.QueryBus = QueryBus; |
@@ -16,42 +16,39 @@ "use strict"; | ||
const constants_1 = require("../decorators/constants"); | ||
let ExplorerService = (() => { | ||
let ExplorerService = class ExplorerService { | ||
constructor(modulesContainer) { | ||
this.modulesContainer = modulesContainer; | ||
let ExplorerService = class ExplorerService { | ||
constructor(modulesContainer) { | ||
this.modulesContainer = modulesContainer; | ||
} | ||
explore() { | ||
const modules = [...this.modulesContainer.values()]; | ||
const commands = this.flatMap(modules, (instance) => this.filterProvider(instance, constants_1.COMMAND_HANDLER_METADATA)); | ||
const queries = this.flatMap(modules, (instance) => this.filterProvider(instance, constants_1.QUERY_HANDLER_METADATA)); | ||
const events = this.flatMap(modules, (instance) => this.filterProvider(instance, constants_1.EVENTS_HANDLER_METADATA)); | ||
const sagas = this.flatMap(modules, (instance) => this.filterProvider(instance, constants_1.SAGA_METADATA)); | ||
return { commands, queries, events, sagas }; | ||
} | ||
flatMap(modules, callback) { | ||
const items = modules | ||
.map((module) => [...module.providers.values()].map(callback)) | ||
.reduce((a, b) => a.concat(b), []); | ||
return items.filter((element) => !!element); | ||
} | ||
filterProvider(wrapper, metadataKey) { | ||
const { instance } = wrapper; | ||
if (!instance) { | ||
return undefined; | ||
} | ||
explore() { | ||
const modules = [...this.modulesContainer.values()]; | ||
const commands = this.flatMap(modules, (instance) => this.filterProvider(instance, constants_1.COMMAND_HANDLER_METADATA)); | ||
const queries = this.flatMap(modules, (instance) => this.filterProvider(instance, constants_1.QUERY_HANDLER_METADATA)); | ||
const events = this.flatMap(modules, (instance) => this.filterProvider(instance, constants_1.EVENTS_HANDLER_METADATA)); | ||
const sagas = this.flatMap(modules, (instance) => this.filterProvider(instance, constants_1.SAGA_METADATA)); | ||
return { commands, queries, events, sagas }; | ||
return this.extractMetadata(instance, metadataKey); | ||
} | ||
extractMetadata(instance, metadataKey) { | ||
if (!instance.constructor) { | ||
return; | ||
} | ||
flatMap(modules, callback) { | ||
const items = modules | ||
.map((module) => [...module.providers.values()].map(callback)) | ||
.reduce((a, b) => a.concat(b), []); | ||
return items.filter((element) => !!element); | ||
} | ||
filterProvider(wrapper, metadataKey) { | ||
const { instance } = wrapper; | ||
if (!instance) { | ||
return undefined; | ||
} | ||
return this.extractMetadata(instance, metadataKey); | ||
} | ||
extractMetadata(instance, metadataKey) { | ||
if (!instance.constructor) { | ||
return; | ||
} | ||
const metadata = Reflect.getMetadata(metadataKey, instance.constructor); | ||
return metadata ? instance.constructor : undefined; | ||
} | ||
}; | ||
ExplorerService = __decorate([ | ||
common_1.Injectable(), | ||
__metadata("design:paramtypes", [modules_container_1.ModulesContainer]) | ||
], ExplorerService); | ||
return ExplorerService; | ||
})(); | ||
const metadata = Reflect.getMetadata(metadataKey, instance.constructor); | ||
return metadata ? instance.constructor : undefined; | ||
} | ||
}; | ||
ExplorerService = __decorate([ | ||
common_1.Injectable(), | ||
__metadata("design:paramtypes", [modules_container_1.ModulesContainer]) | ||
], ExplorerService); | ||
exports.ExplorerService = ExplorerService; |
@@ -10,5 +10,5 @@ "use strict"; | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); | ||
} | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__exportStar(require("./observable-bus"), exports); |
{ | ||
"name": "@nestjs/cqrs", | ||
"version": "7.0.1", | ||
"version": "8.0.0", | ||
"description": "A lightweight CQRS module for Nest framework (node.js)", | ||
@@ -20,28 +20,28 @@ "license": "MIT", | ||
"devDependencies": { | ||
"@commitlint/cli": "9.1.2", | ||
"@commitlint/config-angular": "9.1.2", | ||
"@nestjs/common": "7.4.2", | ||
"@nestjs/core": "7.4.2", | ||
"@types/node": "11.15.2", | ||
"@types/jest": "26.0.10", | ||
"@typescript-eslint/eslint-plugin": "3.9.1", | ||
"@typescript-eslint/parser": "3.9.1", | ||
"eslint": "7.7.0", | ||
"eslint-config-prettier": "6.11.0", | ||
"eslint-plugin-import": "2.22.0", | ||
"husky": "4.2.5", | ||
"jest": "26.4.0", | ||
"lint-staged": "10.2.11", | ||
"prettier": "2.0.5", | ||
"@commitlint/cli": "12.1.4", | ||
"@commitlint/config-angular": "12.1.4", | ||
"@nestjs/common": "7.6.18", | ||
"@nestjs/core": "7.6.18", | ||
"@types/node": "16.0.1", | ||
"@types/jest": "26.0.24", | ||
"@typescript-eslint/eslint-plugin": "4.28.2", | ||
"@typescript-eslint/parser": "4.28.2", | ||
"eslint": "7.30.0", | ||
"eslint-config-prettier": "8.3.0", | ||
"eslint-plugin-import": "2.23.4", | ||
"husky": "7.0.1", | ||
"jest": "27.0.6", | ||
"lint-staged": "11.0.0", | ||
"prettier": "2.3.2", | ||
"reflect-metadata": "0.1.13", | ||
"release-it": "13.6.8", | ||
"rxjs": "6.6.2", | ||
"ts-jest": "26.2.0", | ||
"typescript": "3.9.7" | ||
"release-it": "14.10.0", | ||
"rxjs": "6.6.7", | ||
"ts-jest": "27.0.3", | ||
"typescript": "4.3.5" | ||
}, | ||
"peerDependencies": { | ||
"@nestjs/common": "^6.0.0 || ^7.0.0", | ||
"@nestjs/core": "^6.0.0 || ^7.0.0", | ||
"@nestjs/common": "^8.0.0", | ||
"@nestjs/core": "^8.0.0", | ||
"reflect-metadata": "0.1.13", | ||
"rxjs": "^6.4.0" | ||
"rxjs": "^7.2.0" | ||
}, | ||
@@ -48,0 +48,0 @@ "lint-staged": { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
74691
119
1279