Skip to content

Proposal: Spring-like TransactionalEventListener/TransactionalEventPublisher but for @nestjs/cqrs #443

@frct1

Description

@frct1

Hi,
We using nestjs-cls for all over the places of our backend and @transactional really solved tx propagation on request pipeline. As we actively using @nestjs/cqrs I guess there could be an option as Spring offer like TransactionalEventPublisher that binds to current tx execution and postpone event publish until commit.

Is there any options to do the same thing for nestjs-cls/transactional?
Probably it could be something like Observable that receives callbacks that fire events in them same order as received.

  // When transaction initialised we create ReplaySubject as storage for all events.
  const afterCommitEvents = new ReplaySubject<DomainEvent>();

  // During transaction - events buffered in order
  this.txHost.tx.afterCommitEvents.next(new ProjectCreatedEvent(...));
  this.txHost.tx.afterCommitEvents.next(new WalletCreatedEvent(...));

  // Once transaction commited and connection released proceeding all stored events.
  afterCommitEvents.subscribe(event => this.eventBus.publish(event));
  afterCommitEvents.complete();

Pros:

  • Confidence that events are triggered when transaction succeed

Cons:

  • nestjs-cls/transactional will depend on @nestjs/cqrs.

Comments:
Not sure how we can avoid dependency on cqrs package, but it could be just a feature requirement tbh and we can go with that.

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions