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.
Extended by
Section titled “Extended by”Type Parameters
Section titled “Type Parameters”EventBase
Section titled “EventBase”EventBase extends IEvent = IEvent
The base event type, defaults to IEvent.
Implements
Section titled “Implements”IAggregateRoot<EventBase>
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new AggregateRoot<
EventBase>():AggregateRoot<EventBase>
Returns
Section titled “Returns”AggregateRoot<EventBase>
Accessors
Section titled “Accessors”autoCommit
Section titled “autoCommit”Get Signature
Section titled “Get Signature”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.
Returns
Section titled “Returns”boolean
Set Signature
Section titled “Set Signature”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.
Parameters
Section titled “Parameters”boolean
Returns
Section titled “Returns”void
When true, apply() publishes each event at once instead of buffering it.
Implementation of
Section titled “Implementation of”Methods
Section titled “Methods”apply()
Section titled “apply()”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.
Type Parameters
Section titled “Type Parameters”T extends IEvent = EventBase
Parameters
Section titled “Parameters”T
The domain event to apply.
optionsOrIsFromHistory?
Section titled “optionsOrIsFromHistory?”boolean | ApplyEventOptions
Boolean indicating historical event or options object.
Returns
Section titled “Returns”void
Implementation of
Section titled “Implementation of”commit()
Section titled “commit()”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.
Parameters
Section titled “Parameters”dispatcherContext?
Section titled “dispatcherContext?”unknown
Passed to publishAll, such as { transaction }.
Returns
Section titled “Returns”unknown
What publishAll returns: await it to wait for, and catch the errors of, an asynchronous publisher.
Example
Section titled “Example”const order = publisher.mergeObjectContext(Order.place(id));await order.commit({ transaction });Implementation of
Section titled “Implementation of”getEventHandler()
Section titled “getEventHandler()”
protectedgetEventHandler<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>).
Type Parameters
Section titled “Type Parameters”T extends IEvent = EventBase
Parameters
Section titled “Parameters”T
Returns
Section titled “Returns”((event) => void) | undefined
getEventName()
Section titled “getEventName()”
protectedgetEventName(event):string
Defined in: packages/ddd-core/domain/models/aggregate-root.ts:184
Resolves the constructor name of the event.
Parameters
Section titled “Parameters”unknown
Returns
Section titled “Returns”string
getUncommittedEvents()
Section titled “getUncommittedEvents()”getUncommittedEvents():
EventBase[]
Defined in: packages/ddd-core/domain/models/aggregate-root.ts:114
Returns all uncommitted events currently buffered on this aggregate.
Returns
Section titled “Returns”EventBase[]
Implementation of
Section titled “Implementation of”IAggregateRoot.getUncommittedEvents
loadFromHistory()
Section titled “loadFromHistory()”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.
Parameters
Section titled “Parameters”history
Section titled “history”EventBase[]
Returns
Section titled “Returns”void
Implementation of
Section titled “Implementation of”IAggregateRoot.loadFromHistory
publish()
Section titled “publish()”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).
Type Parameters
Section titled “Type Parameters”T extends IEvent = EventBase
Parameters
Section titled “Parameters”_event
Section titled “_event”T
The event to publish.
_dispatcherContext?
Section titled “_dispatcherContext?”unknown
Passed through to the publisher, such as { transaction }.
Returns
Section titled “Returns”unknown
What the publisher returns.
Implementation of
Section titled “Implementation of”publishAll()
Section titled “publishAll()”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.
Type Parameters
Section titled “Type Parameters”T extends IEvent = EventBase
Parameters
Section titled “Parameters”_events
Section titled “_events”T[]
The events to publish.
_dispatcherContext?
Section titled “_dispatcherContext?”unknown
Passed through to the publisher, such as { transaction }.
Returns
Section titled “Returns”unknown
What the publisher returns.
Implementation of
Section titled “Implementation of”uncommit()
Section titled “uncommit()”uncommit():
void
Defined in: packages/ddd-core/domain/models/aggregate-root.ts:107
Clears all uncommitted events without publishing.
Returns
Section titled “Returns”void