-
Notifications
You must be signed in to change notification settings - Fork 2
es cqrs.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 {}
• new ESCQRSModule(): ESCQRSModule
▸ forFeature<T
>(aggregates
): DynamicModule
Name | Type |
---|---|
T |
extends Aggregate
|
Name | Type |
---|---|
aggregates |
AggregateConstructor <T >[] |
DynamicModule
packages/es-cqrs/src/es-cqrs.module.ts:143
▸ forRoot(options
, eventStoreProvider?
): DynamicModule
Name | Type | Default value |
---|---|---|
options |
EventStoreOptions |
undefined |
eventStoreProvider |
AsyncProvider <EventStoreProvider > |
InmemoryEventStoreProvider |
DynamicModule
packages/es-cqrs/src/es-cqrs.module.ts:123
▸ forRootAsync(options
, eventStoreProvider?
): DynamicModule
Name | Type | Default value |
---|---|---|
options |
AsyncProvider <EventStoreOptions > |
undefined |
eventStoreProvider |
AsyncProvider <EventStoreProvider > |
InmemoryEventStoreProvider |
DynamicModule