Skip to content

Commit

Permalink
Hotfix/2.3.1 (#66)
Browse files Browse the repository at this point in the history
* Added debug info

* Hotfix: added read bus default

* 2.3.1

* Rmoved package-lock.json

* Bug fixed

* Added nestjs-context as peer dep
  • Loading branch information
Jacob Dharandas Méndez authored Jun 29, 2021
1 parent 07db186 commit 7f3cd6d
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 32 deletions.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nestjs-geteventstore",
"version": "2.3.0",
"version": "2.3.1",
"description": "Event Store connector for NestJS-Cqrs",
"author": "Vincent Vermersch <vincent.vermersch@prestashop.com >",
"contributors": [
Expand Down Expand Up @@ -50,6 +50,7 @@
"@nestjs/terminus": "*",
"class-transformer": "*",
"class-validator": "*",
"nestjs-context": "^0.11.0",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^6.6.3"
Expand All @@ -62,7 +63,6 @@
"dependencies": {
"geteventstore-promise": "3.2.5",
"lodash": "^4.17.20",
"nestjs-context": "^0.2.0",
"node-eventstore-client": "0.2.18",
"uuid": "^8.3.2"
},
Expand All @@ -79,6 +79,7 @@
"class-transformer": "^0.4.0",
"class-validator": "^0.13.1",
"jest": "^26.6.3",
"nestjs-context": "^0.11.0",
"prettier": "^2.2.1",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.0.0",
Expand All @@ -89,7 +90,7 @@
"tsconfig-paths": "^3.9.0",
"tslint": "^5.20.1",
"tslint-config-airbnb": "^5.11.2",
"typescript": "4.1.3"
"typescript": "^4.3.4"
},
"jest": {
"moduleFileExtensions": [
Expand Down
1 change: 1 addition & 0 deletions src/cloudevents/write-events-prepublish.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export class WriteEventsPrepublishService<
async prepare(events: T[]) {
const preparedEvents = [];
for (const event of events) {
this.logger.debug(`Preparing ${event.constructor.name}`);
const preparedEvent = event;
preparedEvent.metadata = {
...(event.metadata ?? {}),
Expand Down
12 changes: 9 additions & 3 deletions src/cqrs-event-store.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ export class CqrsEventStoreModule extends CqrsModule {
return {
module: CqrsEventStoreModule,
imports: [
ContextModule.registerWithDefaults(),
// @todo this is giving us some problems in subscriptions with SAGA !!!
// it does not take into account the @Global and generate 2 diff contexts
// ContextModule.register(),
...modules.map((module) => module.imports).flat(),
],
providers: [
Expand Down Expand Up @@ -150,8 +152,12 @@ export class CqrsEventStoreModule extends CqrsModule {
// Attention: modules order is important here, as ES service is registered
// at the end (and projections + subscriptions are passed in config)
const modules = [
this.registerWriteBus(eventStoreConfig, eventBusConfig.write),
this.registerReadBus(eventStoreConfig, eventBusConfig.read),
...(eventBusConfig.write
? [this.registerWriteBus(eventStoreConfig, eventBusConfig.write)]
: []),
...(eventBusConfig.read
? [this.registerReadBus(eventStoreConfig, eventBusConfig.read)]
: []),
registerEventStore(eventStoreConfig, eventStoreServiceConfig),
];

Expand Down
12 changes: 12 additions & 0 deletions src/cqrs/aggregate-root.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Logger } from '@nestjs/common';
import { IEvent } from '@nestjs/cqrs';
import { InvalidPublisherException } from '../exceptions/invalid-publisher.exception';

const INTERNAL_EVENTS = Symbol();
const IS_AUTO_COMMIT_ENABLED = Symbol();

export abstract class AggregateRoot<EventBase extends IEvent = IEvent> {
protected logger = new Logger(this.constructor.name);
public [IS_AUTO_COMMIT_ENABLED] = false;
private readonly [INTERNAL_EVENTS]: EventBase[] = [];
private readonly _publishers: Function[] = [];
Expand Down Expand Up @@ -48,6 +50,11 @@ export abstract class AggregateRoot<EventBase extends IEvent = IEvent> {
}

async commit() {
this.logger.debug(
`Aggregate will commit ${this.getUncommittedEvents().length} in ${
this.publishers.length
} publishers`,
);
for (const publisher of this.publishers) {
await publisher(this.getUncommittedEvents());
}
Expand All @@ -72,6 +79,11 @@ export abstract class AggregateRoot<EventBase extends IEvent = IEvent> {
event: T,
isFromHistory = false,
) {
this.logger.debug(
`Applying ${event.constructor.name} with${
!!this.autoCommit ? '' : 'out'
} autocommit`,
);
if (!isFromHistory) {
this.addEvent(event);
}
Expand Down
5 changes: 1 addition & 4 deletions src/cqrs/event-bus-prepublish.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ModuleRef } from '@nestjs/core';
import { Injectable, Logger } from '@nestjs/common';
import { Injectable } from '@nestjs/common';
import {
EventBusPrepublishPrepareCallbackType,
IBaseEvent,
Expand All @@ -12,7 +12,6 @@ import {
export class EventBusPrepublishService<
EventBase extends IBaseEvent = IBaseEvent
> {
private readonly logger = new Logger(this.constructor.name);
constructor(private readonly moduleRef: ModuleRef) {}

private async getProvider<
Expand All @@ -35,7 +34,6 @@ export class EventBusPrepublishService<
if (!validate) {
return true;
}
this.logger.debug('validating events...');
const validator =
(await this.getProvider<IEventBusPrepublishValidateProvider<T>>(
validate,
Expand All @@ -60,7 +58,6 @@ export class EventBusPrepublishService<
if (!prepare) {
return events;
}
this.logger.debug('preparing events...');
const provider = await this.getProvider<
IEventBusPrepublishPrepareProvider<T>
>(prepare);
Expand Down
6 changes: 5 additions & 1 deletion src/cqrs/read-event-bus.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CommandBus, EventBus as Parent } from '@nestjs/cqrs';
import { Injectable } from '@nestjs/common';
import { Injectable, Logger } from '@nestjs/common';
import {
ReadEventOptionsType,
IReadEvent,
Expand All @@ -15,6 +15,7 @@ import { EventBusPrepublishService } from './event-bus-prepublish.service';
export class ReadEventBus<
EventBase extends IReadEvent = IReadEvent
> extends Parent<EventBase> {
private logger = new Logger(this.constructor.name);
constructor(
@Inject(READ_EVENT_BUS_CONFIG)
private readonly config: ReadEventBusConfigType<EventBase>,
Expand All @@ -23,15 +24,18 @@ export class ReadEventBus<
moduleRef: ModuleRef,
) {
super(commandBus, moduleRef);
this.logger.debug('Registering Read EventBus for EventStore...');
}
async publish<T extends EventBase = EventBase>(event: T) {
this.logger.debug('Publish in read bus');
const preparedEvents = await this.prepublish.prepare(this.config, [event]);
if (!(await this.prepublish.validate(this.config, preparedEvents))) {
return;
}
return super.publish(preparedEvents[0]);
}
async publishAll<T extends EventBase = EventBase>(events: T[]) {
this.logger.debug('Publish all in read bus');
const preparedEvents = await this.prepublish.prepare(this.config, events);
if (!(await this.prepublish.validate(this.config, preparedEvents))) {
return;
Expand Down
7 changes: 4 additions & 3 deletions src/cqrs/write-event-bus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export class WriteEventBus<
moduleRef: ModuleRef,
) {
super(commandBus, moduleRef);
this.logger.debug('Registering Write EventBus for EventStore...');
this.publisher = new EventStorePublisher<EventBase>(
this.eventstore,
this.config,
Expand All @@ -34,10 +35,12 @@ export class WriteEventBus<
expectedVersion?: ExpectedVersion,
streamName?: string,
): Promise<any> {
this.logger.debug('Publish in read bus');
const preparedEvents = await this.prepublish.prepare(this.config, [event]);
if (!(await this.prepublish.validate(this.config, preparedEvents))) {
return;
}
this.logger.debug(`Publishing ${event.constructor.name} in ${streamName}`);
return await this.publisher.publish<T>(
preparedEvents,
// @ts-ignore
Expand All @@ -51,13 +54,11 @@ export class WriteEventBus<
expectedVersion?: ExpectedVersion,
streamName?: string,
): Promise<any> {
this.logger.debug(`preparing ${events.length} events`);
this.logger.debug('Publish All in write bus');
const preparedEvents = await this.prepublish.prepare(this.config, events);
this.logger.debug(`validating ${preparedEvents.length} events`);
if (!(await this.prepublish.validate(this.config, preparedEvents))) {
return;
}
this.logger.debug(`prepared && validated ${preparedEvents.length} events`);
return await this.publisher.publishAll(
preparedEvents,
// @ts-ignore
Expand Down
5 changes: 5 additions & 0 deletions src/event-store/event-store-aggregate-root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ export abstract class EventStoreAggregateRoot<
}

async commit(expectedVersion: ExpectedVersion = ExpectedVersion.Any) {
this.logger.debug(
`Aggregate will commit ${this.getUncommittedEvents().length} events in ${
this.publishers.length
} publishers`,
);
for (const publisher of this.publishers) {
await publisher(
this.getUncommittedEvents(),
Expand Down
93 changes: 75 additions & 18 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1567,7 +1567,32 @@ __metadata:
languageName: node
linkType: hard

"@nestjs/common@npm:^7.5.1, @nestjs/common@npm:^7.6.15":
"@nestjs/common@npm:>=7.6.7":
version: 7.6.18
resolution: "@nestjs/common@npm:7.6.18"
dependencies:
axios: 0.21.1
iterare: 1.2.1
tslib: 2.2.0
uuid: 8.3.2
peerDependencies:
cache-manager: "*"
class-transformer: "*"
class-validator: "*"
reflect-metadata: ^0.1.12
rxjs: ^6.0.0
peerDependenciesMeta:
cache-manager:
optional: true
class-transformer:
optional: true
class-validator:
optional: true
checksum: 464e98a2f6b7a46f8065a388a6283626b33e29e9c73e66dda103d25ca85d638c404f91b0c153dc90646c1cd988a81dc3c8acfa364f0f01de73675870273bf0f2
languageName: node
linkType: hard

"@nestjs/common@npm:^7.6.15":
version: 7.6.15
resolution: "@nestjs/common@npm:7.6.15"
dependencies:
Expand All @@ -1592,7 +1617,36 @@ __metadata:
languageName: node
linkType: hard

"@nestjs/core@npm:^7.5.1, @nestjs/core@npm:^7.6.15":
"@nestjs/core@npm:>=7.6.7":
version: 7.6.18
resolution: "@nestjs/core@npm:7.6.18"
dependencies:
"@nuxtjs/opencollective": 0.3.2
fast-safe-stringify: 2.0.7
iterare: 1.2.1
object-hash: 2.1.1
path-to-regexp: 3.2.0
tslib: 2.2.0
uuid: 8.3.2
peerDependencies:
"@nestjs/common": ^7.0.0
"@nestjs/microservices": ^7.0.0
"@nestjs/platform-express": ^7.0.0
"@nestjs/websockets": ^7.0.0
reflect-metadata: ^0.1.12
rxjs: ^6.0.0
peerDependenciesMeta:
"@nestjs/microservices":
optional: true
"@nestjs/platform-express":
optional: true
"@nestjs/websockets":
optional: true
checksum: 8867f5419f392f3e0851ad910f24ed3e42ff1051a77d98bf04242f6fa1033242b8c11eefa7d7736b7424aba2fd667724bf88b1483bf115f1cc73006fcf699fe7
languageName: node
linkType: hard

"@nestjs/core@npm:^7.6.15":
version: 7.6.15
resolution: "@nestjs/core@npm:7.6.15"
dependencies:
Expand Down Expand Up @@ -7043,12 +7097,12 @@ __metadata:
languageName: node
linkType: hard

"nestjs-context@npm:^0.2.0":
version: 0.2.0
resolution: "nestjs-context@npm:0.2.0"
"nestjs-context@npm:^0.11.0":
version: 0.11.0
resolution: "nestjs-context@npm:0.11.0"
dependencies:
"@nestjs/common": ^7.5.1
"@nestjs/core": ^7.5.1
"@nestjs/common": ">=7.6.7"
"@nestjs/core": ">=7.6.7"
"@nestjs/graphql": ^7.10.3
"@nestjs/platform-express": ^7.5.1
apollo-server-express: ^2.23.0
Expand All @@ -7059,7 +7113,10 @@ __metadata:
rimraf: ^3.0.2
rxjs: ^6.6.3
short-uuid: ^4.1.0
checksum: df11f6b42b2c73bca4a3ef02aef6032cd9b11fb1d787f233f0e191d1561510f7cf38bad52f41459ba40ac5ddbb3df6ee11ca177d6dbae010f2dec28423b6f2d5
peerDependencies:
"@nestjs/common": ">=7.6.7"
"@nestjs/core": ">=7.6.7"
checksum: a87e21dd59f48ee3a14b2f549482768d28af7450bdc1bfd0c088a32d2eb8064c8b10dff5dd6facdd1eab3d6a95a66c96713553530fe40164a1d4e29538b491b3
languageName: node
linkType: hard

Expand All @@ -7081,7 +7138,7 @@ __metadata:
geteventstore-promise: 3.2.5
jest: ^26.6.3
lodash: ^4.17.20
nestjs-context: ^0.2.0
nestjs-context: ^0.11.0
node-eventstore-client: 0.2.18
prettier: ^2.2.1
reflect-metadata: ^0.1.13
Expand All @@ -7093,7 +7150,7 @@ __metadata:
tsconfig-paths: ^3.9.0
tslint: ^5.20.1
tslint-config-airbnb: ^5.11.2
typescript: 4.1.3
typescript: ^4.3.4
uuid: ^8.3.2
peerDependencies:
"@nestjs/common": "*"
Expand Down Expand Up @@ -9464,23 +9521,23 @@ __metadata:
languageName: node
linkType: hard

typescript@4.1.3:
version: 4.1.3
resolution: "typescript@npm:4.1.3"
typescript@^4.3.4:
version: 4.3.4
resolution: "typescript@npm:4.3.4"
bin:
tsc: bin/tsc
tsserver: bin/tsserver
checksum: 4f7ab1506ea22c7a1c313ec5b4285e93ce08d709ad6086d02d3096adb399ca339972ee56d1e578213c51dd0fb7b0fad50283c2d3c39642405644458ae29774f8
checksum: 9791c5fe98db6cb70ec0efad8d3d2eb957b9643ee600b90bff49267de69768d0d5f59c41a8fd731cfd63df1ccfb2cb25505a2015d7b39ac10ea51f605a058e16
languageName: node
linkType: hard

"typescript@patch:typescript@4.1.3#builtin<compat/typescript>":
version: 4.1.3
resolution: "typescript@patch:typescript@npm%3A4.1.3#builtin<compat/typescript>::version=4.1.3&hash=a45b0e"
"typescript@patch:typescript@^4.3.4#builtin<compat/typescript>":
version: 4.3.4
resolution: "typescript@patch:typescript@npm%3A4.3.4#builtin<compat/typescript>::version=4.3.4&hash=ddfc1b"
bin:
tsc: bin/tsc
tsserver: bin/tsserver
checksum: 28f0f616bf6284543bb14d4bfe0e1a74963e5df0396ebccfc6a57bfd4ab70b4284e51b3a2045ed2466151a4a58c8a549edb886a2f7a2232cf08fcf73496592e6
checksum: c8766e84a4f71e925cc059af8286cc7f41819fe821d433b90c83e9b36729cbda7a8e4be3292d8a1433cd717d529c93217d58d173418f43862290b206bd88b284
languageName: node
linkType: hard

Expand Down

0 comments on commit 7f3cd6d

Please sign in to comment.