Skip to content

Commit

Permalink
Merge pull request #120 from bharathkontham/master
Browse files Browse the repository at this point in the history
Fix for model update by Id and docType modification issue
  • Loading branch information
bharathkontham authored Mar 15, 2019
2 parents c5c6963 + caac914 commit 0c61705
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 25 deletions.
73 changes: 49 additions & 24 deletions lib/esConnector.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
});

Expand All @@ -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
});
Expand Down Expand Up @@ -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);
}
}
);
});
};

/**
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down

0 comments on commit 0c61705

Please sign in to comment.