// Copyright (c) Cratis. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. import { createLogger } from '../logging/createLogger.js'; import { ChronicleConnection } from '../connection/index.js'; import { EventStoreName } from '../EventStoreName.js'; import { EventType, EventTypeGeneration, EventTypeId, IEventTypes } from '../events/index.js'; import { EventStoreSubscriptionBuilder } from './EventStoreSubscriptionBuilder.js'; import { EventStoreSubscriptionDefinition } from './EventStoreSubscriptionDefinition.js'; import { EventStoreSubscriptionId } from './EventStoreSubscriptionId.js'; import { IEventStoreSubscriptionBuilder } from './IEventStoreSubscriptionBuilder.js'; import { IEventStoreSubscriptions } from './IEventStoreSubscriptions.js'; /** * Represents an implementation of {@link IEventStoreSubscriptions}. */ export class EventStoreSubscriptions implements IEventStoreSubscriptions { private readonly _logger: ReturnType; constructor( private readonly _eventTypes: IEventTypes, private readonly _targetEventStore: string, private readonly _connection: ChronicleConnection ) { this._logger = createLogger('@cratis/chronicle/EventStoreSubscriptions', _connection.logger); } /** @inheritdoc */ async subscribe( subscriptionId: EventStoreSubscriptionId | string, sourceEventStore: EventStoreName | string, configure?: (builder: IEventStoreSubscriptionBuilder) => void ): Promise { const id = typeof subscriptionId === 'string' ? new EventStoreSubscriptionId(subscriptionId) : subscriptionId; const source = typeof sourceEventStore === 'string' ? sourceEventStore : sourceEventStore.value; const builder = new EventStoreSubscriptionBuilder(this._eventTypes, id, source); configure?.(builder); const definition = builder.build(); this._logger.info('Registering event store subscription', { targetEventStore: this._targetEventStore, subscriptionId: id.value, sourceEventStore: source, eventTypeCount: definition.eventTypes.length }); await this._connection.eventStoreSubscriptions.add({ TargetEventStore: this._targetEventStore, Subscriptions: [{ Identifier: definition.id.value, SourceEventStore: definition.sourceEventStore, EventTypes: definition.eventTypes.map(_ => ({ Id: _.id.value, Generation: _.generation.value, Tombstone: _.tombstone })) }] }); } /** @inheritdoc */ async unsubscribe(subscriptionId: EventStoreSubscriptionId | string): Promise { const id = typeof subscriptionId === 'string' ? subscriptionId : subscriptionId.value; this._logger.info('Removing event store subscription', { targetEventStore: this._targetEventStore, subscriptionId: id }); await this._connection.eventStoreSubscriptions.remove({ TargetEventStore: this._targetEventStore, SubscriptionIds: [id] }); } /** @inheritdoc */ async getAll(): Promise { const response = await this._connection.eventStoreSubscriptions.getSubscriptions({ TargetEventStore: this._targetEventStore }); return (response.items ?? []).map(definition => new EventStoreSubscriptionDefinition( new EventStoreSubscriptionId(definition.Identifier), definition.SourceEventStore, (definition.EventTypes ?? []).map(eventType => new EventType( new EventTypeId(eventType.Id), new EventTypeGeneration(eventType.Generation), eventType.Tombstone )) )); } }