@atscript/db-mongo 0.1.135 → 0.1.137
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 +7 -23
- package/dist/agg.mjs +6 -22
- package/dist/index.cjs +259 -191
- package/dist/index.d.cts +16 -2
- package/dist/index.d.mts +16 -2
- package/dist/index.mjs +246 -178
- package/dist/mongo-accumulator-BalE9LkU.cjs +265 -0
- package/dist/mongo-accumulator-Dc0w4t0l.mjs +236 -0
- package/package.json +2 -2
- package/dist/mongo-filter-Bf5rWV4P.cjs +0 -73
- package/dist/mongo-filter-CtyITmZM.mjs +0 -68
package/dist/agg.cjs
CHANGED
|
@@ -1,27 +1,11 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const
|
|
2
|
+
const require_mongo_accumulator = require("./mongo-accumulator-BalE9LkU.cjs");
|
|
3
3
|
let _atscript_db_agg = require("@atscript/db/agg");
|
|
4
4
|
let _uniqu_core = require("@uniqu/core");
|
|
5
5
|
//#region src/agg.ts
|
|
6
|
-
/** `$group` accumulator
|
|
7
|
-
const ACCUMULATORS = {
|
|
8
|
-
sum: "$sum",
|
|
9
|
-
avg: "$avg",
|
|
10
|
-
min: "$min",
|
|
11
|
-
max: "$max"
|
|
12
|
-
};
|
|
13
|
-
/**
|
|
14
|
-
* Maps an AggregateExpr to a MongoDB $group accumulator expression.
|
|
15
|
-
*/
|
|
6
|
+
/** Maps an AggregateExpr to its MongoDB `$group` accumulator (see `buildAccumulator`). */
|
|
16
7
|
function toAccumulator(expr) {
|
|
17
|
-
|
|
18
|
-
if (expr.$fn !== "count") return { [ACCUMULATORS[expr.$fn]]: `$${expr.$field}` };
|
|
19
|
-
if (expr.$field === "*") return { $sum: 1 };
|
|
20
|
-
return { $sum: { $cond: [
|
|
21
|
-
{ $ne: [`$${expr.$field}`, null] },
|
|
22
|
-
1,
|
|
23
|
-
0
|
|
24
|
-
] } };
|
|
8
|
+
return require_mongo_accumulator.buildAccumulator(expr.$fn, expr.$field === "*" ? "*" : `$${expr.$field}`);
|
|
25
9
|
}
|
|
26
10
|
const DAY_MS = 864e5;
|
|
27
11
|
const ISO_DATE = "%Y-%m-%d";
|
|
@@ -120,13 +104,13 @@ function bucketExpression(b) {
|
|
|
120
104
|
function buildPrefix(query, searchStage) {
|
|
121
105
|
const controls = query.controls || {};
|
|
122
106
|
const groupBy = controls.$groupBy ?? [];
|
|
123
|
-
const pipeline = searchStage ? [searchStage, { $match:
|
|
107
|
+
const pipeline = searchStage ? [searchStage, { $match: require_mongo_accumulator.buildMongoFilter(query.filter) }] : [{ $match: require_mongo_accumulator.buildMongoFilter(query.filter) }];
|
|
124
108
|
const groupId = {};
|
|
125
109
|
const groupKeys = [];
|
|
126
110
|
for (const [index, key] of groupBy.entries()) {
|
|
127
111
|
const idKey = `k${index}`;
|
|
128
112
|
const bucket = controls.$select?.bucketByAlias(key);
|
|
129
|
-
groupId[idKey] = bucket ? bucketExpression(bucket) :
|
|
113
|
+
groupId[idKey] = bucket ? bucketExpression(bucket) : require_mongo_accumulator.orNull(`$${key}`);
|
|
130
114
|
groupKeys.push([key, idKey]);
|
|
131
115
|
}
|
|
132
116
|
return {
|
|
@@ -163,11 +147,11 @@ function buildGroupedStages(query, { accumulators, searchStage }) {
|
|
|
163
147
|
for (const expr of controls.$select?.aggregates ?? []) {
|
|
164
148
|
const alias = (0, _atscript_db_agg.resolveAlias)(expr);
|
|
165
149
|
groupStage[alias] = toAccumulator(expr);
|
|
166
|
-
project[alias] = 1;
|
|
150
|
+
project[alias] = expr.$fn === "countDistinct" ? require_mongo_accumulator.distinctCountExpr(`$${alias}`) : 1;
|
|
167
151
|
}
|
|
168
152
|
pipeline.push({ $group: groupStage });
|
|
169
153
|
pipeline.push({ $project: project });
|
|
170
|
-
if (controls.$having) pipeline.push({ $match:
|
|
154
|
+
if (controls.$having) pipeline.push({ $match: require_mongo_accumulator.buildMongoFilter(controls.$having) });
|
|
171
155
|
return {
|
|
172
156
|
pipeline,
|
|
173
157
|
controls
|
package/dist/agg.mjs
CHANGED
|
@@ -1,26 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { i as orNull, n as distinctCountExpr, r as buildMongoFilter, t as buildAccumulator } from "./mongo-accumulator-Dc0w4t0l.mjs";
|
|
2
|
+
import { resolveAlias } from "@atscript/db/agg";
|
|
3
3
|
import { BUCKET_MAX_INSTANT, BUCKET_MIN_INSTANT } from "@uniqu/core";
|
|
4
4
|
//#region src/agg.ts
|
|
5
|
-
/** `$group` accumulator
|
|
6
|
-
const ACCUMULATORS = {
|
|
7
|
-
sum: "$sum",
|
|
8
|
-
avg: "$avg",
|
|
9
|
-
min: "$min",
|
|
10
|
-
max: "$max"
|
|
11
|
-
};
|
|
12
|
-
/**
|
|
13
|
-
* Maps an AggregateExpr to a MongoDB $group accumulator expression.
|
|
14
|
-
*/
|
|
5
|
+
/** Maps an AggregateExpr to its MongoDB `$group` accumulator (see `buildAccumulator`). */
|
|
15
6
|
function toAccumulator(expr) {
|
|
16
|
-
|
|
17
|
-
if (expr.$fn !== "count") return { [ACCUMULATORS[expr.$fn]]: `$${expr.$field}` };
|
|
18
|
-
if (expr.$field === "*") return { $sum: 1 };
|
|
19
|
-
return { $sum: { $cond: [
|
|
20
|
-
{ $ne: [`$${expr.$field}`, null] },
|
|
21
|
-
1,
|
|
22
|
-
0
|
|
23
|
-
] } };
|
|
7
|
+
return buildAccumulator(expr.$fn, expr.$field === "*" ? "*" : `$${expr.$field}`);
|
|
24
8
|
}
|
|
25
9
|
const DAY_MS = 864e5;
|
|
26
10
|
const ISO_DATE = "%Y-%m-%d";
|
|
@@ -125,7 +109,7 @@ function buildPrefix(query, searchStage) {
|
|
|
125
109
|
for (const [index, key] of groupBy.entries()) {
|
|
126
110
|
const idKey = `k${index}`;
|
|
127
111
|
const bucket = controls.$select?.bucketByAlias(key);
|
|
128
|
-
groupId[idKey] = bucket ? bucketExpression(bucket) :
|
|
112
|
+
groupId[idKey] = bucket ? bucketExpression(bucket) : orNull(`$${key}`);
|
|
129
113
|
groupKeys.push([key, idKey]);
|
|
130
114
|
}
|
|
131
115
|
return {
|
|
@@ -162,7 +146,7 @@ function buildGroupedStages(query, { accumulators, searchStage }) {
|
|
|
162
146
|
for (const expr of controls.$select?.aggregates ?? []) {
|
|
163
147
|
const alias = resolveAlias(expr);
|
|
164
148
|
groupStage[alias] = toAccumulator(expr);
|
|
165
|
-
project[alias] = 1;
|
|
149
|
+
project[alias] = expr.$fn === "countDistinct" ? distinctCountExpr(`$${alias}`) : 1;
|
|
166
150
|
}
|
|
167
151
|
pipeline.push({ $group: groupStage });
|
|
168
152
|
pipeline.push({ $project: project });
|