Skip to content

Instantly share code, notes, and snippets.

@juanpujol
Last active August 7, 2016 00:40
Show Gist options
  • Save juanpujol/3a3ad4aa8a60b649f632127bd5814d2f to your computer and use it in GitHub Desktop.
Save juanpujol/3a3ad4aa8a60b649f632127bd5814d2f to your computer and use it in GitHub Desktop.
loopback-connector-es destroyAll fix for v2.x
/**
* 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);
}
}
);
};
@pulkitsinghal
Copy link

Questions:

  1. should self.settings.apiVersion.startsWith('2') be replaced with self.settings.apiVersion.indexOf('2') === 0 ?

  2. should:

    throw new Error('id is required');
    return cb(null);
    

    be replaced with return cb(new Error('id is required'), null); ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment