juicycleff / nestjs-event-store

NestJS CQRS module for EventStore.org. It requires @nestjs/cqrs
https://xraph.com
MIT License
199 stars 29 forks source link

EventHanlers not called #6

Closed ibosev closed 4 years ago

ibosev commented 4 years ago

Hello,

I am trying to hook up your module to handle event sourcing in my project, but event handlers are never called. Events are being persisted in the event store, but I never get to the handle method in my cqrs event handlers. Do you use custom event publisher as daypaio does in his command handlers?

Below are snippets from my code:

command handler:

    const userAggregate = this.publisher.mergeObjectContext(
      Object.assign(new AuthUserDto(), userDto)
    );

    userAggregate.createUser();
    userAggregate.commit();

event impl:

export class UserCreatedEvent implements IEvent {
  constructor(
    public readonly userDto: AuthUserDto) {}
}

event handler:

@EventsHandler(UserCreatedEvent)
export class UserCreatedHandler
  implements IEventHandler<UserCreatedEvent> {
  handle(event: UserCreatedEvent) {
    Logger.log(event, 'UserCreatedEvent');
  }
}

es module register:

 EventStoreModule.registerFeature({
       ....
       ....
      eventHandlers: {
        UserCreatedEvent: (data) => new UserCreatedEvent(data),
       ....
      },
    }),

Edit: 1) I can confirm that normally without event-sourcing module events are being handled correctly - handle method in event handler is being called as expected. This is when using this.publisher.mergeObjectContext() and then commit() on aggregate root.

2) If I use EventBus directly and publish events from it, they are being handled as well: this.eventBus.publish(new UserCreatedEvent(userAggregate));

So the issue has something to do with EventPublisher from @nestjs/cqrs.

juicycleff commented 4 years ago

@ibosev yes I have seen this issue when using the Official CQRS module, it's been low hanging for a while now. See example usage if this library and how I use CQRS module here https://github.com/juicycleff/ultimate-backend/tree/next

juicycleff commented 4 years ago

Issue 6 has the fix