2017-01-06 03:32:41 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-12-07 16:13:48 +00:00
|
|
|
const assert = require('assert');
|
|
|
|
const async = require('async');
|
|
|
|
const debug = require('debug')('loopback:kvao:delete-all');
|
|
|
|
const utils = require('../utils');
|
2017-01-06 03:32:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete all keys (and values) associated to the current model.
|
|
|
|
*
|
|
|
|
* @options {Object} options Unused ATM, placeholder for future options.
|
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err Error object.
|
|
|
|
* @promise
|
|
|
|
*
|
|
|
|
* @header KVAO.prototype.deleteAll([options, ]cb)
|
|
|
|
*/
|
|
|
|
module.exports = function deleteAll(options, callback) {
|
|
|
|
if (callback == undefined && typeof options === 'function') {
|
|
|
|
callback = options;
|
|
|
|
options = {};
|
|
|
|
} else if (!options) {
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(typeof options === 'object', 'options must be an object');
|
|
|
|
|
|
|
|
callback = callback || utils.createPromiseCallback();
|
|
|
|
|
2018-12-07 16:13:48 +00:00
|
|
|
const connector = this.getConnector();
|
2017-01-06 03:32:41 +00:00
|
|
|
if (typeof connector.deleteAll === 'function') {
|
|
|
|
connector.deleteAll(this.modelName, options, callback);
|
|
|
|
} else if (typeof connector.delete === 'function') {
|
|
|
|
debug('Falling back to unoptimized key-value pair deletion');
|
|
|
|
iterateAndDelete(connector, this.modelName, options, callback);
|
|
|
|
} else {
|
2018-12-07 16:13:48 +00:00
|
|
|
const errMsg = 'Connector does not support key-value pair deletion';
|
2017-01-06 03:32:41 +00:00
|
|
|
debug(errMsg);
|
|
|
|
process.nextTick(function() {
|
2018-12-07 16:13:48 +00:00
|
|
|
const err = new Error(errMsg);
|
2017-01-06 03:32:41 +00:00
|
|
|
err.statusCode = 501;
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return callback.promise;
|
|
|
|
};
|
|
|
|
|
|
|
|
function iterateAndDelete(connector, modelName, options, callback) {
|
2018-12-07 16:13:48 +00:00
|
|
|
const iter = connector.iterateKeys(modelName, {});
|
|
|
|
const keys = [];
|
2017-01-06 03:32:41 +00:00
|
|
|
iter.next(onNextKey);
|
|
|
|
|
|
|
|
function onNextKey(err, key) {
|
|
|
|
if (err) return callback(err);
|
|
|
|
if (key === undefined) return callback();
|
|
|
|
connector.delete(modelName, key, options, onDeleted);
|
|
|
|
}
|
|
|
|
|
|
|
|
function onDeleted(err) {
|
|
|
|
if (err) return callback(err);
|
|
|
|
iter.next(onNextKey);
|
|
|
|
}
|
|
|
|
}
|