Socket
Socket
Sign inDemoInstall

@nestjs/cqrs

Package Overview
Dependencies
46
Maintainers
1
Versions
44
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 5.1.1 to 6.0.0

dist/constants.d.ts

10

dist/aggregate-root.d.ts
import { IEvent } from './interfaces/index';
declare const INTERNAL_EVENTS: unique symbol;
declare const IS_AUTO_COMMIT_ENABLED: unique symbol;
export declare abstract class AggregateRoot {
private readonly events;
[IS_AUTO_COMMIT_ENABLED]: boolean;
private readonly [INTERNAL_EVENTS];
autoCommit: boolean;

@@ -11,4 +14,5 @@ publish(event: IEvent): void;

apply(event: IEvent, isFromHistory?: boolean): void;
private getEventHandler(event);
private getEventName(event);
private getEventHandler;
private getEventName;
}
export {};
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var _a, _b;
const INTERNAL_EVENTS = Symbol();
const IS_AUTO_COMMIT_ENABLED = Symbol();
class AggregateRoot {
constructor() {
this.events = [];
this.autoCommit = false;
this[_a] = false;
this[_b] = [];
}
set autoCommit(value) {
this[IS_AUTO_COMMIT_ENABLED] = value;
}
get autoCommit() {
return this[IS_AUTO_COMMIT_ENABLED];
}
publish(event) { }
commit() {
this.events.forEach((event) => this.publish(event));
this.events.length = 0;
this[INTERNAL_EVENTS].forEach(event => this.publish(event));
this[INTERNAL_EVENTS].length = 0;
}
uncommit() {
this.events.length = 0;
this[INTERNAL_EVENTS].length = 0;
}
getUncommittedEvents() {
return this.events;
return this[INTERNAL_EVENTS];
}

@@ -24,3 +33,3 @@ loadFromHistory(history) {

if (!isFromHistory && !this.autoCommit) {
this.events.push(event);
this[INTERNAL_EVENTS].push(event);
}

@@ -40,2 +49,3 @@ this.autoCommit && this.publish(event);

}
_a = IS_AUTO_COMMIT_ENABLED, _b = INTERNAL_EVENTS;
exports.AggregateRoot = AggregateRoot;

@@ -0,16 +1,17 @@

import { Type } from '@nestjs/common';
import { ModuleRef } from '@nestjs/core';
import 'reflect-metadata';
import { Type } from '@nestjs/common';
import { ICommandBus, ICommand, ICommandHandler } from './interfaces/index';
import { ICommand, ICommandBus, ICommandHandler } from './interfaces/index';
import { ObservableBus } from './utils/observable-bus';
export declare type CommandHandlerMetatype = Type<ICommandHandler<ICommand>>;
export declare type CommandHandlerType = Type<ICommandHandler<ICommand>>;
export declare class CommandBus extends ObservableBus<ICommand> implements ICommandBus {
private readonly moduleRef;
private handlers;
private moduleRef;
setModuleRef(moduleRef: any): void;
constructor(moduleRef: ModuleRef);
execute<T extends ICommand>(command: T): Promise<any>;
bind<T extends ICommand>(handler: ICommandHandler<T>, name: string): void;
register(handlers: CommandHandlerMetatype[]): void;
protected registerHandler(handler: CommandHandlerMetatype): void;
private getCommandName(command);
private reflectCommandName(handler);
register(handlers?: CommandHandlerType[]): void;
protected registerHandler(handler: CommandHandlerType): void;
private getCommandName;
private reflectCommandName;
}

@@ -8,18 +8,19 @@ "use strict";

};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
const common_1 = require("@nestjs/common");
const core_1 = require("@nestjs/core");
require("reflect-metadata");
const common_1 = require("@nestjs/common");
const constants_1 = require("./decorators/constants");
const command_not_found_exception_1 = require("./exceptions/command-not-found.exception");
const index_1 = require("./index");
const observable_bus_1 = require("./utils/observable-bus");
const constants_1 = require("./utils/constants");
const index_1 = require("./index");
let CommandBus = class CommandBus extends observable_bus_1.ObservableBus {
constructor() {
super(...arguments);
constructor(moduleRef) {
super();
this.moduleRef = moduleRef;
this.handlers = new Map();
this.moduleRef = null;
}
setModuleRef(moduleRef) {
this.moduleRef = moduleRef;
}
execute(command) {

@@ -31,5 +32,3 @@ const handler = this.handlers.get(this.getCommandName(command));

this.subject$.next(command);
return new Promise(resolve => {
handler.execute(command, resolve);
});
return handler.execute(command);
}

@@ -39,12 +38,10 @@ bind(handler, name) {

}
register(handlers) {
register(handlers = []) {
handlers.forEach(handler => this.registerHandler(handler));
}
registerHandler(handler) {
if (!this.moduleRef) {
throw new index_1.InvalidModuleRefException();
const instance = this.moduleRef.get(handler, { strict: false });
if (!instance) {
return;
}
const instance = this.moduleRef.get(handler);
if (!instance)
return;
const target = this.reflectCommandName(handler);

@@ -65,4 +62,5 @@ if (!target) {

CommandBus = __decorate([
common_1.Injectable()
common_1.Injectable(),
__metadata("design:paramtypes", [core_1.ModuleRef])
], CommandBus);
exports.CommandBus = CommandBus;

@@ -1,2 +0,13 @@

export declare class CQRSModule {
import { OnModuleInit } from '@nestjs/common';
import { CommandBus } from './command-bus';
import { EventBus } from './event-bus';
import { QueryBus } from './query-bus';
import { ExplorerService } from './services/explorer.service';
export declare class CqrsModule implements OnModuleInit {
private readonly explorerService;
private readonly eventsBus;
private readonly commandsBus;
private readonly queryBus;
constructor(explorerService: ExplorerService, eventsBus: EventBus, commandsBus: CommandBus, queryBus: QueryBus);
onModuleInit(): void;
}

@@ -8,15 +8,37 @@ "use strict";

};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
const common_1 = require("@nestjs/common");
const command_bus_1 = require("./command-bus");
const event_bus_1 = require("./event-bus");
const event_publisher_1 = require("./event-publisher");
const event_bus_1 = require("./event-bus");
let CQRSModule = class CQRSModule {
const query_bus_1 = require("./query-bus");
const explorer_service_1 = require("./services/explorer.service");
let CqrsModule = class CqrsModule {
constructor(explorerService, eventsBus, commandsBus, queryBus) {
this.explorerService = explorerService;
this.eventsBus = eventsBus;
this.commandsBus = commandsBus;
this.queryBus = queryBus;
}
onModuleInit() {
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([
CqrsModule = __decorate([
common_1.Module({
providers: [command_bus_1.CommandBus, event_bus_1.EventBus, event_publisher_1.EventPublisher],
exports: [command_bus_1.CommandBus, event_bus_1.EventBus, event_publisher_1.EventPublisher],
})
], CQRSModule);
exports.CQRSModule = CQRSModule;
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;

@@ -1,29 +0,28 @@

import { Type } from '@nestjs/common';
import { OnModuleDestroy, Type } from '@nestjs/common';
import { ModuleRef } from '@nestjs/core';
import { Observable } from 'rxjs';
import { CommandBus } from './command-bus';
import { Saga } from './index';
import { IEventPublisher } from './interfaces/events/event-publisher.interface';
import { IEvent, IEventBus, IEventHandler } from './interfaces/index';
import { IEvent, IEventBus, IEventHandler, ISaga } from './interfaces/index';
import { ObservableBus } from './utils/observable-bus';
export declare type EventHandlerMetatype = Type<IEventHandler<IEvent>>;
export declare class EventBus extends ObservableBus<IEvent> implements IEventBus {
export declare type EventHandlerType = Type<IEventHandler<IEvent>>;
export declare class EventBus extends ObservableBus<IEvent> implements IEventBus, OnModuleDestroy {
private readonly commandBus;
private moduleRef;
private readonly moduleRef;
private _publisher;
constructor(commandBus: CommandBus);
private useDefaultPublisher();
setModuleRef(moduleRef: any): void;
private readonly subscriptions;
constructor(commandBus: CommandBus, moduleRef: ModuleRef);
publisher: IEventPublisher;
onModuleDestroy(): void;
publish<T extends IEvent>(event: T): void;
ofType<T extends IEvent>(event: T & {
name: string;
}): Observable<IEvent>;
bind<T extends IEvent>(handler: IEventHandler<IEvent>, name: string): void;
combineSagas(sagas: Saga[]): void;
register(handlers: EventHandlerMetatype[]): void;
protected registerHandler(handler: EventHandlerMetatype): void;
publishAll(events: IEvent[]): void;
bind(handler: IEventHandler<IEvent>, name: string): void;
registerSagas(types?: Type<any>[]): void;
register(handlers?: EventHandlerType[]): void;
protected registerHandler(handler: EventHandlerType): void;
protected ofEventName(name: string): Observable<IEvent>;
private getEventName(event);
protected registerSaga(saga: Saga): void;
private reflectEventsNames(handler);
publisher: IEventPublisher;
private getEventName;
protected registerSaga(saga: ISaga): void;
private reflectEventsNames;
private useDefaultPublisher;
}

@@ -13,48 +13,60 @@ "use strict";

const common_1 = require("@nestjs/common");
const core_1 = require("@nestjs/core");
const rxjs_1 = require("rxjs");
const operators_1 = require("rxjs/operators");
const util_1 = require("util");
const command_bus_1 = require("./command-bus");
const constants_1 = require("./decorators/constants");
const invalid_saga_exception_1 = require("./exceptions/invalid-saga.exception");
const index_1 = require("./index");
const constants_1 = require("./utils/constants");
const default_pubsub_1 = require("./utils/default-pubsub");
const default_pubsub_1 = require("./helpers/default-pubsub");
const observable_bus_1 = require("./utils/observable-bus");
let EventBus = class EventBus extends observable_bus_1.ObservableBus {
constructor(commandBus) {
constructor(commandBus, moduleRef) {
super();
this.commandBus = commandBus;
this.moduleRef = null;
this.moduleRef = moduleRef;
this.subscriptions = [];
this.useDefaultPublisher();
}
useDefaultPublisher() {
const pubSub = new default_pubsub_1.DefaultPubSub();
pubSub.bridgeEventsTo(this.subject$);
this._publisher = pubSub;
get publisher() {
return this._publisher;
}
setModuleRef(moduleRef) {
this.moduleRef = moduleRef;
set publisher(_publisher) {
this._publisher = _publisher;
}
onModuleDestroy() {
this.subscriptions.forEach(subscription => subscription.unsubscribe());
}
publish(event) {
this._publisher.publish(event);
}
ofType(event) {
return this.ofEventName(event.name);
publishAll(events) {
(events || []).forEach(event => this._publisher.publish(event));
}
bind(handler, name) {
const stream$ = name ? this.ofEventName(name) : this.subject$;
stream$.subscribe(event => handler.handle(event));
const subscription = stream$.subscribe(event => handler.handle(event));
this.subscriptions.push(subscription);
}
combineSagas(sagas) {
[].concat(sagas).map(saga => this.registerSaga(saga));
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 invalid_saga_exception_1.InvalidSagaException();
}
return metadata.map((key) => instance[key]);
})
.reduce((a, b) => a.concat(b), []);
sagas.forEach(saga => this.registerSaga(saga));
}
register(handlers) {
register(handlers = []) {
handlers.forEach(handler => this.registerHandler(handler));
}
registerHandler(handler) {
if (!this.moduleRef) {
throw new index_1.InvalidModuleRefException();
const instance = this.moduleRef.get(handler, { strict: false });
if (!instance) {
return;
}
const instance = this.moduleRef.get(handler);
if (!instance)
return;
const eventsNames = this.reflectEventsNames(handler);

@@ -71,2 +83,5 @@ eventsNames.map(event => this.bind(instance, event.name));

registerSaga(saga) {
if (!util_1.isFunction(saga)) {
throw new invalid_saga_exception_1.InvalidSagaException();
}
const stream$ = saga(this);

@@ -76,5 +91,6 @@ if (!(stream$ instanceof rxjs_1.Observable)) {

}
stream$
.pipe(operators_1.filter(e => e))
const subscription = stream$
.pipe(operators_1.filter(e => !!e))
.subscribe(command => this.commandBus.execute(command));
this.subscriptions.push(subscription);
}

@@ -84,13 +100,13 @@ reflectEventsNames(handler) {

}
get publisher() {
return this._publisher;
useDefaultPublisher() {
const pubSub = new default_pubsub_1.DefaultPubSub();
pubSub.bridgeEventsTo(this.subject$);
this._publisher = pubSub;
}
set publisher(_publisher) {
this._publisher = _publisher;
}
};
EventBus = __decorate([
common_1.Injectable(),
__metadata("design:paramtypes", [command_bus_1.CommandBus])
__metadata("design:paramtypes", [command_bus_1.CommandBus,
core_1.ModuleRef])
], EventBus);
exports.EventBus = EventBus;
export * from './command-not-found.exception';
export * from './invalid-saga.exception';
export * from './invalid-command-handler.exception';
export * from './invalid-events-handler.exception';
export * from './invalid-module-ref.exception';
export * from './invalid-query-handler.exception';
export * from './invalid-saga.exception';
export * from './query-not-found.exception';

@@ -7,5 +7,6 @@ "use strict";

__export(require("./command-not-found.exception"));
__export(require("./invalid-saga.exception"));
__export(require("./invalid-command-handler.exception"));
__export(require("./invalid-events-handler.exception"));
__export(require("./invalid-module-ref.exception"));
__export(require("./invalid-query-handler.exception"));
__export(require("./invalid-saga.exception"));
__export(require("./query-not-found.exception"));

@@ -5,5 +5,5 @@ "use strict";

constructor() {
super(`Invalid command handler exception. Define handled command using @CommandHandler() decorator`);
super(`Invalid command handler exception (missing @CommandHandler() decorator?)`);
}
}
exports.InvalidCommandHandlerException = InvalidCommandHandlerException;

@@ -5,5 +5,5 @@ "use strict";

constructor() {
super(`Invalid event handler exception. Define handled events using @EventsHandler() decorator`);
super(`Invalid event handler exception (missing @EventsHandler() decorator?)`);
}
}
exports.InvalidEventsHandlerException = InvalidEventsHandlerException;

@@ -5,5 +5,5 @@ "use strict";

constructor() {
super(`Invalid saga exception. Each saga should retuns an Observable object.`);
super(`Invalid saga exception. Each saga should return an Observable object`);
}
}
exports.InvalidSagaException = InvalidSagaException;
export * from './aggregate-root';
export * from './utils';
export * from './interfaces';
export * from './exceptions';
export * from './command-bus';
export * from './cqrs.module';
export * from './command-bus';
export * from './decorators';
export * from './event-bus';
export * from './event-publisher';
export * from './exceptions';
export * from './interfaces';
export * from './operators';
export * from './query-bus';
export * from './utils';

@@ -7,7 +7,10 @@ "use strict";

__export(require("./aggregate-root"));
__export(require("./utils"));
__export(require("./exceptions"));
__export(require("./command-bus"));
__export(require("./cqrs.module"));
__export(require("./command-bus"));
__export(require("./decorators"));
__export(require("./event-bus"));
__export(require("./event-publisher"));
__export(require("./exceptions"));
__export(require("./operators"));
__export(require("./query-bus"));
__export(require("./utils"));
import { ICommand } from './command.interface';
export interface ICommandHandler<T extends ICommand> {
execute(command: T, resolve: (value?) => void): any;
export interface ICommandHandler<T extends ICommand = any> {
execute(command: T): Promise<any>;
}
import { IEvent } from './event.interface';
export interface IEventBus {
publish<T extends IEvent>(event: T): any;
publishAll(events: IEvent[]): any;
}
import { IEvent } from './event.interface';
export interface IEventHandler<T extends IEvent> {
export interface IEventHandler<T extends IEvent = any> {
handle(event: T): any;
}

@@ -7,3 +7,6 @@ export * from './commands/command-bus.interface';

export * from './events/event.interface';
export * from './event-observable.interface';
export * from './queries/query-bus.interface';
export * from './queries/query-handler.interface';
export * from './queries/query-result.interface';
export * from './queries/query.interface';
export * from './saga.type';

@@ -1,3 +0,4 @@

import { EventObservable } from './event-observable.interface';
import { Observable } from 'rxjs';
import { ICommand } from './commands/command.interface';
import { IEvent } from './events/event.interface';
export declare type Saga = (events$: EventObservable<IEvent>) => any;
export declare type ISaga = (events$: Observable<IEvent>) => Observable<ICommand>;
export * from './observable-bus';
export * from './command-handler.decorator';
export * from './events-handler.decorator';

@@ -7,3 +7,1 @@ "use strict";

__export(require("./observable-bus"));
__export(require("./command-handler.decorator"));
__export(require("./events-handler.decorator"));

@@ -1,8 +0,5 @@

import { Subject, Observable } from 'rxjs';
import { EventObservable } from '../interfaces/event-observable.interface';
import { Type } from '@nestjs/common';
export declare class ObservableBus<T> extends Observable<T> implements EventObservable<T> {
import { Observable, Subject } from 'rxjs';
export declare class ObservableBus<T> extends Observable<T> {
protected subject$: Subject<T>;
constructor();
ofType(...types: Type<any>[]): Observable<T>;
}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const rxjs_1 = require("rxjs");
const operators_1 = require("rxjs/operators");
const isEmpty = array => !(array && array.length > 0);
class ObservableBus extends rxjs_1.Observable {

@@ -12,6 +10,3 @@ constructor() {

}
ofType(...types) {
return this.pipe(operators_1.filter(event => !isEmpty(types.filter(type => event instanceof type))));
}
}
exports.ObservableBus = ObservableBus;
{
"name": "@nestjs/cqrs",
"version": "5.1.1",
"version": "6.0.0",
"description": "A lightweight CQRS module for Nest framework (node.js)",
"license": "MIT",
"scripts": {
"test": "jest",
"build": "tsc -p tsconfig.json",

@@ -14,28 +15,22 @@ "precommit": "lint-staged",

"devDependencies": {
"@nestjs/common": "^5.0.0",
"@types/node": "^7.0.41",
"husky": "^0.14.3",
"lint-staged": "^7.0.0",
"prettier": "^1.11.1",
"reflect-metadata": "0.1.12",
"rxjs": "^6.0.0",
"rxjs-compat": "^6.2.1",
"typescript": "^2.8.0"
"@nestjs/common": "6.0.0",
"@types/node": "11.9.4",
"@types/jest": "23.3.14",
"husky": "0.14.3",
"jest": "24.1.0",
"lint-staged": "8.1.4",
"prettier": "1.11.1",
"rxjs": "6.4.0",
"ts-jest": "23.10.5",
"typescript": "3.3.3"
},
"peerDependencies": {
"@nestjs/common": "^5.0.0",
"@nestjs/common": "^6.0.0",
"@nestjs/core": "^6.0.0",
"reflect-metadata": "0.1.12",
"rxjs": "^6.0.0"
"rxjs": "^6.4.0"
},
"lint-staged": {
"*.ts": [
"prettier --write",
"git add"
]
},
"dependencies": {
"@nestjs/common": "^5.0.0",
"reflect-metadata": "^0.1.12",
"rxjs": "^6.1.0"
"*.ts": ["prettier --write", "git add"]
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc