UNPKG

@hotmeshio/hotmesh

Version:

Serverless Workflow

100 lines (99 loc) 4.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ConnectorService = void 0; const utils_1 = require("../../modules/utils"); const ioredis_1 = require("./providers/ioredis"); const nats_1 = require("./providers/nats"); const postgres_1 = require("./providers/postgres"); const redis_1 = require("./providers/redis"); class ConnectorService { static async disconnectAll() { await redis_1.RedisConnection.disconnectAll(); await ioredis_1.RedisConnection.disconnectAll(); await postgres_1.PostgresConnection.disconnectAll(); await nats_1.NatsConnection.disconnectAll(); } /** * Connect to a provider (redis, nats, postgres) and return the native * client. Connections are handled by the engine and worker routers at * initialization, but the factory method provided here is useful * for testing provider configurations. */ static async connectClient(ProviderConfig) { const target = {}; await ConnectorService.initClient(ProviderConfig, target, 'client'); return target.client; } /** * Initialize `store`, `stream`, and `subscription` clients for any provider. * @private */ static async initClients(target) { let connection = utils_1.polyfill.providerConfig(target); if (!('store' in connection)) { connection = target.connection = { ...connection, store: { ...connection }, stream: { ...connection }, sub: { ...connection }, }; } // Expanded form if (connection.store) { await ConnectorService.initClient(connection.store, target, 'store'); } if (connection.stream) { await ConnectorService.initClient(connection.stream, target, 'stream'); } if (connection.sub) { await ConnectorService.initClient(connection.sub, target, 'sub'); // use store for publishing events if same as subscription if (connection.sub.class !== connection.store.class) { //initialize a separate client for publishing events, using //the same provider as the subscription client connection.pub = { class: connection.sub.class, options: { ...connection.sub.options }, provider: connection.sub.provider, }; await ConnectorService.initClient(connection.pub, target, 'pub'); } } } /** * Binds a provider client native instance to the target object. * @private */ static async initClient(ProviderConfig, target, field) { if (target[field]) { return; } const providerClass = ProviderConfig.class; const options = ProviderConfig.options; const providerName = ProviderConfig.provider || (0, utils_1.identifyProvider)(providerClass); //e.g. 'postgres.poolclient' const providerType = providerName.split('.')[0]; //e.g. 'postgres' let clientInstance; const id = (0, utils_1.guid)(); switch (providerType) { case 'redis': clientInstance = await redis_1.RedisConnection.connect(id, providerClass, options, { provider: providerName }); break; case 'ioredis': clientInstance = await ioredis_1.RedisConnection.connect(id, providerClass, options, { provider: providerName }); break; case 'nats': clientInstance = await nats_1.NatsConnection.connect(id, providerClass, options, { provider: providerName }); break; case 'postgres': //if connecting as a poolClient for subscription, auto connect the client const bAutoConnect = field === 'sub'; clientInstance = await postgres_1.PostgresConnection.connect(id, providerClass, options, { connect: bAutoConnect, provider: providerName }); break; default: throw new Error(`Unknown provider type: ${providerType}`); } // Bind the resolved client instance to the target object target[field] = clientInstance.getClient(); } } exports.ConnectorService = ConnectorService;