Last active
August 7, 2016 00:40
-
-
Save juanpujol/3a3ad4aa8a60b649f632127bd5814d2f to your computer and use it in GitHub Desktop.
loopback-connector-es destroyAll fix for v2.x
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Delete model instances by query | |
* @param {String} modelName name | |
* @param {String} whereClause criteria | |
* @param {Function} cb callback | |
*/ | |
ESConnector.prototype.destroyAll = function destroyAll(modelName, whereClause, cb) { | |
var self = this; | |
if ((!cb) && _.isFunction(whereClause)) { | |
cb = whereClause; | |
whereClause = {}; | |
} | |
console.log('ESConnector.prototype.destroyAll', 'modelName', modelName, 'whereClause', JSON.stringify(whereClause,null,0)); | |
var idName = self.idName(modelName); | |
var body = { | |
query: self.buildWhere(modelName, idName, whereClause).query | |
}; | |
var defaults = self.addDefaults(modelName); | |
/** | |
* if elasticsearch v2.x use delete api instead of deleteByQuery | |
*/ | |
if (self.settings.apiVersion.startsWith('2')) { | |
if (!whereClause.id) { | |
throw new Error('id is required'); | |
return cb(null); | |
} | |
return self.destroy(modelName, whereClause.id, cb); | |
} | |
self.db.deleteByQuery(_.defaults({ body: body }, defaults)).then( | |
function (response) { | |
cb(null, response); | |
}, | |
function (err) { | |
console.error(err.message); | |
if (err) { | |
return cb(err, null); | |
} | |
} | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Questions:
should
self.settings.apiVersion.startsWith('2')
be replaced withself.settings.apiVersion.indexOf('2') === 0
?should:
be replaced with
return cb(new Error('id is required'), null);
?