JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 166
  • Score
    100M100P100Q75665F
  • License ISC

NestJS module for implementing Event Sourcing.

Package Exports

  • event-sourcing-nestjs

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (event-sourcing-nestjs) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Event Sourcing for Nestjs

npm version

Library that implements event sourcing using NestJS and his CQRS library.

Features

  • StoreEventBus: A class that replaces Nest's EventBus to also persists events in mongodb.
  • StoreEventPublisher: A class that replaces Nest's EventPublisher.
  • ViewUpdaterHandler: The EventBus will also delegate the Events to his View Updaters, so you can update your read database.
  • Replay: You can re-run stored events. This will only trigger the view updater handlers to reconstruct your read db.

State of the art

State of the art

Getting started

npm install event-sourcing-nestjs @nestjs/cqrs --save

Usage

Importing

import { Module } from '@nestjs/common';
import { EventSourcingModule } from 'event-sourcing-nestjs';

@Module({
  imports: [
    EventSourcingModule.forRoot({
      mongoURL: 'mongodb://localhost:27017/eventstore',
    }),
  ],
})
export class ApplicationModule {}

Event emitter

Instead of using Nest's EventBus use StoreEventBus, so events will persist before their handlers are executed.

import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
import { StoreEventBus } from 'event-sourcing-nestjs';

@CommandHandler(CreateUserCommand)
export class CreateUserHandler implements ICommandHandler<CreateUserCommand> {

    constructor(
        private readonly eventBus: StoreEventBus,
    ) {}

    async execute(command: CreateUserCommand) {
        this.eventBus.publish(new UserCreatedEvent(command.name));
    }

}

Or use StoreEventPublisher if you want to dispatch event from your AggregateRoot.

View updaters

After emitting an event, use a view updater to update the read database state. This view updaters will be used to recontruct the db if needed.

import { IViewUpdater, ViewUpdater } from 'event-sourcing-nestjs';

@ViewUpdater(UserCreatedEvent)
export class UserCreatedUpdater implements IViewUpdater<UserCreatedEvent> {

    async handle(event: UserCreatedEvent) {
        // Save user into our view db
    }
}

Reconstructing the view db

Add this script to your package.json:

"reconstruct-view-db": "ts-node -r tsconfig-paths/register node_modules/event-sourcing-nestjs/src/scripts/reconstruct-view-db.ts"

And whenever you want to reconstruct your view db run:

npm run reconstruct-view-db mongodb://localhost:27017/eventstore

Example

You can find a full example here.

TODOs

  • Use snapshots, so we can reconstruct the DB faster.