UNPKG

@azure/cosmos

Version:
119 lines (118 loc) 4.97 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var GroupByEndpointComponent_exports = {}; __export(GroupByEndpointComponent_exports, { GroupByEndpointComponent: () => GroupByEndpointComponent }); module.exports = __toCommonJS(GroupByEndpointComponent_exports); var import_hashObject = require("../../utils/hashObject.js"); var import_Aggregators = require("../Aggregators/index.js"); var import_headerUtils = require("../headerUtils.js"); var import_emptyGroup = require("./emptyGroup.js"); var import_parallelQueryResult = require("../parallelQueryResult.js"); class GroupByEndpointComponent { constructor(executionContext, queryInfo) { this.executionContext = executionContext; this.queryInfo = queryInfo; } groupings = /* @__PURE__ */ new Map(); aggregateResultArray = []; completed = false; hasMoreResults() { return this.executionContext.hasMoreResults(); } async fetchMore(diagnosticNode) { if (this.completed) { return { result: void 0, headers: (0, import_headerUtils.getInitialHeader)() }; } const aggregateHeaders = (0, import_headerUtils.getInitialHeader)(); const response = await this.executionContext.fetchMore(diagnosticNode); if (!response) { if (this.groupings.size > 0) { return this.consolidateGroupResults(aggregateHeaders); } return { result: void 0, headers: aggregateHeaders }; } (0, import_headerUtils.mergeHeaders)(aggregateHeaders, response.headers); if (response.result === void 0 || !Array.isArray(response.result.buffer) || response.result.buffer.length === 0) { if (this.groupings.size > 0) { return this.consolidateGroupResults(aggregateHeaders); } return { result: void 0, headers: aggregateHeaders }; } const parallelResult = response.result; const dataToProcess = parallelResult.buffer; for (const item of dataToProcess) { if (item) { const group = item.groupByItems ? await (0, import_hashObject.hashObject)(item.groupByItems) : import_emptyGroup.emptyGroup; const aggregators = this.groupings.get(group); const payload = item.payload; if (aggregators) { for (const key of Object.keys(payload)) { const effectiveGroupByValue = payload[key] ? payload[key] : (/* @__PURE__ */ new Map()).set("item2", null); const aggregateResult = (0, import_emptyGroup.extractAggregateResult)(effectiveGroupByValue); aggregators.get(key).aggregate(aggregateResult); } } else { const grouping = /* @__PURE__ */ new Map(); this.groupings.set(group, grouping); for (const key of Object.keys(payload)) { const aggregateType = this.queryInfo.groupByAliasToAggregateType[key]; const aggregator = (0, import_Aggregators.createAggregator)(aggregateType); grouping.set(key, aggregator); if (aggregateType) { const aggregateResult = (0, import_emptyGroup.extractAggregateResult)(payload[key]); aggregator.aggregate(aggregateResult); } else { aggregator.aggregate(payload[key]); } } } } } if (this.executionContext.hasMoreResults()) { const result = (0, import_parallelQueryResult.createParallelQueryResult)( [], // empty buffer /* @__PURE__ */ new Map() ); return { result, headers: aggregateHeaders }; } else { return this.consolidateGroupResults(aggregateHeaders); } } consolidateGroupResults(aggregateHeaders) { for (const grouping of this.groupings.values()) { const groupResult = {}; for (const [aggregateKey, aggregator] of grouping.entries()) { groupResult[aggregateKey] = aggregator.getResult(); } this.aggregateResultArray.push(groupResult); } this.completed = true; const result = (0, import_parallelQueryResult.createParallelQueryResult)(this.aggregateResultArray, /* @__PURE__ */ new Map()); return { result, headers: aggregateHeaders }; } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { GroupByEndpointComponent });