2020-01-21 18:12:14 +00:00
|
|
|
// Copyright IBM Corp. 2016,2019. All Rights Reserved.
|
2019-05-08 15:45:37 +00:00
|
|
|
// Node module: loopback-datasource-juggler
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2016-08-15 12:55:26 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const assert = require('assert');
|
|
|
|
const utils = require('../utils');
|
2016-08-15 12:55:26 +00:00
|
|
|
|
|
|
|
/**
|
2016-09-17 00:03:33 +00:00
|
|
|
* Asynchronously iterate all keys in the database. Similar to `.keys()` but
|
|
|
|
* instead allows for iteration over large data sets without having to load
|
|
|
|
* everything into memory at once.
|
2016-08-15 12:55:26 +00:00
|
|
|
*
|
|
|
|
* @param {Object} filter An optional filter object with the following
|
2016-09-17 00:03:33 +00:00
|
|
|
* @param {String} filter.match Glob string to use to filter returned
|
|
|
|
* keys (i.e. `userid.*`). All connectors are required to support `*` and
|
|
|
|
* `?`. They may also support additional special characters that are
|
|
|
|
* specific to the backing database.
|
2016-08-15 12:55:26 +00:00
|
|
|
* @param {Object} options
|
2016-09-17 00:03:33 +00:00
|
|
|
* @returns {AsyncIterator} An Object implementing `next(cb) -> Promise`
|
|
|
|
* function that can be used to iterate all keys.
|
2016-08-15 12:55:26 +00:00
|
|
|
*
|
|
|
|
* @header KVAO.iterateKeys(filter)
|
|
|
|
*/
|
|
|
|
module.exports = function keyValueIterateKeys(filter, options) {
|
|
|
|
filter = filter || {};
|
|
|
|
options = options || {};
|
|
|
|
|
|
|
|
assert(typeof filter === 'object', 'filter must be an object');
|
|
|
|
assert(typeof options === 'object', 'options must be an object');
|
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const iter = this.getConnector().iterateKeys(this.modelName, filter, options);
|
2016-08-15 12:55:26 +00:00
|
|
|
// promisify the returned iterator
|
|
|
|
return {
|
|
|
|
next: function(callback) {
|
|
|
|
callback = callback || utils.createPromiseCallback();
|
|
|
|
iter.next(callback);
|
|
|
|
return callback.promise;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|