UNPKG

contexture-mongo

Version:
88 lines (87 loc) 3.37 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; 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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var dateHistogram_exports = {}; __export(dateHistogram_exports, { default: () => dateHistogram_default }); module.exports = __toCommonJS(dateHistogram_exports); var import_fp = __toESM(require("lodash/fp.js")); var import_futil = __toESM(require("futil")); var import_statistical = require("./statistical.js"); var dateHistogram_default = { async result({ key_field, value_field, interval, include = ["min", "max", "avg", "sum"], timezone }, search) { let stats = import_fp.default.omit(["_id"], (0, import_statistical.statsAgg)(value_field).$group); stats.cardinality = { $addToSet: `$${value_field}` }; let timeAgg = timezone ? { date: `$${key_field}`, timezone } : `$${key_field}`; return { entries: import_fp.default.map( (x) => ({ ...x, key: (/* @__PURE__ */ new Date(`${x.year}-${x.month || 1}-${x.day || 1}Z`)).getTime() }), await search([ { $group: { _id: { ...interval === "day" && { day: { $dayOfMonth: timeAgg } }, ...import_fp.default.includes(interval, ["day", "month"]) && { month: { $month: timeAgg } }, year: { $year: timeAgg } }, ...import_fp.default.pick(include, stats) } }, // Mongo ignore missing fields during project :) { $project: { day: "$_id.day", month: "$_id.month", year: "$_id.year", _id: 0, ...import_futil.default.arrayToObject( import_fp.default.identity, (x) => x === "cardinality" ? { $size: "$cardinality" } : 1, include ) } }, { $sort: { year: 1, month: 1, day: 1 } } ]) ) }; } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = {});