UNPKG

jsm-utilities

Version:
84 lines (83 loc) 3.41 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.applyOnDocumentChunks = void 0; const utils_helpers_1 = require("../../../common/helpers/utils.helpers"); const objects_1 = require("../../../common/objects/objects"); /** * * @param {number} chunkSize * - optional * - default 100 * @param {number} maxItems * - optional * - default -1 * @param {number} totalCount * - optional * - default 0 * @update 2020-12-06 added allowDiskUse(true) to query */ const applyOnDocumentChunks = (params) => __awaiter(void 0, void 0, void 0, function* () { const { query, onChunk, onItem, chunkSize, maxItems, totalCount, log } = Object.assign(Object.assign({}, (0, objects_1.extractPartialObject)(params, ["query", "onChunk", "onItem"])), (0, utils_helpers_1.defaults)((0, objects_1.extractPartialObject)(params, ["chunkSize", "maxItems", "totalCount"]), { chunkSize: 100, maxItems: -1, totalCount: 0, log: false })); let page = 0; let items_found = true; let counter = 0; while (items_found && (maxItems === -1 || counter < maxItems)) { page++; if (log) console.log(`loading ${chunkSize} items, page: ${page}`); const items = yield query .clone() .allowDiskUse(true) .limit(chunkSize) .skip((page - 1) * chunkSize); if (log) console.log(`loaded ${items.length} items, page: ${page}`, query.getQuery()); items_found = items.length > 0; if (!items_found && log) console.log('The end of items'); if (!onItem) counter += items.length; /* ----------------------------- Handle onChunk ----------------------------- */ if (onChunk && items_found) { const progress = totalCount ? parseFloat(((counter * 100) / totalCount).toFixed(2)) : 0; yield onChunk(items, page, progress); } /* ----------------------------- Handle onItem ----------------------------- */ if (onItem) { for (const [index, item] of items.entries()) { if (maxItems > 0 && counter >= maxItems) { break; } counter++; const progress = totalCount ? parseFloat(((counter * 100) / totalCount).toFixed(2)) : 0; yield onItem(item, counter, page, progress); } } } if (log) console.log('Done'); return { totalProcessed: counter, totalPages: page, completed: true, }; }); exports.applyOnDocumentChunks = applyOnDocumentChunks;