UNPKG

@dugongjs/core

Version:

44 lines (43 loc) 2.12 kB
import { aggregateMetadataRegistry } from "../../domain/index.js"; import { AggregateFactory } from "../aggregate-factory/aggregate-factory.js"; import { aggregateSnapshotTransformer } from "../aggregate-snapshot-transformer/aggregate-snapshot-transformer.js"; export class AggregateQueryService { constructor(options) { this.currentOrigin = options.currentOrigin; this.domainEventRepository = options.domainEventRepository; this.logger = options.logger; } async getAggregateTypes() { return aggregateMetadataRegistry.getAggregateTypes(); } async getAggregateIds(origin, aggregateType, tenantId) { return this.domainEventRepository.getAggregateIds(null, origin ?? this.currentOrigin, aggregateType, tenantId); } async getAggregate(origin, aggregateType, aggregateId, tenantId, toSequenceNumber) { const aggregateClass = aggregateMetadataRegistry.getAggregateClass(aggregateType, origin ?? undefined); if (!aggregateClass) { return null; } const factory = new AggregateFactory({ aggregateClass, transactionManager: { transaction: (fn) => fn({}) }, domainEventRepository: this.domainEventRepository, currentOrigin: this.currentOrigin, tenantId, logger: this.logger }); const aggregate = await factory.buildFromEventLog(aggregateId, { toSequenceNumber }); if (!aggregate) { return null; } const snapshotMetadata = aggregateMetadataRegistry.getAggregateSnapshotMetadata(aggregateClass); if (snapshotMetadata) { const snapshot = await aggregateSnapshotTransformer.takeSnapshot(origin ?? this.currentOrigin, aggregateType, aggregate, tenantId); return snapshot.snapshotData; } return aggregate; } async getDomainEventsForAggregate(origin, aggregateType, aggregateId, tenantId) { return this.domainEventRepository.getAggregateDomainEvents(null, origin ?? this.currentOrigin, aggregateType, aggregateId, tenantId); } }