UNPKG

nestjs-event-store-cqrs

Version:
117 lines 4.96 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); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.EventStoreService = void 0; const common_1 = require("@nestjs/common"); const constants_1 = require("./constants"); const kafkajs_1 = require("kafkajs"); const cqrs_1 = require("@nestjs/cqrs"); let EventStoreService = class EventStoreService { constructor(options, eventBus) { this.eventBus = eventBus; this.logger = new common_1.Logger(this.constructor.name); this.events = []; this.eventsForSearch = {}; this.addEvents(options.events); this.clientConfig = Object.assign(Object.assign({}, constants_1.DEFAULT_CLIENT_CONFIG), options.client); this.consumerConfig = Object.assign(Object.assign({}, constants_1.DEFAULT_CONSUMER_CONFIG), options.consumer); } async onModuleInit() { this.connect(); this.logger.verbose('EventStore modile init'); this.subject$ = this.eventBus.subject$; this.bridgeEventsTo(this.eventBus.subject$); this.eventBus.publisher = this; } async onModuleDestroy() { await this.disconnect(); this.logger.log('EventStore connection destroyed'); } async connect() { this.client = new kafkajs_1.Kafka(this.clientConfig); this.producer = await this.client.producer(); await this.producer.connect(); this.logger.verbose('EventStore producer connected'); this.consumer = await this.client.consumer(this.consumerConfig); await this.consumer.connect(); this.logger.verbose('EventStore consumer connected'); await this.subscribe(); } async subscribe() { try { const subscriptions = []; this.events.forEach((event) => { subscriptions.push(this.consumer.subscribe({ topic: event.name, fromBeginning: false, })); }); if (!subscriptions.length) { this.logger.log('EventStore module was init without eventHandlers'); return; } await Promise.all(subscriptions); await this.consumer.run({ eachMessage: async ({ topic, message }) => { const value = JSON.parse(message.value.toString()); if (this.events && this.eventsForSearch[topic]) { const func = new this.eventsForSearch[topic](...Object.values(value)); this.subject$.next(func); } else [ this.logger.warn(`Event of type ${topic} not handled`, this.constructor.name), ]; }, }); } catch (error) { this.logger.error(`subscribe event error: ${error.stack}`); } } async disconnect() { var _a, _b; await ((_a = this.producer) === null || _a === void 0 ? void 0 : _a.disconnect()); await ((_b = this.consumer) === null || _b === void 0 ? void 0 : _b.disconnect()); } async publish(event) { if (event === undefined) { return; } if (event === null) { return; } const eventPayload = { topic: event.constructor.name, messages: [{ value: JSON.stringify(event) }], }; await this.producer.send(eventPayload); } async bridgeEventsTo(subject) { this.subject$ = subject; } addEvents(events = []) { this.events = [...this.events, ...events]; this.events.map((event) => { this.eventsForSearch[event.name] = event; }); } }; EventStoreService = __decorate([ (0, common_1.Injectable)(), __param(0, (0, common_1.Inject)(constants_1.EVENT_STORE_OPTIONS)), __metadata("design:paramtypes", [Object, cqrs_1.EventBus]) ], EventStoreService); exports.EventStoreService = EventStoreService; //# sourceMappingURL=event-store.service.js.map