8.0.0 • Published 3 years ago

nestjs-async-cqrs v8.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

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.