castore-dev / castore

Making Event Sourcing easy 😎
MIT License
225 stars 18 forks source link

ConnectedEventStore's onEventPushed isnt trigged for group events #186

Open kthompson opened 2 months ago

kthompson commented 2 months ago

Describe the bug When using a ConnectedEventStore that is connected to a message bus, the message bus does not receive events that are created via the EventStore.pushEventGroup method.

This seems to occur because onEventPushed is never called because when the GroupEvent is created, the eventStore that is attached to it is the inner EventStore rather than the ConnectedEventStore.

I think overriding the groupEvent method in ConnectedEventStore to assign the connectedEventStore after the GroupEvent is created would resolve this.

To Reproduce Steps to reproduce the behavior:

  1. Create EventStore "ProductEventStore"
  2. Create bus instance of MessageBus implementation of NotificationMessageBus<ProductEventStore>
  3. Create connected instance of ProductEventStore, ie `new ConnectedEventStore(ProductEventStore, bus);
  4. make a call to EventStore.pushGroupEvent(...)
  5. No events come into the bus

Expected behavior We should see all of the events that were pushed come into the bus. They get committed to the EventStore but are not seen on the bus.

Additional context You can work around the issue by assigning the connected bus for each group event created. ie:

const event1 = connected.groupEvent({ ... });
event1.eventStore = connected;

// ...

EventStore.pushEventGroup(event1, event2, ...);
guillaumeduboc commented 1 week ago

I have the same issue

It seems that when setting the eventStore property, this represents an EventStore and not a ConnectedEventStore https://github.com/castore-dev/castore/blob/c9b9beab799ae0cf3d1ccd6a06ae93272305742d/packages/core/src/eventStore/eventStore.ts#L221

kthompson commented 22 hours ago

Yea my work around has been to do something like....

const event = eventStore.groupEvent({
  //  ....
});

event.eventStore = eventStore;

Where eventStore is your connected event store.