Socket
Socket
Sign inDemoInstall

event-sourcing-nestjs

Package Overview
Dependencies
29
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    event-sourcing-nestjs

NestJS module for implementing Event Sourcing.


Version published
Weekly downloads
349
increased by10.44%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

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.
  • EventStore: Get history of events for an aggregate.

State of the art

State of the art

Getting started

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

Usage

Importing

app.module.ts

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

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

Using it in your modules

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

@Module({
  imports: [
    EventSourcingModule.forFeature(),
  ],
})
export class UserModule {}

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.

Read more info about the Materialized View pattern here

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
    }
}

Events

Finally, your events must extend the abstract class StorableEvent.

export class UserCreatedEvent extends StorableEvent {
    eventAggregate = 'user';
    eventVersion = 1;
    id = '_id_';
}

Get event history for an aggregate

const aggregate = 'user';
const id = '_id_';
console.log(await this.eventStore.getEvents(aggregate, id));

Reconstructing the view db

await ReconstructViewDb.run(await NestFactory.create(AppModule.forRoot()));

Example

You can find a working example using the Materialized View pattern here.

TODOs

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

Keywords

FAQs

Last updated on 21 Mar 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc