UNPKG

chen-elasticsearch

Version:
182 lines 5.52 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()); }); }; function __export(m) { for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; } const core_1 = require('chen/core'); const connection_1 = require('./connection'); __export(require('./connection')); /** * ElasticSearchModel class */ class ElasticModel extends core_1.Model { constructor(...args) { super(...args); /** * Primary key in the attributes list * @type {string} */ this.primaryKey = 'id'; } /** * Create instance then save to database * @param {StdObject} attributes * @return {Promise<ElasticModel>} */ static create(attributes) { return __awaiter(this, void 0, void 0, function* () { let modelInstance = Reflect.construct(this, []); modelInstance.fill(attributes); if (!(yield modelInstance.save())) return null; return modelInstance; }); } /** * Save model attributes to database * @return {Promise<boolean>} */ save() { return __awaiter(this, void 0, void 0, function* () { let data = { index: this.index, type: this.type, id: this.getId(), body: this.getAttributes() }; let response = yield this.getClient().index(data); // set the new id this.setId(response._id); return true; }); } /** * Set client * @param {string} name * @returns {this} */ setClient(name) { this.client = connection_1.ElasticSearchConnectionFactory.getGlobalInstance().getConnection(name); return this; } /** * Get model current client instance * * @returns {ElasticSearchClient} */ getClient() { if (!this.client) { this.setClient(this.clientName); } return this.client; } /** * Get primary field value * @return {any} */ getId() { return this.attributes[this.primaryKey]; } /** * Set primary field value * @param {string | number} id * @return {this} */ setId(id) { this.attributes[this.primaryKey] = id; return this; } /** * Search for records in database * @param {SearchParams} query * @return {Promise<any>} */ static query(query) { let model = Reflect.construct(this, []); query.index = query.index ? query.index : model.index; query.type = query.type ? query.type : model.type; return model.getClient().search(query); } } exports.ElasticModel = ElasticModel; /** * ElasticSearchService class */ class ElasticSearchService extends core_1.StorageService { /** * Add new record * @param {StdObject} data * @param {boolean = true} validate * @return {Promise<T>} */ create(data, validate = false) { return __awaiter(this, void 0, void 0, function* () { return yield this.model.create(data); }); } /** * save model changes with validation * @param {T} model * @param {boolean = true} validate * @return {null} */ save(model, validate) { return __awaiter(this, void 0, void 0, function* () { return null; }); } /** * update record by specified id * @param {number} id * @param {StdObject} data * @param {boolean = true} validate * @return {Promise<boolean>} */ update(id, data, validate = true) { return __awaiter(this, void 0, void 0, function* () { return id; }); } /** * remove record by specified id * @param {number} id * @return {Promise<boolean>} */ destroy(id) { return __awaiter(this, void 0, void 0, function* () { return id; }); } /** * retrieves first record by specific field * @param {string} field * @param {any} optOrValue sql operator or value * @param {any} value * @return {Promise<T>} */ getOneBy(field, optOrValue, value) { return __awaiter(this, void 0, void 0, function* () { // TODO: no support yet for the operator; optOrValue = value ? value : optOrValue; // create filter object let body = { size: 1, query: { bool: { filter: [] } } }; let filter = { term: {} }; filter.term[field] = optOrValue; body.query.bool.filter.push(filter); let data = yield this.model.query({ body: body }); if (!data.hits.total) { return null; } return Reflect.construct(this.model, [data.hits.hits[0]._source]); }); } } exports.ElasticSearchService = ElasticSearchService; //# sourceMappingURL=index.js.map