class AggregateRoot

Base class for implementing event-sourced aggregate roots.

Aggregate roots are the core building blocks of domain-driven design and event sourcing. They encapsulate business logic, maintain consistency boundaries, and generate events that represent state changes. The aggregate’s current state is rebuilt by replaying its historical events through reducer functions.

Key Features: - *\*Event Sourcing\**: State is derived from events, not stored directly - \**Business Logic\**: Encapsulates domain rules and invariants - \**Event Generation\**: Emits events when state changes occur - \**Immutable History\**: Complete audit trail of all changes - \**Checkpointing\*\*: Optional performance optimization for large event histories

Properties

config

Aggregate configuration settings

Signature
readonly config: AggregateConfig;

history

Complete event history for this aggregate instance

Signature
history: EventicleEvent[];

id

Unique identifier for this aggregate instance

Signature
id: string;

newEvents

Events raised since last persistence (pending events)

Signature
newEvents: EventicleEvent[];

reducers

Event reducer functions mapping event types to state update logic

Signature
reducers: any;

replaying

Flag indicating if aggregate is currently replaying historical events

Signature
replaying: boolean;

type

Signature
get type(): string;

Constructors

(constructor)(type)

Constructs a new instance of the AggregateRoot class

Parameters
Name Type Description

type

string | ~AggregateConfig

Signature
constructor(type: string | AggregateConfig);

Methods

currentCheckpoint()

Returns the current state as a checkpoint for performance optimization.

Checkpoints allow aggregates with long event histories to snapshot their current state, reducing the time needed to rebuild from events. This method should be implemented when storeCheckpoint is enabled in the config.

Signature
currentCheckpoint(): object;

handleEvent(event)

Applies an event to the aggregate state using the appropriate reducer.

This method is called automatically by raiseEvent and during event replay. It looks up the reducer function for the event type and applies the event to update the aggregate’s state.

Parameters
Name Type Description

event

EventicleEvent

The event to apply to the aggregate state

Returns

void

Signature
handleEvent(event: EventicleEvent): void;

raiseEvent(event)

Raises a new domain event and applies it to the aggregate state.

This is the primary method for recording state changes in event-sourced aggregates. The event is automatically assigned metadata (ID, timestamp, source) and applied through the appropriate reducer function.

Parameters
Name Type Description

event

EventicleEvent

The domain event to raise

Returns

EventicleEvent<any>

The event with populated metadata

Signature
raiseEvent(event: EventicleEvent): EventicleEvent<any>;