diff --git a/lib/esConnector.js b/lib/esConnector.js index 28b8beb..ddb1507 100755 --- a/lib/esConnector.js +++ b/lib/esConnector.js @@ -1236,6 +1236,9 @@ ESConnector.prototype.updateAll = function updateAll(model, where, data, options // default language for inline scripts is painless if ES 5, so this needs the extra params. body.script.inline += 'ctx._source.' + key + '=params.' + key + ';'; body.script.params[key] = value; + if (key === 'docType') { + body.script.params[key] = model; + } } }); @@ -1247,7 +1250,7 @@ ESConnector.prototype.updateAll = function updateAll(model, where, data, options self.db.updateByQuery(document) .then(function (response) { log('ESConnector.prototype.updateAll', 'response', response); - cb(null, { + return cb(null, { updated: response.updated, total: response.total }); @@ -1370,38 +1373,57 @@ ESConnector.prototype.destroy = function destroy(modelName, id, done) { /** * Update a model instance by id * - * NOTES: - * > The _source field need to be enabled for this feature to work. */ + ESConnector.prototype.updateAttributes = function updateAttrs(modelName, id, data, callback) { var self = this; - log('ESConnector.prototype.updateAttributes', 'modelName', modelName, 'id', id, 'data', data); - - if (id === undefined || id === null) { - throw new Error('id not set!'); + if (self.debug) { + log('ESConnector.prototype.updateAttributes', 'modelName', modelName, 'id', id, 'data', data); } + var idName = self.idName(modelName); + log('ESConnector.prototype.updateAttributes', 'idName', idName); - var defaults = self.addDefaults(modelName, 'updateAttributes'); - self.db.update(_.defaults({ - id: id, - body: { - doc: data, - 'doc_as_upsert': false + var defaults = self.addDefaults(modelName, 'updateAll'); + + var body = { + query: self.buildWhere(modelName, idName, { + _id: id + }).query + }; + + body.script = { + inline: '', + params: {} + }; + _.forEach(data, function (value, key) { + if (key !== '_id' || key !== idName) { + // default language for inline scripts is painless if ES 5, so this needs the extra params. + body.script.inline += 'ctx._source.' + key + '=params.' + key + ';'; + body.script.params[key] = value; + if (key === 'docType') { + body.script.params[key] = modelName; + } } - }, defaults)).then( - function (response) { - // TODO: what does the framework want us to return as arguments w/ callback? - callback(null, response); - //callback(null, response._id); - //callback(null, data); - }, - function (err) { + }); + + var document = _.defaults({ + body: body + }, defaults); + log('ESConnector.prototype.updateAttributes', 'document to update', document); + + self.db.updateByQuery(document) + .then(function (response) { + log('ESConnector.prototype.updateAttributes', 'response', response); + return callback(null, { + updated: response.updated, + total: response.total + }); + }, function (err) { log('ESConnector.prototype.updateAttributes', err.message); if (err) { return callback(err, null); } - } - ); + }); }; /** @@ -1465,7 +1487,7 @@ ESConnector.prototype.save = function (model, data, done) { if (id === undefined || id === null) { return done('Document id not setted!', null); } - + data.docType = model; self.db.update(_.defaults({ id: id, body: { @@ -1509,6 +1531,7 @@ ESConnector.prototype.updateOrCreate = function updateOrCreate(modelName, data, } var defaults = self.addDefaults(modelName, 'updateOrCreate'); + data.docType = modelName; self.db.update(_.defaults({ id: id, body: { @@ -1583,6 +1606,7 @@ ESConnector.prototype.replaceOrCreate = function (modelName, data, callback) { document[self.idField] = id; document.body = {}; _.assign(document.body, data); + document.body.docType = modelName; log('ESConnector.prototype.replaceOrCreate', 'document', document); self.db.index( document @@ -1615,6 +1639,7 @@ ESConnector.prototype.replaceById = function replace(modelName, id, data, option document[self.idField] = self.getDocumentId(id); document.body = {}; _.assign(document.body, data); + document.body.docType = modelName; if (modelProperties.hasOwnProperty(idName)) { document.body[idName] = id; } diff --git a/package.json b/package.json index 83dba36..4a2108b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "loopback-connector-esv6", - "version": "1.2.2", + "version": "1.3.0", "description": "LoopBack Connector for Elasticsearch 6.x", "main": "index.js", "scripts": {