UNPKG

@actonate/mirkwood

Version:

GraphQL based Rapid Server-side Development framework

79 lines (65 loc) 2.83 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _elasticsearch = require('elasticsearch'); var _elasticsearch2 = _interopRequireDefault(_elasticsearch); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var ElasticSearch = function () { function ElasticSearch(_ref) { var config = _ref.config; _classCallCheck(this, ElasticSearch); this.config = config[process.env['NODE_ENV'] || 'development']; if (!this.config) { this.client = null; return; } this.client = new _elasticsearch2.default.Client({ host: [this.config.host, this.config.port || 9200].join(':') }); } _createClass(ElasticSearch, [{ key: 'index', value: function index(typeName, id, body) { var _this = this; return this.search(typeName, { "query": { "term": { "_id": id } } }).then(function (res) { if (res.total !== 0) { return _this.client.update({ index: _this.config.index, type: typeName, id: id, body: { doc: body } }); } return _this.client.create({ index: _this.config.index, type: typeName, id: id, body: body }); }); } }, { key: 'delete', value: function _delete(typeName, id) { return this.client.delete({ index: this.config.index, type: typeName, id: id }); } }, { key: 'search', value: function search(modelName, match) { var _this2 = this; return new Promise(function (resolve, reject) { _this2.client.search({ index: _this2.config.index, type: modelName, body: match }).then(function (res) { var response = { total: res.hits.total, maxScore: res.hits.max_score, hits: res.hits.hits.map(function (hit) { return { _id: hit._id, source: hit._source, score: hit._score }; }) }; resolve(response); }).catch(function (err) { reject(err); }); }); } }]); return ElasticSearch; }(); exports.default = ElasticSearch;