@microtica/database
Version:
Database tools
40 lines • 1.85 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.processInChunks = exports.getInChunks = void 0;
const Promise = require("bluebird");
const _ = require("lodash");
/**
* Facilitates query-chunking by allowing you to process a list in chunks and aggregating the results.
* For a version that does not expect results to be returned, use processInChunks.
* @param values the values to process.
* @param subsetCallback the callback to call for each subset of results.
* Each subset is expected to produce a promise that resolves to a list of results.
* @param [chunkSize] the chunk size. If not specified, will use a server-configured value.
* @return {Promise<R[]>} returns a list of combined results from all chunks.
*/
function getInChunks(values, subsetCallback, chunkSize = 300) {
if (values.length === 0) {
return Promise.resolve([]);
}
return Promise.map(_.chunk(values, chunkSize), valueSubset => {
return subsetCallback(valueSubset);
}).then(chunks => _.flatten(chunks));
}
exports.getInChunks = getInChunks;
/**
* Facilitates query-chunking by allowing you to process a list in chunks.
* For a version that aggregates the return results, use getInChunks.
* @param values the values to process.
* @param subsetCallback the callback to call for each subset of results. Each subset is expected to produce a promise.
* @param [chunkSize] the chunk size. If not specified, will use a server-configured value.
*/
function processInChunks(values, subsetCallback, chunkSize = 300) {
if (values.length === 0) {
return Promise.resolve();
}
return Promise.map(_.chunk(values, chunkSize), valueSubset => {
return subsetCallback(valueSubset);
}).return();
}
exports.processInChunks = processInChunks;
//# sourceMappingURL=chunked-query.js.map