nestjs-async-cqrs
TypeScript icon, indicating that this package has built-in type declarations

8.0.0 • Public • Published

Nest Logo

Installation

$ npm install --save nestjs-async-cqrs

Quick Start

Overview & CQRS Tutorial

The current CQRS module that ships with NestJS does not allow you to await the publishing of an event. This node module extends the NestJS CQRS module to allow for asynchronous publishing of events.

Example:

export class Hero extends AggregateRoot {
  constructor(private readonly id: string) {
    super();
  }

  async killEnemy(enemyId: string) {
    // logic
    await this.apply(new HeroKilledDragonEvent(this.id, enemyId));
  }

  async addItem(itemId: string) {
    // logic
    await this.apply(new HeroFoundItemEvent(this.id, itemId));
  }
}

@CommandHandler(KillDragonCommand)
export class KillDragonHandler implements ICommandHandler<KillDragonCommand> {
  constructor(
    private readonly repository: HeroRepository,
    private readonly publisher: EventPublisher,
  ) {}

  async execute(command: KillDragonCommand) {
    console.log(clc.greenBright('KillDragonCommand...'));

    const { heroId, dragonId } = command;
    const hero = this.publisher.mergeObjectContext(
      await this.repository.findOneById(+heroId),
    );
    await hero.killEnemy(dragonId);
    await hero.commit();
  }
}

This is achieved by modifying the AggregateRoot, EventBus and EventPublisher classes to support async methods.

Support

Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please read more here.

Stay in touch

License

Nest is MIT licensed.

Readme

Keywords

none

Package Sidebar

Install

npm i nestjs-async-cqrs

Weekly Downloads

14

Version

8.0.0

License

MIT

Unpacked Size

77.7 kB

Total Files

110

Last publish

Collaborators

  • bradsheppard