2017-01-06 03:32:41 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-12-07 16:13:48 +00:00
|
|
|
const assert = require('assert');
|
|
|
|
const debug = require('debug')('loopback:kvao:delete');
|
|
|
|
const utils = require('../utils');
|
2017-01-06 03:32:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete the key-value pair associated to the given key.
|
|
|
|
*
|
|
|
|
* @param {String} key Key to use when searching the database.
|
|
|
|
* @options {Object} options
|
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err Error object.
|
|
|
|
* @param {*} result Value associated with the given key.
|
|
|
|
* @promise
|
|
|
|
*
|
|
|
|
* @header KVAO.prototype.delete(key[, options], cb)
|
|
|
|
*/
|
|
|
|
module.exports = function keyValueDelete(key, options, callback) {
|
|
|
|
if (callback == undefined && typeof options === 'function') {
|
|
|
|
callback = options;
|
|
|
|
options = {};
|
|
|
|
} else if (!options) {
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(typeof key === 'string' && key, 'key must be a non-empty string');
|
|
|
|
|
|
|
|
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.delete === 'function') {
|
|
|
|
connector.delete(this.modelName, key, 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;
|
|
|
|
};
|