UNPKG

lemon-engine

Version:

Lemon Engine Module to Synchronize Node over DynamoDB + ElastiCache + Elasticsearch by [lemoncloud](https://lemoncloud.io)

221 lines 10.4 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var http_proxy_1 = __importDefault(require("./http-proxy")); var maker = function (_$, name, options) { name = name || 'ES6'; var $U = _$.U; // re-use global instance (utils). var $_ = _$._; // re-use global instance (_ lodash). if (!$U) throw new Error('$U is required!'); if (!$_) throw new Error('$_ is required!'); var NS = $U.NS(name, 'cyan'); // NAMESPACE TO BE PRINTED. //! load common functions var _log = _$.log; var _inf = _$.inf; var _err = _$.err; /** **************************************************************************************************************** * Internal Proxy Function ** ****************************************************************************************************************/ var ENDPOINT = $U.env('ES6_ENDPOINT', typeof options == 'string' ? options : ''); var $proxy = function () { if (!ENDPOINT) throw new Error('env:ES6_ENDPOINT is required!'); var SVC = 'X' + name; var $SVC = _$(SVC, null); return $SVC ? $SVC : http_proxy_1.default(_$, SVC, ENDPOINT); // re-use proxy by name }; /** **************************************************************************************************************** * Main Implementation. ** ****************************************************************************************************************/ var thiz = new (/** @class */ (function () { function class_1() { this.name = function () { return "elastic6-proxy:" + name; }; this.endpoint = function () { return ENDPOINT; }; } /** * Create index with document initialization. * * @param index - ES index. * @param type - document type of index * @param options - options of document. * @returns {Promise.<*>} */ class_1.prototype.do_create_index_type = function (index, type, options) { if (!index) return Promise.reject(new Error(NS + 'index is required')); // if (!type) return Promise.reject(new Error(NS + 'type is required')); var param = null; var $param = Object.assign({}, param || {}); // $param.$type = type; // must be '' return $proxy() .do_post(index, '0', 'create-index', $param, options) .then(function (_) { return _.result; }); }; /** * delete target index. * * @param {*} index index name * @param {*} type (optional) type * @param {*} options (optional) options. */ class_1.prototype.do_delete_index_type = function (index, type, options) { if (!index) return Promise.reject(new Error(NS + 'index is required')); // if (!type) return Promise.reject(new Error(NS + 'type is required')); var param = null; var $param = Object.assign({}, param || {}); // $param.$type = type; // must be '' return $proxy() .do_post(index, '0', 'delete-index', $param, options) .then(function (_) { return _.result; }); }; //! create new document. class_1.prototype.do_create_item = function (index, type, id, data) { if (!index) return Promise.reject(new Error(NS + 'parameter:index is required')); if (!type) return Promise.reject(new Error(NS + 'parameter:type is required')); if (!id) return Promise.reject(new Error(NS + 'parameter:id is required')); if (!data) return Promise.reject(new Error(NS + 'parameter:data is required')); var options = null; // optional values. var $param = Object.assign({}, options || {}); $param.$type = type; return $proxy() .do_post(index, id, undefined, $param, data) .then(function (_) { return _.result; }); }; //! push new document. (if without id) class_1.prototype.do_push_item = function (index, type, data, id) { if (!index) return Promise.reject(new Error(NS + 'parameter:index is required')); if (!type) return Promise.reject(new Error(NS + 'parameter:type is required')); if (!data) return Promise.reject(new Error(NS + 'parameter:data is required')); var options = null; // optional values. var $param = Object.assign({}, options || {}); $param.$type = type; id = id || ''; // make sure valid text. id = id === '' ? '0' : ''; return $proxy() .do_post(index, id, 'push', $param, data) .then(function (_) { return _.result; }); }; //! update document only for specified data set. class_1.prototype.do_update_item = function (index, type, id, data) { if (!index) return Promise.reject(new Error(NS + 'parameter:index is required')); if (!type) return Promise.reject(new Error(NS + 'parameter:type is required')); if (!id) return Promise.reject(new Error(NS + 'parameter:id is required')); if (!data) return Promise.reject(new Error(NS + 'parameter:data is required')); var options = null; // optional values. var $param = Object.assign({}, options || {}); $param.$type = type; return $proxy() .do_put(index, id, undefined, $param, data) .then(function (_) { return _.result; }); }; //! Read document with projection (data) class_1.prototype.do_get_item = function (index, type, id, data) { if (!index) return Promise.reject(new Error(NS + 'parameter:index is required')); if (!type) return Promise.reject(new Error(NS + 'parameter:type is required')); if (!id) return Promise.reject(new Error(NS + 'parameter:id is required')); // if (!data) return Promise.reject(new Error(NS + 'parameter:data is required')); var options = null; // optional values. var $param = Object.assign({}, options || {}); $param.$type = type; return $proxy() .do_get(index, id, undefined, $param, data) .then(function (_) { return _.result; }); }; //! delete document. class_1.prototype.do_delete_item = function (index, type, id) { if (!index) return Promise.reject(new Error(NS + 'parameter:index is required')); if (!type) return Promise.reject(new Error(NS + 'parameter:type is required')); if (!id) return Promise.reject(new Error(NS + 'parameter:id is required')); // if (!data) return Promise.reject(new Error(NS + 'parameter:data is required')); var options = null; // optional values. var $param = Object.assign({}, options || {}); $param.$type = type; return $proxy() .do_delete(index, id, undefined, $param) .then(function (_) { return _.result; }); }; //! search item /** * Search Syntax. (Simple) * - 기본적으로 'mini-language'를 그대로 지원하도록한다. * - 입력의 파라마터의 키값은 테스트할 필드들이다. * {"stock":">1"} => query_string : "stock:>1" * * - 파라미터 예약: * $query : ES _search 용 쿼리를 그대로 이용. * $exist : 'a,!b,c' => a AND NOT b AND c 를 _exists_ 항목으로 풀어씀. * $source : _source 항목에 포함될 내용. (undefined => _source:false) * $limit : same as "size" * $page : same as "from" / "size" ($limit 를 ipp 으로 함축하여 이용). * * * [Mini-Language] * ``` * # find title field which contains quick or brown. * title:(quick OR brown) * * # not-null value. * _exists_:title * * # regular exp. * name:/joh?n(ath[oa]n)/ * ``` * * * * 참고: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#query-string-syntax * 참고: http://okfnlabs.org/blog/2013/07/01/elasticsearch-query-tutorial.html * * @returns {Promise.<*>} */ class_1.prototype.do_search_item = function (index, type, param) { if (!index) return Promise.reject(new Error(NS + 'parameter:index is required')); // if (!type) return Promise.reject(new Error(NS + 'parameter:type is required')); // if (!id) return Promise.reject(new Error(NS + 'parameter:id is required')); if (!param) return Promise.reject(new Error(NS + 'parameter:param is required')); var $param = Object.assign({}, param || {}); $param.$type = type; //TODO:WARN! conflict with 'type' field. return $proxy() .do_get(index, '', undefined, $param) .then(function (_) { return _.result; }); }; class_1.prototype.do_test_self = function (that) { that = that || {}; _log(NS, '- do_test_self()... param=', that); var $param = Object.assign({}, that || {}); // $param.$type = type; return $proxy() .do_get('#', '0', 'test-self', $param) .then(function (_) { return _.result; }); }; return class_1; }()))(); //! create & register service. return _$(name, thiz); }; exports.default = maker; //# sourceMappingURL=elastic6-proxy.js.map