AggregateRoot.commit() implementation breaks transactional semantics
I'm submitting a...
[ ] Regression
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
Current behavior
The current implementation of AggregateRoot.prototype.commit() invokes this.publish() for each applied event one-by-one iteratively. Unfortunately, this prevents publishing all events as a unit transactionally, which IMHO defeats the purpose of a commit:
https://github.com/nestjs/cqrs/blob/9e4e21a6dbb7b3143ede21c37fd28cf1cd902fb4/src/aggregate-root.ts#L22-L25
Expected behavior
In practice, AggreateRoot.publish() currently forwards to IEventBus.publish() following a call to either EventPublisher.prototype.mergeObjectContext or EventPublisher.prototype.mergeClassContext. Instead of iteratively calling publish(), these APIs should use a publishAll API instead. Surprisingly, publishAll() already exists on the IEventBus interface but does not appear to be used anywhere.
What is the motivation / use case for changing the behavior?
Correct transactional handling of applied aggregate events
Also, thank you for nestjs. This is a wonderful project.
Thank you @goldsam! Would you like to create a PR to use publishAll() instead?
Shouldn't this issue be closed? Or is there something left to do / watch out about this topic?