-
Notifications
You must be signed in to change notification settings - Fork 2
es cqrs.Interface.Event
@sclable/nestjs-libs / es-cqrs / Event
Event interface
In order for the event-store to replay your events implement the Event
interface and create a constructor according
to EventConstructor, or for conveniency extend the DefaultEvent class:
constructor(
public readonly aggregateId: string,
public readonly aggregateType: string,
public readonly revision: number,
public readonly createdAt: Date,
public readonly userId: string,
public readonly data: {[key: string]: any}
)
It is recommended to define data
as an interface with the event
Example:
import { Event } from '@sclable/es-cqrs'
interface EventData {
accountName: string,
email: string,
birthday: Date,
}
export class AccountCreatedEvent implements Event {
public constructor(
public readonly aggregateId: string,
public readonly aggregateType: string,
public readonly revision: number,
public readonly createdAt: Date,
public readonly userId: string,
public readonly data: EventData,
) {}
}
To use CustomEventOptions define that as the forth parameter
Example:
@EventForModule('MyModule')
export class AccountCreatedEvent implements Event {
public constructor(
public readonly aggregateId: string,
public readonly aggregateType: string,
public readonly revision: number,
public readonly createdAt: Date,
public readonly userId: string,
public readonly data: EventData,
public readonly custom: CustomEventOptions,
) {}
}
Or use it as a computed parameter
Example:
@EventForModule('MyModule')
export class AccountCreatedEvent implements Event {
public constructor(
public readonly aggregateId: string,
public readonly aggregateType: string,
public readonly revision: number,
public readonly createdAt: Date,
public readonly userId: string,
public readonly data: EventData,
public readonly custom: CustomEventOptions = { queueById: data.customId }
) {}
}
To handle these events implement the IEventHandler
interface.
Example:
import { EventHandler, IEventHandler } from '@sclable/es-cqrs'
import { AccountCreatedEvent } from './events'
@EventHandler(AccountCreatedEvent)
export class AccountCreatedEventHandler implements IEventHandler<AccountCreatedEvent> {
async handle(event: AccountCreatedEvent) {
// do something (e.g. change DB, change view, emit to other type of listeners)
}
}
IEvent
aggregateId:
string
packages/es-cqrs/src/interfaces/event.ts:96
aggregateType:
string
packages/es-cqrs/src/interfaces/event.ts:97
createdAt:
Date
packages/es-cqrs/src/interfaces/event.ts:99
optional
customOptions:CustomEventOptions
packages/es-cqrs/src/interfaces/event.ts:103
data:
any
packages/es-cqrs/src/interfaces/event.ts:102
revision:
number
packages/es-cqrs/src/interfaces/event.ts:98
userId:
string