-
Notifications
You must be signed in to change notification settings - Fork 2
es cqrs.Class.Aggregate
@sclable/nestjs-libs / es-cqrs / Aggregate
Defined in: packages/es-cqrs/src/aggregate.ts:48
Aggregate class that has an id and a revision number
Use the EventSourcableAggregate decorator so that the ES-CQRS module can register possible events. EventRegistry.
Implement event handlers inside with the name on${EventClassName} to change the aggregates upon events.
Creation events should get a revision of 1. Modifier events should get an incremented value from the aggregate's
current revision. To make this easier call this.uppedRevision() for both.
Example:
import { Aggregate, Event } from '@sclable/es-cqrs'
import { AccountCreatedEvent, AccountNameChangedEvent } from './events'
import { v4 as uuidv4 } from 'uuid'
@EventSourcableAggregate(AccountCreatedEvent, AccountNameChangedEvent)
export class AccountAggregate extends Aggregate {
accountName: string
public static create(id: string, userId: string) {
const self = new AccountAggregate(id, userId)
self.apply(new AccountCreatedEvent(id, self.uppedRevision()))
return self
}
public changeName(accountName: string) {
this.apply(new AccountNameChangedEvent(this.id, this.uppedRevision(), {accountName}))
}
public onAccountCreatedEvent(event: AccountCreatedEvent) {
this.accountName = 'default'
}
public onAccountNameChangedEvent(event: AccountNameChangedEvent) {
this.accountName = event.data.name
}
}-
AggregateRoot<Event>
new Aggregate(
id,userId):Aggregate
Defined in: packages/es-cqrs/src/aggregate.ts:51
string
string
Aggregate
AggregateRoot<Event>.constructor
readonlyid:string
Defined in: packages/es-cqrs/src/aggregate.ts:51
EventSourcedAggregate.id
revision:
number=0
Defined in: packages/es-cqrs/src/aggregate.ts:49
EventSourcedAggregate.revision
readonlyuserId:string
Defined in: packages/es-cqrs/src/aggregate.ts:51
EventSourcedAggregate.userId
apply(
event,options?):void
Defined in: packages/es-cqrs/src/aggregate.ts:59
Applies an event. If auto commit is enabled, the event will be published immediately (note: must be merged with the publisher context in order to work). Otherwise, the event will be stored in the internal events array, and will be published when the commit method is called. Also, the corresponding event handler will be called (if exists). For example, if the event is called UserCreatedEvent, the "onUserCreatedEvent" method will be called.
The event to apply.
boolean | { isFromHistory?: boolean; skipHandler?: boolean; }
void
AggregateRoot.apply
protectedapplyEvent<T>(event,data):void
Defined in: packages/es-cqrs/src/aggregate.ts:83
Apply event helper
Takes care of upping the revision and saving the user ID
T
event data type
event class
T
event data
void
getUncommittedEvents():
Event[]
Defined in: packages/es-cqrs/src/aggregate.ts:55
Returns all uncommitted events.
Event[]
All uncommitted events.
AggregateRoot.getUncommittedEvents
uppedRevision():
number
Defined in: packages/es-cqrs/src/aggregate.ts:70
number
an incremented revision for events