Merge pull request #1206 from strongloop/add-flush-op-to-kvao
Add flush operation to KVAO
This commit is contained in:
commit
79ec4211ab
|
@ -201,6 +201,12 @@ KeyValueMemoryConnector.prototype.disconnect = function(callback) {
|
|||
process.nextTick(callback);
|
||||
};
|
||||
|
||||
KeyValueMemoryConnector.prototype.flush =
|
||||
function(modelName, options, callback) {
|
||||
this._store = Object.create(null);
|
||||
callback();
|
||||
};
|
||||
|
||||
function StoreItem(value, ttl) {
|
||||
this.value = value;
|
||||
this.setTtl(ttl);
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
'use strict';
|
||||
|
||||
var assert = require('assert');
|
||||
var utils = require('../utils');
|
||||
|
||||
/**
|
||||
* 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.flush(options, cb)
|
||||
*/
|
||||
module.exports = function flush(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();
|
||||
|
||||
this.getConnector().flush(this.modelName, options, callback);
|
||||
return callback.promise;
|
||||
};
|
|
@ -8,6 +8,7 @@ module.exports = KeyValueAccessObject;
|
|||
KeyValueAccessObject.get = require('./get');
|
||||
KeyValueAccessObject.set = require('./set');
|
||||
KeyValueAccessObject.expire = require('./expire');
|
||||
KeyValueAccessObject.flush = require('./flush');
|
||||
KeyValueAccessObject.ttl = require('./ttl');
|
||||
KeyValueAccessObject.iterateKeys = require('./iterate-keys');
|
||||
KeyValueAccessObject.keys = require('./keys');
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
'use strict';
|
||||
|
||||
const bdd = require('../helpers/bdd-if');
|
||||
const helpers = require('./_helpers');
|
||||
const should = require('should');
|
||||
|
||||
module.exports = function(dataSourceFactory, connectorCapabilities) {
|
||||
var supportsFlushOperation =
|
||||
connectorCapabilities.supportsFlushOperation !== false;
|
||||
|
||||
bdd.describeIf(supportsFlushOperation, 'flush', function() {
|
||||
let CacheItem;
|
||||
beforeEach(function unpackContext() {
|
||||
CacheItem = helpers.givenCacheItem(dataSourceFactory);
|
||||
});
|
||||
|
||||
it('removes all associated keys for a given model', function() {
|
||||
return helpers.givenKeys(CacheItem, ['key1', 'key2'])
|
||||
.then(() => CacheItem.flush())
|
||||
.then(() => CacheItem.keys())
|
||||
.done((keys) => {
|
||||
should(keys).eql([]);
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
Loading…
Reference in New Issue