@atscript/db-mongo 0.1.208 → 0.1.209
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agg.cjs +17 -50
- package/dist/agg.mjs +18 -51
- package/dist/index.cjs +133 -63
- package/dist/index.d.cts +38 -11
- package/dist/index.d.mts +38 -11
- package/dist/index.mjs +134 -64
- package/dist/{mongo-accumulator-DLHfj5bv.cjs → mongo-accumulator-Chn-xOiV.cjs} +150 -56
- package/dist/{mongo-accumulator-DyBCoPSm.mjs → mongo-accumulator-DoHOVkri.mjs} +126 -56
- package/dist/plugin.cjs +7 -2
- package/dist/plugin.mjs +7 -2
- package/package.json +9 -9
package/dist/agg.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_mongo_accumulator = require("./mongo-accumulator-
|
|
2
|
+
const require_mongo_accumulator = require("./mongo-accumulator-Chn-xOiV.cjs");
|
|
3
3
|
let _atscript_db = require("@atscript/db");
|
|
4
4
|
let _uniqu_core = require("@uniqu/core");
|
|
5
5
|
let _atscript_db_agg = require("@atscript/db/agg");
|
|
@@ -13,12 +13,13 @@ let _atscript_db_agg = require("@atscript/db/agg");
|
|
|
13
13
|
*/
|
|
14
14
|
/**
|
|
15
15
|
* The condition of a conditional aggregate as an aggregation expression, or
|
|
16
|
-
* `undefined
|
|
17
|
-
*
|
|
18
|
-
*
|
|
16
|
+
* `undefined`. Its comparands arrive in storage form: a decimal / long
|
|
17
|
+
* field's a `Decimal128` / `Long` (the field's formatter), an exact inner
|
|
18
|
+
* column's at the outer level of a two-level aggregate a `Decimal128` (the
|
|
19
|
+
* core's `exactComparand`, since 0.1.209).
|
|
19
20
|
*/
|
|
20
|
-
function conditionOf(where
|
|
21
|
-
return where === void 0 ? void 0 : require_mongo_accumulator.whereToExpr(
|
|
21
|
+
function conditionOf(where) {
|
|
22
|
+
return where === void 0 ? void 0 : require_mongo_accumulator.whereToExpr(where);
|
|
22
23
|
}
|
|
23
24
|
/**
|
|
24
25
|
* Maps an AggregateExpr to its MongoDB `$group` accumulator (see
|
|
@@ -39,40 +40,16 @@ function toAccumulator(expr, where, select) {
|
|
|
39
40
|
const asDouble = (path) => ({ $toDouble: `$${path}` });
|
|
40
41
|
/**
|
|
41
42
|
* The operand a single-field aggregate reads: `"*"` for `count(*)`, the field
|
|
42
|
-
* path —
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
43
|
+
* path — for a `sum` / `avg` of a `decimal` or `long` field read as a
|
|
44
|
+
* `Decimal128` ({@link asDecimal}; longs since 0.1.209): exact. A `min` /
|
|
45
|
+
* `max` / `first` / `last` reads the stored `Decimal128` / `Long`, ordered by
|
|
46
|
+
* value. The core reads the result back as the alias kind's text.
|
|
46
47
|
*/
|
|
47
48
|
function sourceOperand(expr, select) {
|
|
48
49
|
const field = expr.$field;
|
|
49
50
|
if (field === "*") return "*";
|
|
50
|
-
return
|
|
51
|
+
return (expr.$fn === "sum" || expr.$fn === "avg") && (0, _atscript_db.exactKindOf)(select?.sources.get(field)?.designType) !== void 0 ? require_mongo_accumulator.asDecimal(`$${field}`) : `$${field}`;
|
|
51
52
|
}
|
|
52
|
-
/** Whether `expr` is a `sum` / `avg` / `min` / `max` of a decimal — a `Decimal128` ({@link sourceOperand}). */
|
|
53
|
-
function readsDecimal(expr, select) {
|
|
54
|
-
return typeof expr.$field === "string" && expr.$fn !== "count" && expr.$fn !== "countDistinct" && select?.sources.get(expr.$field)?.designType === "decimal";
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* The columns of a level's rows that hold `Decimal128`s (since 0.1.207): its
|
|
58
|
-
* `sum` / `avg` / `min` / `max` of a decimal ({@link readsDecimal}) and, at
|
|
59
|
-
* the outer level of a two-level aggregate, a group key or `first` / `last`
|
|
60
|
-
* of an inner one — a `$having` comparand of one is compared as a
|
|
61
|
-
* `Decimal128` ({@link decimalComparands}).
|
|
62
|
-
*/
|
|
63
|
-
function decimalAliases(controls) {
|
|
64
|
-
const select = controls.$select;
|
|
65
|
-
const out = /* @__PURE__ */ new Set();
|
|
66
|
-
for (const expr of select?.aggregates ?? []) if (readsDecimal(expr, select)) out.add((0, _atscript_db_agg.resolveAlias)(expr));
|
|
67
|
-
const inner = controls.$from ? decimalAliases(controls.$from) : void 0;
|
|
68
|
-
if (inner?.size) {
|
|
69
|
-
for (const key of controls.$groupBy ?? []) if (inner.has(key)) out.add(key);
|
|
70
|
-
for (const fl of select?.firstLast ?? []) if (inner.has(fl.column)) out.add(fl.alias);
|
|
71
|
-
}
|
|
72
|
-
return out;
|
|
73
|
-
}
|
|
74
|
-
/** Prefix of the hidden `$group` field counting a sum's non-null values. */
|
|
75
|
-
const NON_NULL_PREFIX = "__as_n_";
|
|
76
53
|
const DAY_MS = 864e5;
|
|
77
54
|
const ISO_DATE = "%Y-%m-%d";
|
|
78
55
|
/** Units `$dateToString` labels straight from the instant in the zone (literal `01` for day / month). */
|
|
@@ -219,27 +196,17 @@ function buildGroupedStages(controls, pipeline, accumulators) {
|
|
|
219
196
|
if (firstLast.length > 0 && rowOrder.length > 0) pipeline.push(...require_mongo_accumulator.rowOrderStages(rowOrder));
|
|
220
197
|
const project = { _id: 0 };
|
|
221
198
|
for (const [field, idKey] of groupKeys) project[field] = `$_id.${idKey}`;
|
|
222
|
-
const nullWhenEmpty = (alias, value, where) => {
|
|
223
|
-
const count = `${NON_NULL_PREFIX}${alias}`;
|
|
224
|
-
groupStage[count] = require_mongo_accumulator.buildAccumulator("count", value, where);
|
|
225
|
-
return { $cond: [
|
|
226
|
-
{ $eq: [`$${count}`, 0] },
|
|
227
|
-
null,
|
|
228
|
-
`$${alias}`
|
|
229
|
-
] };
|
|
230
|
-
};
|
|
231
|
-
const rowDecimals = controls.$from ? decimalAliases(controls.$from) : /* @__PURE__ */ new Set();
|
|
232
199
|
for (const expr of controls.$select?.aggregates ?? []) {
|
|
233
200
|
const alias = (0, _atscript_db_agg.resolveAlias)(expr);
|
|
234
|
-
const where = conditionOf(expr.$where
|
|
201
|
+
const where = conditionOf(expr.$where);
|
|
235
202
|
groupStage[alias] = toAccumulator(expr, where, controls.$select);
|
|
236
|
-
project[alias] = expr.$fn === "countDistinct" ? require_mongo_accumulator.distinctCountExpr(`$${alias}`) : expr.$fn === "sum" ? nullWhenEmpty(alias, sourceOperand(expr, controls.$select), where) : 1;
|
|
203
|
+
project[alias] = expr.$fn === "countDistinct" ? require_mongo_accumulator.distinctCountExpr(`$${alias}`) : expr.$fn === "sum" ? require_mongo_accumulator.nullWhenEmpty(groupStage, alias, sourceOperand(expr, controls.$select), where) : 1;
|
|
237
204
|
}
|
|
238
205
|
for (const e of controls.$select?.exprAggregates ?? []) {
|
|
239
206
|
const value = require_mongo_accumulator.exprToMongo(e.expr, asDouble);
|
|
240
|
-
const where = conditionOf(e.where
|
|
207
|
+
const where = conditionOf(e.where);
|
|
241
208
|
groupStage[e.alias] = require_mongo_accumulator.buildAccumulator(e.fn, value, where);
|
|
242
|
-
project[e.alias] = e.fn === "sum" ? nullWhenEmpty(e.alias, value, where) : 1;
|
|
209
|
+
project[e.alias] = e.fn === "sum" ? require_mongo_accumulator.nullWhenEmpty(groupStage, e.alias, value, where) : 1;
|
|
243
210
|
}
|
|
244
211
|
for (const fl of firstLast) {
|
|
245
212
|
groupStage[fl.alias] = { [`$${fl.fn}`]: require_mongo_accumulator.orNull(`$${fl.column}`) };
|
|
@@ -248,7 +215,7 @@ function buildGroupedStages(controls, pipeline, accumulators) {
|
|
|
248
215
|
pipeline.push({ $group: groupStage });
|
|
249
216
|
pipeline.push({ $project: project });
|
|
250
217
|
for (const e of controls.$select?.exprs ?? []) pipeline.push({ $addFields: { [e.alias]: require_mongo_accumulator.exprToMongo(e.expr, asDouble) } });
|
|
251
|
-
if (controls.$having) pipeline.push({ $match: require_mongo_accumulator.buildMongoFilter(
|
|
218
|
+
if (controls.$having) pipeline.push({ $match: require_mongo_accumulator.buildMongoFilter(controls.$having) });
|
|
252
219
|
return pipeline;
|
|
253
220
|
}
|
|
254
221
|
/**
|
package/dist/agg.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { DbError, aggregateColumns, evaluateExpr } from "@atscript/db";
|
|
1
|
+
import { _ as orNull, a as nullWhenEmpty, g as exprToMongo, h as sortStages, i as distinctCountExpr, m as rowOrderStages, r as buildAccumulator, s as buildMongoFilter, t as asDecimal, u as mongoFilterStages, y as whereToExpr } from "./mongo-accumulator-DoHOVkri.mjs";
|
|
2
|
+
import { DbError, aggregateColumns, evaluateExpr, exactKindOf } from "@atscript/db";
|
|
3
3
|
import { BUCKET_MAX_INSTANT, BUCKET_MIN_INSTANT } from "@uniqu/core";
|
|
4
4
|
import { assertAggregateFn, resolveAlias } from "@atscript/db/agg";
|
|
5
5
|
//#region src/agg.ts
|
|
@@ -12,12 +12,13 @@ import { assertAggregateFn, resolveAlias } from "@atscript/db/agg";
|
|
|
12
12
|
*/
|
|
13
13
|
/**
|
|
14
14
|
* The condition of a conditional aggregate as an aggregation expression, or
|
|
15
|
-
* `undefined
|
|
16
|
-
*
|
|
17
|
-
*
|
|
15
|
+
* `undefined`. Its comparands arrive in storage form: a decimal / long
|
|
16
|
+
* field's a `Decimal128` / `Long` (the field's formatter), an exact inner
|
|
17
|
+
* column's at the outer level of a two-level aggregate a `Decimal128` (the
|
|
18
|
+
* core's `exactComparand`, since 0.1.209).
|
|
18
19
|
*/
|
|
19
|
-
function conditionOf(where
|
|
20
|
-
return where === void 0 ? void 0 : whereToExpr(
|
|
20
|
+
function conditionOf(where) {
|
|
21
|
+
return where === void 0 ? void 0 : whereToExpr(where);
|
|
21
22
|
}
|
|
22
23
|
/**
|
|
23
24
|
* Maps an AggregateExpr to its MongoDB `$group` accumulator (see
|
|
@@ -38,40 +39,16 @@ function toAccumulator(expr, where, select) {
|
|
|
38
39
|
const asDouble = (path) => ({ $toDouble: `$${path}` });
|
|
39
40
|
/**
|
|
40
41
|
* The operand a single-field aggregate reads: `"*"` for `count(*)`, the field
|
|
41
|
-
* path —
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
42
|
+
* path — for a `sum` / `avg` of a `decimal` or `long` field read as a
|
|
43
|
+
* `Decimal128` ({@link asDecimal}; longs since 0.1.209): exact. A `min` /
|
|
44
|
+
* `max` / `first` / `last` reads the stored `Decimal128` / `Long`, ordered by
|
|
45
|
+
* value. The core reads the result back as the alias kind's text.
|
|
45
46
|
*/
|
|
46
47
|
function sourceOperand(expr, select) {
|
|
47
48
|
const field = expr.$field;
|
|
48
49
|
if (field === "*") return "*";
|
|
49
|
-
return
|
|
50
|
+
return (expr.$fn === "sum" || expr.$fn === "avg") && exactKindOf(select?.sources.get(field)?.designType) !== void 0 ? asDecimal(`$${field}`) : `$${field}`;
|
|
50
51
|
}
|
|
51
|
-
/** Whether `expr` is a `sum` / `avg` / `min` / `max` of a decimal — a `Decimal128` ({@link sourceOperand}). */
|
|
52
|
-
function readsDecimal(expr, select) {
|
|
53
|
-
return typeof expr.$field === "string" && expr.$fn !== "count" && expr.$fn !== "countDistinct" && select?.sources.get(expr.$field)?.designType === "decimal";
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* The columns of a level's rows that hold `Decimal128`s (since 0.1.207): its
|
|
57
|
-
* `sum` / `avg` / `min` / `max` of a decimal ({@link readsDecimal}) and, at
|
|
58
|
-
* the outer level of a two-level aggregate, a group key or `first` / `last`
|
|
59
|
-
* of an inner one — a `$having` comparand of one is compared as a
|
|
60
|
-
* `Decimal128` ({@link decimalComparands}).
|
|
61
|
-
*/
|
|
62
|
-
function decimalAliases(controls) {
|
|
63
|
-
const select = controls.$select;
|
|
64
|
-
const out = /* @__PURE__ */ new Set();
|
|
65
|
-
for (const expr of select?.aggregates ?? []) if (readsDecimal(expr, select)) out.add(resolveAlias(expr));
|
|
66
|
-
const inner = controls.$from ? decimalAliases(controls.$from) : void 0;
|
|
67
|
-
if (inner?.size) {
|
|
68
|
-
for (const key of controls.$groupBy ?? []) if (inner.has(key)) out.add(key);
|
|
69
|
-
for (const fl of select?.firstLast ?? []) if (inner.has(fl.column)) out.add(fl.alias);
|
|
70
|
-
}
|
|
71
|
-
return out;
|
|
72
|
-
}
|
|
73
|
-
/** Prefix of the hidden `$group` field counting a sum's non-null values. */
|
|
74
|
-
const NON_NULL_PREFIX = "__as_n_";
|
|
75
52
|
const DAY_MS = 864e5;
|
|
76
53
|
const ISO_DATE = "%Y-%m-%d";
|
|
77
54
|
/** Units `$dateToString` labels straight from the instant in the zone (literal `01` for day / month). */
|
|
@@ -218,27 +195,17 @@ function buildGroupedStages(controls, pipeline, accumulators) {
|
|
|
218
195
|
if (firstLast.length > 0 && rowOrder.length > 0) pipeline.push(...rowOrderStages(rowOrder));
|
|
219
196
|
const project = { _id: 0 };
|
|
220
197
|
for (const [field, idKey] of groupKeys) project[field] = `$_id.${idKey}`;
|
|
221
|
-
const nullWhenEmpty = (alias, value, where) => {
|
|
222
|
-
const count = `${NON_NULL_PREFIX}${alias}`;
|
|
223
|
-
groupStage[count] = buildAccumulator("count", value, where);
|
|
224
|
-
return { $cond: [
|
|
225
|
-
{ $eq: [`$${count}`, 0] },
|
|
226
|
-
null,
|
|
227
|
-
`$${alias}`
|
|
228
|
-
] };
|
|
229
|
-
};
|
|
230
|
-
const rowDecimals = controls.$from ? decimalAliases(controls.$from) : /* @__PURE__ */ new Set();
|
|
231
198
|
for (const expr of controls.$select?.aggregates ?? []) {
|
|
232
199
|
const alias = resolveAlias(expr);
|
|
233
|
-
const where = conditionOf(expr.$where
|
|
200
|
+
const where = conditionOf(expr.$where);
|
|
234
201
|
groupStage[alias] = toAccumulator(expr, where, controls.$select);
|
|
235
|
-
project[alias] = expr.$fn === "countDistinct" ? distinctCountExpr(`$${alias}`) : expr.$fn === "sum" ? nullWhenEmpty(alias, sourceOperand(expr, controls.$select), where) : 1;
|
|
202
|
+
project[alias] = expr.$fn === "countDistinct" ? distinctCountExpr(`$${alias}`) : expr.$fn === "sum" ? nullWhenEmpty(groupStage, alias, sourceOperand(expr, controls.$select), where) : 1;
|
|
236
203
|
}
|
|
237
204
|
for (const e of controls.$select?.exprAggregates ?? []) {
|
|
238
205
|
const value = exprToMongo(e.expr, asDouble);
|
|
239
|
-
const where = conditionOf(e.where
|
|
206
|
+
const where = conditionOf(e.where);
|
|
240
207
|
groupStage[e.alias] = buildAccumulator(e.fn, value, where);
|
|
241
|
-
project[e.alias] = e.fn === "sum" ? nullWhenEmpty(e.alias, value, where) : 1;
|
|
208
|
+
project[e.alias] = e.fn === "sum" ? nullWhenEmpty(groupStage, e.alias, value, where) : 1;
|
|
242
209
|
}
|
|
243
210
|
for (const fl of firstLast) {
|
|
244
211
|
groupStage[fl.alias] = { [`$${fl.fn}`]: orNull(`$${fl.column}`) };
|
|
@@ -247,7 +214,7 @@ function buildGroupedStages(controls, pipeline, accumulators) {
|
|
|
247
214
|
pipeline.push({ $group: groupStage });
|
|
248
215
|
pipeline.push({ $project: project });
|
|
249
216
|
for (const e of controls.$select?.exprs ?? []) pipeline.push({ $addFields: { [e.alias]: exprToMongo(e.expr, asDouble) } });
|
|
250
|
-
if (controls.$having) pipeline.push({ $match: buildMongoFilter(
|
|
217
|
+
if (controls.$having) pipeline.push({ $match: buildMongoFilter(controls.$having) });
|
|
251
218
|
return pipeline;
|
|
252
219
|
}
|
|
253
220
|
/**
|