Skip to content

es cqrs.Aggregate

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

Class: Aggregate

es-cqrs.Aggregate

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
  }
}

Hierarchy

  • AggregateRoot<Event>

    Aggregate

Implements

  • EventSourcedAggregate

Table of contents

Constructors

Properties

Methods

Constructors

constructor

new Aggregate(id, userId): Aggregate

Parameters

Name Type
id string
userId string

Returns

Aggregate

Overrides

AggregateRoot&lt;Event&gt;.constructor

Defined in

packages/es-cqrs/src/aggregate.ts:51

Properties

id

Readonly id: string

Implementation of

EventSourcedAggregate.id

Defined in

packages/es-cqrs/src/aggregate.ts:51


revision

revision: number = 0

Implementation of

EventSourcedAggregate.revision

Defined in

packages/es-cqrs/src/aggregate.ts:49


userId

Readonly userId: string

Implementation of

EventSourcedAggregate.userId

Defined in

packages/es-cqrs/src/aggregate.ts:51

Methods

apply

apply(event, options?): void

Parameters

Name Type Default value
event Event undefined
options boolean | { isFromHistory?: boolean ; skipHandler?: boolean } false

Returns

void

Overrides

AggregateRoot.apply

Defined in

packages/es-cqrs/src/aggregate.ts:59


applyEvent

applyEvent<T>(event, data): void

Apply event helper

Takes care of upping the revision and saving the user ID

Type parameters

Name
T

Parameters

Name Type Description
event EventConstructor event class
data T event data

Returns

void

Typeparam

T event data type

Defined in

packages/es-cqrs/src/aggregate.ts:83


getUncommittedEvents

getUncommittedEvents(): Event[]

Returns

Event[]

Overrides

AggregateRoot.getUncommittedEvents

Defined in

packages/es-cqrs/src/aggregate.ts:55


uppedRevision

uppedRevision(): number

Returns

number

an incremented revision for events

Defined in

packages/es-cqrs/src/aggregate.ts:70

Clone this wiki locally