Add flush operation to KVAO

Used to delete all keys (and values) associated to the current model.
This commit is contained in:
Simon Ho 2016-12-30 17:27:49 -08:00
parent d095cc1161
commit f38709b7cb
4 changed files with 63 additions and 0 deletions

View File

@ -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);

30
lib/kvao/flush.js Normal file
View File

@ -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;
};

View File

@ -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');

26
test/kvao/flush.suite.js Normal file
View File

@ -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([]);
});
});
});
};