UNPKG

@dugongjs/core

Version:

52 lines (51 loc) 3.2 kB
import type { AggregateRoot } from "../../domain/abstract-aggregate-root/aggregate-root.js"; import type { IMessageProducer } from "../../ports/outbound/message-broker/i-message-producer.js"; import type { IOutboundMessageMapper } from "../../ports/outbound/message-broker/i-outbound-message-mapper.js"; import type { IDomainEventRepository } from "../../ports/outbound/repository/i-domain-event-repository.js"; import type { ISnapshotRepository } from "../../ports/outbound/repository/i-snapshot-repository.js"; import type { SerializableObject } from "../../types/serializable-object.type.js"; import { AbstractAggregateHandler, type AbstractAggregateHandlerOptions } from "../abstract-aggregate-handler/abstract-aggregate-handler.js"; export type AggregateManagerOptions<TAggregateRootClass extends AggregateRoot> = AbstractAggregateHandlerOptions<TAggregateRootClass> & { domainEventRepository: IDomainEventRepository; snapshotRepository: ISnapshotRepository; messageProducer?: IMessageProducer<any>; outboundMessageMapper?: IOutboundMessageMapper<any>; }; export type CommitOptions = { correlationId?: string; triggeredByUserId?: string; triggeredByEventId?: string; metadata?: SerializableObject; }; /** * Manager for handling aggregates in the application layer. * Provides methods for applying and committing domain events, creating snapshots, and publishing domain events as messages. */ export declare class AggregateManager<TAggregateRootClass extends AggregateRoot> extends AbstractAggregateHandler<TAggregateRootClass> { private readonly domainEventRepository; private readonly snapshotRepository; private readonly messageProducer?; private readonly outboundMessageMapper?; constructor(options: AggregateManagerOptions<TAggregateRootClass>); /** * Applies staged domain events to the aggregate. * @param aggregate The aggregate instance to which the domain events will be applied. */ applyStagedDomainEvents(aggregate: InstanceType<TAggregateRootClass>): void; /** * Commits staged domain events to the event log and publishes them as messages if necessary. * @param aggregate The aggregate instance whose staged domain events will be committed. * @param options Options for committing domain events, such as correlation ID, triggered by user ID, and metadata. */ commitStagedDomainEvents(aggregate: InstanceType<TAggregateRootClass>, options?: CommitOptions): Promise<void>; /** * Applies and commits staged domain events for the given aggregate instance. * This method combines the application and committing of staged domain events into a single operation. * @param aggregate The aggregate instance for which the staged domain events will be applied and committed. * @param options The options for committing the domain events, such as correlation ID, triggered by user ID, and metadata. */ applyAndCommitStagedDomainEvents(aggregate: InstanceType<TAggregateRootClass>, options?: CommitOptions): Promise<void>; private injectDomainEventMetadata; private publishDomainEventsAsMessagesIfNecessary; private createSnapshotIfNecessary; }