2.0.7 • Published 4 years ago

@hardyscc/nestjs-event-store v2.0.7

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

Installation

$ yarn install @hardyscc/nestjs-event-store

Description

This module aims to bridge the gap between NestJs and Event Store. It supports all different subscription strategies of supported by Event Store. Such as Volatile, CatchUp and Persistent subscriptions fairly easily.

Setup from versions from v2.0.0

Setup root app module
import { Module } from '@nestjs/common';
import { NestjsEventStoreModule } from '@hardyscc/nestjs-event-store';

@Module({
  imports: [
    NestjsEventStoreModule.forRoot({
      tcpEndpoint: {
        host: process.env.ES_TCP_HOSTNAME || AppConfig.eventstore?.hostname,
        port: parseInt(process.env.ES_TCP_PORT, 10) || AppConfig.eventstore?.tcpPort,
      },
      options: {
        defaultUserCredentials: {
          password: AppConfig.eventstore?.tcpPassword,
          username: AppConfig.eventstore?.tcpUsername,
        },
      },
    }),
  ]
})
export class AppModule {}

Setup module

Note featureStreamName field is not important if you're subscription type is persistent'

import { Module } from '@nestjs/common';
import { CommandBus, CqrsModule, EventBus } from '@nestjs/cqrs';
import {
  NestjsEventStoreModule,
  EventStore,
  EventStoreSubscriptionType
} from '@hardyscc/nestjs-event-store';

import {
  UserCommandHandlers,
  UserCreatedEvent,
  UserEventHandlers,
  UserQueryHandlers
} from '../cqrs';
import { UserSagas } from './sagas';

@Module({
  imports: [
    CqrsModule,
    NestjsEventStoreModule.forFeature({
      featureStreamName: '$ce-user',
      subscriptionsDelay: 2000, // Default is 0 (Optional)
      subscriptions: [
        {
          type: EventStoreSubscriptionType.CatchUp,
          stream: '$ce-user',
          resolveLinkTos: true, // Default is true (Optional)
          lastCheckpoint: 13 // Default is 0 (Optional)
        },
        {
          type: EventStoreSubscriptionType.Volatile,
          stream: '$ce-user'
        },
        {
          type: EventStoreSubscriptionType.Persistent,
          stream: '$ce-user',
          persistentSubscriptionName: 'steamName',
          resolveLinkTos: true // Default is true (Optional)
        }
      ],
      eventHandlers: {
        UserLoggedInEvent: data => new UserLoggedInEvent(data),
        UserRegisteredEvent: data => new UserRegisteredEvent(data),
        EmailVerifiedEvent: data => new EmailVerifiedEvent(data)
      }
    })
  ],

  providers: [
    UserSagas,
    ...UserQueryHandlers,
    ...UserCommandHandlers,
    ...UserEventHandlers
  ]
})
export class UserModule {}

Setup from versions below v2.0.0

Setup root app module

import { Module } from '@nestjs/common';
import { NestjsEventStoreModule } from '@hardyscc/nestjs-event-store';

@Module({
  imports: [
    NestjsEventStoreModule.forRoot({
      http: {
        port: parseInt(process.env.ES_HTTP_PORT, 10),
        protocol: process.env.ES_HTTP_PROTOCOL
      },
      tcp: {
        credentials: {
          password: process.env.ES_TCP_PASSWORD,
          username: process.env.ES_TCP_USERNAME
        },
        hostname: process.env.ES_TCP_HOSTNAME,
        port: parseInt(process.env.ES_TCP_PORT, 10),
        protocol: process.env.ES_TCP_PROTOCOL
      }
    })
  ]
})
export class AppModule {}

Setup module

import { Module } from '@nestjs/common';
import { CommandBus, CqrsModule, EventBus } from '@nestjs/cqrs';
import {
  NestjsEventStoreModule,
  EventStore
} from '@hardyscc/nestjs-event-store';

import {
  UserCommandHandlers,
  UserCreatedEvent,
  UserEventHandlers,
  UserQueryHandlers
} from '../cqrs';
import { UserSagas } from './sagas';

@Module({
  imports: [
    CqrsModule,
    NestjsEventStoreModule.forFeature({
      name: 'user',
      resolveLinkTos: false
    })
  ],

  providers: [
    UserSagas,
    ...UserQueryHandlers,
    ...UserCommandHandlers,
    ...UserEventHandlers
  ]
})
export class UserModule {
  constructor(
    private readonly command$: CommandBus,
    private readonly event$: EventBus,
    private readonly eventStore: EventStore
  ) {}

  onModuleInit(): any {
    this.eventStore.setEventHandlers(this.eventHandlers);
    this.eventStore.bridgeEventsTo((this.event$ as any).subject$);
    this.event$.publisher = this.eventStore;
  }

  eventHandlers = {
    UserCreatedEvent: data => new UserCreatedEvent(data)
  };
}

Notice

2.0.0 release inspired by nestjs-eventstore

License

This project is MIT licensed.

2.0.5

4 years ago

2.0.7

4 years ago

2.0.6

4 years ago

2.0.4

4 years ago

2.0.3

4 years ago

1.0.0

4 years ago