Skip to content

es cqrs.ESCQRSModule

Sclable CI edited this page Sep 11, 2024 · 198 revisions

Class: ESCQRSModule

es-cqrs.ESCQRSModule

The main module

In the root module import ESCQRSModule.forRoot(). By default the module will create an event store in memory so provide database configuration as the second parameter for this module.

Example: app.module.ts

import { ESCQRSModule, EventStoreOptions } from '@sclable/es-cqrs'

const eventStoreOptions: EventStoreOptions = {
  type: 'postgres',
  initOptions: {
    url: 'postgres://app:app@localhost/app',
    namespace: 'db_name',
  }
}

@Module({
  imports: [
    ESCQRSModule.forRoot(eventStoreOptions),
    MyModule,
  ]
})
export class AppModule {}

The module accepts options asyncronously as well from an options provider

Example: app.module.ts

import { ESCQRSModule, EventStoreOptions } from '@sclable/es-cqrs'

@Module({
  imports: [
    ESCQRSModule.forRootAsync({
      inject: [GlobalOptionsProvider]
      useFactory: async (globalOptions: GlobalOptionsProvider) => {
        return await globalOptions.getEventStoreOptions()
      }
    }),
    MyModule,
  ]
})
export class AppModule {}

or an options factory

Example: app.module.ts

import { ESCQRSModule, EventStoreOptions } from '@sclable/es-cqrs'

class OptionsFactory implements EventStoreOptionsFactory {
  public async createEventStoreOptions() {
    return {type: 'inmemory', logging: true}
  }
}

@Module({
  imports: [
    ESCQRSModule.forRootAsync({
      useClass: OptionsFactory
    }),
    MyModule,
  ]
})
export class AppModule {}

Import this module in a feature module to get access to the aggregate repositories. Gather the aggregates in an array of AggregateConstructor[].

Example: aggregates/index.ts

import { AccountAggregate } from './account.aggregate'
import { UserAggregate } from './user.aggregate'
import { Aggregate, AggregateConstructor } from '@sclable/es-cqrs'

export const aggregates: AggregateConstructor[] = [AccountAggregate, UserAggregate]

export { AccountAggregate } from './account.aggregate'
export { UserAggregate } from './user.aggregate'

In order to handle commands and events add the handlers as providers to your feature module

Example: my.module.ts

import { Module } from '@nestjs/common'
import { ReplayService, ESCQRSModule } from '@sclable/es-cqrs'
import { aggregates } from './aggregates'
import { commandHandlers } from './command-handlers'
import { eventHandlers } from './event-handlers'

@Module({
  imports: [
    ESCQRSModule.forFeature(aggregates)
  ],
  providers: [...commandHandlers, ...eventHandlers]
})
export class MyModule {}

Table of contents

Constructors

Methods

Constructors

constructor

new ESCQRSModule(): ESCQRSModule

Returns

ESCQRSModule

Methods

forFeature

forFeature<T>(aggregates): DynamicModule

Type parameters

Name Type
T extends Aggregate

Parameters

Name Type
aggregates AggregateConstructor<T>[]

Returns

DynamicModule

Defined in

packages/es-cqrs/src/es-cqrs.module.ts:143


forRoot

forRoot(options, eventStoreProvider?): DynamicModule

Parameters

Name Type Default value
options EventStoreOptions undefined
eventStoreProvider AsyncProvider<EventStoreProvider> InmemoryEventStoreProvider

Returns

DynamicModule

Defined in

packages/es-cqrs/src/es-cqrs.module.ts:123


forRootAsync

forRootAsync(options, eventStoreProvider?): DynamicModule

Parameters

Name Type Default value
options AsyncProvider<EventStoreOptions> undefined
eventStoreProvider AsyncProvider<EventStoreProvider> InmemoryEventStoreProvider

Returns

DynamicModule

Defined in

packages/es-cqrs/src/es-cqrs.module.ts:133

Clone this wiki locally