2019-05-08 15:45:37 +00:00
|
|
|
// Copyright IBM Corp. 2016,2018. All Rights Reserved.
|
|
|
|
// Node module: loopback-datasource-juggler
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2016-08-08 08:15:22 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const assert = require('assert');
|
|
|
|
const utils = require('../utils');
|
2016-08-08 08:15:22 +00:00
|
|
|
|
|
|
|
/**
|
2016-09-17 00:03:33 +00:00
|
|
|
* Set the TTL (time to live) in ms (milliseconds) for a given key. TTL is the
|
|
|
|
* remaining time before a key-value pair is discarded from the database.
|
2016-08-08 08:15:22 +00:00
|
|
|
*
|
2016-09-17 00:03:33 +00:00
|
|
|
* @param {String} key Key to use when searching the database.
|
|
|
|
* @param {Number} ttl TTL in ms to set for the key.
|
|
|
|
* @options {Object} options
|
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err Error object.
|
|
|
|
* @promise
|
2016-08-08 08:15:22 +00:00
|
|
|
*
|
2016-09-17 00:03:33 +00:00
|
|
|
* @header KVAO.expire(key, ttl, cb)
|
2016-08-08 08:15:22 +00:00
|
|
|
*/
|
|
|
|
module.exports = function keyValueExpire(key, ttl, 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');
|
|
|
|
assert(typeof ttl === 'number' && ttl > 0, 'ttl must be a positive integer');
|
|
|
|
assert(typeof options === 'object', 'options must be an object');
|
|
|
|
|
|
|
|
callback = callback || utils.createPromiseCallback();
|
|
|
|
this.getConnector().expire(this.modelName, key, ttl, options, callback);
|
|
|
|
return callback.promise;
|
|
|
|
};
|