loopback-datasource-juggler/lib/kvao/keys.js

57 lines
1.6 KiB
JavaScript
Raw Normal View History

'use strict';
2018-12-07 14:54:29 +00:00
const assert = require('assert');
const utils = require('../utils');
/**
2016-09-17 00:03:33 +00:00
* Return all keys in the database.
*
2016-09-17 00:03:33 +00:00
* **WARNING**: This method is not suitable for large data sets as all
* key-values pairs are loaded into memory at once. For large data sets,
* use `iterateKeys()` instead.
*
* @param {Object} filter An optional filter object with the following
2016-09-17 00:03:33 +00:00
* @param {String} filter.match Glob string used to filter returned
* keys (i.e. `userid.*`). All connectors are required to support `*` and
* `?`, but may also support additional special characters specific to the
* database.
* @param {Object} options
2016-09-17 00:03:33 +00:00
* @callback {Function} callback
* @promise
*
2016-09-17 00:03:33 +00:00
*
* @header KVAO.keys(filter, callback)
*/
module.exports = function keyValueKeys(filter, options, callback) {
if (callback === undefined) {
if (typeof options === 'function') {
callback = options;
options = undefined;
} else if (options === undefined && typeof filter === 'function') {
callback = filter;
filter = undefined;
}
}
filter = filter || {};
options = options || {};
assert(typeof filter === 'object', 'filter must be an object');
assert(typeof options === 'object', 'options must be an object');
callback = callback || utils.createPromiseCallback();
2018-12-07 14:54:29 +00:00
const iter = this.iterateKeys(filter, options);
const keys = [];
iter.next(onNextKey);
function onNextKey(err, key) {
if (err) return callback(err);
if (key === undefined) return callback(null, keys);
keys.push(key);
iter.next(onNextKey);
}
return callback.promise;
};