UNPKG

chen-elasticsearch

Version:
123 lines 4.22 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator.throw(value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments)).next()); }); }; const core_1 = require('chen/core'); const elasticsearch_1 = require('elasticsearch'); /** * ElasticSearchClient class */ class ElasticSearchClient extends elasticsearch_1.Client { /** * ElasticSearchClient constructor * @param {string} private name * @param {SQLConfig} private config */ constructor(name, config) { super(config); this.name = name; this.config = config; } } exports.ElasticSearchClient = ElasticSearchClient; /** * ElasticSearchConnectionFactory class */ class ElasticSearchConnectionFactory { /** * ElasticSearchConnectionFactory constructor * @param {ElasticSearchConnectionFactoryConfig} private config */ constructor(config) { this.config = config; /** * Created connections by the factory instance * @type {Map<string, ElasticSearchClient>} */ this.connections = new Map(); } /** * Get single instance * @param {ElasticSearchConnectionFactoryConfig} config * @return {ElasticSearchConnectionFactory} */ static getGlobalInstance() { if (!ElasticSearchConnectionFactory.globalInstance) { throw new core_1.Exception('ElasticSearchConnectionFactory global instance is not created'); } return ElasticSearchConnectionFactory.globalInstance; } /** * Create global instance * @param {ElasticSearchConnectionFactoryConfig} config */ static createGlobalInstance(config) { if (ElasticSearchConnectionFactory.globalInstance) { throw new core_1.Exception('ElasticSearchConnectionFactory global instance is already created'); } ElasticSearchConnectionFactory.globalInstance = new ElasticSearchConnectionFactory(config); } /** * Get connection credentials * @param {string} name * @return {ElasticSearchConfig} */ getCredentials(name) { if (!this.config.connections) return null; return this.config.connections[name] || null; } /** * Create connection then cache the connection to a Map object * the next time getConnection is called with the same parameter used * it will return the previously created connection. * @param {string} name * @return {ElasticSearchClient} */ getConnection(name) { name = name || this.config.default; if (!this.connections.has(name)) { let connection = this.getNewConnection(name); if (connection) { this.connections.set(name, connection); } } return this.connections.get(name) || null; } /** * Always get a new connection * @param {string} name * @return {ElasticSearchClient} */ getNewConnection(name) { let creds = this.getCredentials(name); if (!creds) return null; return new ElasticSearchClient(name, creds); } } exports.ElasticSearchConnectionFactory = ElasticSearchConnectionFactory; /** * ElasticSearchProvider class */ class ElasticSearchProvider extends core_1.Provider { /** * Create connection * @param {Application} app */ register(app) { return __awaiter(this, void 0, void 0, function* () { let dbConf = app.getConfig().get('extensions.chen-elasticsearch'); if (dbConf) { ElasticSearchConnectionFactory.createGlobalInstance(dbConf); } }); } } exports.ElasticSearchProvider = ElasticSearchProvider; //# sourceMappingURL=connection.js.map