UNPKG

eventstore-interconnect

Version:
132 lines 6.85 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; var GrpcReaderService_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.GrpcReaderService = void 0; const common_1 = require("@nestjs/common"); const db_client_1 = require("@eventstore/db-client"); const shared_utils_1 = require("@nestjs/common/utils/shared.utils"); const constants_1 = require("../constants"); const constants_2 = require("../../../constants"); const __1 = require("../../../"); const logger_1 = require("../../../logger"); let GrpcReaderService = GrpcReaderService_1 = class GrpcReaderService { constructor(connectionConfiguration, eventHandler, subscriptions, eventStoreDBClient, grpcConnectionInitializer, connectionGuard, logger) { this.connectionConfiguration = connectionConfiguration; this.eventHandler = eventHandler; this.subscriptions = subscriptions; this.eventStoreDBClient = eventStoreDBClient; this.grpcConnectionInitializer = grpcConnectionInitializer; this.connectionGuard = connectionGuard; this.logger = logger; } async onModuleInit() { await this.grpcConnectionInitializer.init(); await this.startEventstoreClient(); await this.upsertPersistantSubscriptions(); this.logger.log('READER : connected to ' + this.connectionConfiguration.connectionString); } async startEventstoreClient() { this.client = this.grpcConnectionInitializer.getConnectedClient(); await this.connectionGuard.startConnectionLinkPinger(this.client, this.connectionConfiguration); } async upsertPersistantSubscriptions() { await this.init(async (event) => { try { await this.eventHandler.handle(event); } catch (e) { this.logger.error(`Unexpected error while handling an event... Details : ${e.message}`); } }); } async init(onEvent) { await this.subscribeToPersistentSubscriptions(this.subscriptions, onEvent); this.logger.log(`EventStore v21 connected`); } async subscribeToPersistentSubscriptions(subscriptions = [], onEvent) { await this.upsertPersistentSubscriptions(subscriptions); return Promise.all(subscriptions.map((subscription) => { this.logger.log(`Connecting to persistent subscription "${subscription.group}" on stream "${subscription.stream}"...`); const persistentSubscription = this.client.subscribeToPersistentSubscriptionToStream(subscription.stream, subscription.group); persistentSubscription.on('data', (event) => { try { onEvent(event); persistentSubscription.ack(event); } catch (e) { persistentSubscription.nack('park', 'An error occured...', event); throw e; } }); if (!(0, shared_utils_1.isNil)(subscription.onSubscriptionStart)) { persistentSubscription.on('confirmation', subscription.onSubscriptionStart); } if (!(0, shared_utils_1.isNil)(subscription.onSubscriptionDropped)) { persistentSubscription.on('close', subscription.onSubscriptionDropped); } persistentSubscription.on('error', (error) => { if (subscription.onError) { subscription.onError(error); } else { this.logger.error(error); } }); this.logger.log(`Connected to "${subscription.group}" on stream ${subscription.stream}.`); return persistentSubscription; })); } async upsertPersistentSubscriptions(subscriptions) { for (const subscription of subscriptions) { await this.upsertPersistentSubscription(subscription); } } async upsertPersistentSubscription(subscription) { try { await this.client.createPersistentSubscriptionToStream(subscription.stream, subscription.group, { ...(0, db_client_1.persistentSubscriptionToStreamSettingsFromDefaults)(), ...subscription.settingsForCreation?.subscriptionSettings, liveBufferSize: 1, }, subscription.settingsForCreation?.baseOptions); this.logger.log(`Persistent subscription "${subscription.group}" on stream ${subscription.stream} created.`); } catch (e) { if (GrpcReaderService_1.isNotAlreadyExistsError(e)) { this.logger.error('Subscription creation try : ', e); throw new Error(e); } await this.client.updatePersistentSubscriptionToStream(subscription.stream, subscription.group, { ...(0, db_client_1.persistentSubscriptionToStreamSettingsFromDefaults)(), ...subscription.settingsForCreation?.subscriptionSettings, }, subscription.settingsForCreation?.baseOptions); } } static isNotAlreadyExistsError(e) { return e.code !== constants_1.PERSISTENT_SUBSCRIPTION_ALREADY_EXIST_ERROR_CODE; } }; GrpcReaderService = GrpcReaderService_1 = __decorate([ (0, common_1.Injectable)(), __param(0, (0, common_1.Inject)(constants_2.CONNECTION_CONFIGURATION)), __param(1, (0, common_1.Inject)(__1.EVENT_HANDLER)), __param(2, (0, common_1.Inject)(constants_1.SUBSCRIPTIONS)), __param(3, (0, common_1.Inject)(constants_2.EVENTSTORE_DB_CLIENT)), __param(4, (0, common_1.Inject)(__1.GRPC_CONNECTION_INITIALIZER)), __param(5, (0, common_1.Inject)(__1.EVENTSTORE_CONNECTION_GUARD)), __param(6, (0, common_1.Inject)(logger_1.LOGGER)), __metadata("design:paramtypes", [Object, Object, Array, Object, Object, Object, common_1.Logger]) ], GrpcReaderService); exports.GrpcReaderService = GrpcReaderService; //# sourceMappingURL=grpc-reader.service.js.map