Skip to content

AggregateRoot

Defined in: packages/ddd-core/domain/models/aggregate-root.ts:27

Abstract base class representing a Domain-Driven Design (DDD) aggregate root.

Provides a framework-neutral event buffering lifecycle:

  • Records uncommitted domain events via apply.
  • Dispatches events to optional on<EventName> instance handlers for internal state updates.
  • Supports state rehydration via loadFromHistory.
  • Exposes buffered events via getUncommittedEvents for out-of-band application dispatch.
  • Clears buffered events via uncommit once reliably persisted or published.

EventBase extends IEvent = IEvent

The base event type, defaults to IEvent.

new AggregateRoot<EventBase>(): AggregateRoot<EventBase>

AggregateRoot<EventBase>

get autoCommit(): boolean

Defined in: packages/ddd-core/domain/models/aggregate-root.ts:43

Gets whether the aggregate root automatically commits and publishes events upon application.

boolean

set autoCommit(value): void

Defined in: packages/ddd-core/domain/models/aggregate-root.ts:36

Sets whether the aggregate root should automatically commit and publish events upon application.

boolean

void

When true, apply() publishes each event at once instead of buffering it.

IAggregateRoot.autoCommit

apply<T>(event, optionsOrIsFromHistory?): void

Defined in: packages/ddd-core/domain/models/aggregate-root.ts:138

Applies an event to the aggregate root.

If fromHistory is false and autoCommit is disabled, the event is appended to the uncommitted events buffer. If autoCommit is enabled, the event is published immediately. Unless skipHandler is true, routes to on<EventName>(event) if present.

T extends IEvent = EventBase

T

The domain event to apply.

boolean | ApplyEventOptions

Boolean indicating historical event or options object.

void

IAggregateRoot.apply


commit(dispatcherContext?): unknown

Defined in: packages/ddd-core/domain/models/aggregate-root.ts:95

Hands a copy of the buffered events to publishAll with the dispatcher context, then clears the buffer. The buffer is cleared once publishAll() returns, before an asynchronous publisher settles, so a caller that does not await cannot publish the same events twice; if publishAll() throws, the events stay buffered.

unknown

Passed to publishAll, such as { transaction }.

unknown

What publishAll returns: await it to wait for, and catch the errors of, an asynchronous publisher.

const order = publisher.mergeObjectContext(Order.place(id));
await order.commit({ transaction });

IAggregateRoot.commit


protected getEventHandler<T>(event): ((event) => void) | undefined

Defined in: packages/ddd-core/domain/models/aggregate-root.ts:171

Resolves the method handler corresponding to the applied event name (on<EventName>).

T extends IEvent = EventBase

T

((event) => void) | undefined


protected getEventName(event): string

Defined in: packages/ddd-core/domain/models/aggregate-root.ts:184

Resolves the constructor name of the event.

unknown

string


getUncommittedEvents(): EventBase[]

Defined in: packages/ddd-core/domain/models/aggregate-root.ts:114

Returns all uncommitted events currently buffered on this aggregate.

EventBase[]

IAggregateRoot.getUncommittedEvents


loadFromHistory(history): void

Defined in: packages/ddd-core/domain/models/aggregate-root.ts:122

Loads domain events from history to rehydrate the aggregate’s internal state. Historical events invoke event handlers without being re-buffered.

EventBase[]

void

IAggregateRoot.loadFromHistory


publish<T>(_event, _dispatcherContext?): unknown

Defined in: packages/ddd-core/domain/models/aggregate-root.ts:56

Called by apply() for each event while autoCommit is enabled; a no-op that returns undefined unless overridden or connected to a publisher (for example NestJS’s EventPublisher.mergeObjectContext).

T extends IEvent = EventBase

T

The event to publish.

unknown

Passed through to the publisher, such as { transaction }.

unknown

What the publisher returns.

IAggregateRoot.publish


publishAll<T>(_events, _dispatcherContext?): unknown

Defined in: packages/ddd-core/domain/models/aggregate-root.ts:71

Called by commit() with a copy of the buffered events; a no-op that returns undefined unless overridden or connected to a publisher.

T extends IEvent = EventBase

T[]

The events to publish.

unknown

Passed through to the publisher, such as { transaction }.

unknown

What the publisher returns.

IAggregateRoot.publishAll


uncommit(): void

Defined in: packages/ddd-core/domain/models/aggregate-root.ts:107

Clears all uncommitted events without publishing.

void

IAggregateRoot.uncommit