daypaio/nestjs-eventstore

Support for event metadata when writing to EventStore

Opened this issue · 0 comments

Hi,
I'm using NestJS CQRS architecture with nestjs-eventstore package for emitting event to EventStore and I'm planning to implement a middleware for pub/sub of events to handle things like logging and and setting some default event metadata.

Besides the fact that it's going to be tricky because of lack of support for any kind of events interception when publishing (this is not the subject of this issue, but any ideas are welcome too) there is one more fundamental problem: lack of support for setting event metadata when writing an event to EventStore.

The critical place in the code seems to be EventStoreBus.publish method:

async publish(event: IEvent, stream?: string) {
const payload: EventData = createEventData(
v4(),
event.constructor.name,
true,
Buffer.from(JSON.stringify(event)),
);

which just doesn't support metadata (it would be a fifth argument passed to createEventData)

I would imagine this could be solved by adding another custom field in the event object and using it in EventPublisher, similarly to how currently streamName is handled:

mergeObjectContext<T extends AggregateRoot>(object: T): T {
const eventBus = this.eventBus;
object.publish = (event: IEvent) => {
eventBus.publish(event, (event as IAggregateEvent).streamName);
};
return object;
}

My question is:
do you have plans to implement support for writing event metadata with the event or any experiences with implementing it? Is there anything I should know that I don't see at this point and that is a blocker or a more complex issue?

Thanks!