@atscript/db-mongo 0.1.38
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/LICENSE +21 -0
- package/dist/agg-CUX5Jb_A.mjs +75 -0
- package/dist/agg-FBVtOv9k.cjs +77 -0
- package/dist/agg.cjs +127 -0
- package/dist/agg.d.ts +25 -0
- package/dist/agg.mjs +102 -0
- package/dist/index.cjs +1710 -0
- package/dist/index.d.ts +336 -0
- package/dist/index.mjs +1703 -0
- package/dist/mongo-filter-C8w5by9H.cjs +65 -0
- package/dist/mongo-filter-CL69Yhcm.mjs +30 -0
- package/dist/plugin.cjs +188 -0
- package/dist/plugin.d.ts +5 -0
- package/dist/plugin.mjs +162 -0
- package/package.json +92 -0
- package/scripts/setup-skills.js +78 -0
- package/skills/atscript-db-mongo/.gitkeep +0 -0
- package/skills/atscript-db-mongo/SKILL.md +45 -0
- package/skills/atscript-db-mongo/annotations.md +168 -0
- package/skills/atscript-db-mongo/collections.md +141 -0
- package/skills/atscript-db-mongo/core.md +83 -0
- package/skills/atscript-db-mongo/patches.md +205 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025-present Artem Maltsev
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { buildMongoFilter } from "./mongo-filter-CL69Yhcm.mjs";
|
|
2
|
+
import { resolveAlias } from "@atscript/db/agg";
|
|
3
|
+
|
|
4
|
+
//#region packages/db-mongo/src/agg.ts
|
|
5
|
+
/** Simple accumulators that map directly to `{ $<fn>: '$field' }`. */ const SIMPLE_ACCUMULATORS = {
|
|
6
|
+
sum: "$sum",
|
|
7
|
+
avg: "$avg",
|
|
8
|
+
min: "$min",
|
|
9
|
+
max: "$max"
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Maps an AggregateExpr to a MongoDB $group accumulator expression.
|
|
13
|
+
*/ function toAccumulator(expr) {
|
|
14
|
+
const simple = SIMPLE_ACCUMULATORS[expr.$fn];
|
|
15
|
+
if (simple) return { [simple]: `$${expr.$field}` };
|
|
16
|
+
if (expr.$fn === "count") {
|
|
17
|
+
if (expr.$field === "*") return { $sum: 1 };
|
|
18
|
+
return { $sum: { $cond: [
|
|
19
|
+
{ $ne: [`$${expr.$field}`, null] },
|
|
20
|
+
1,
|
|
21
|
+
0
|
|
22
|
+
] } };
|
|
23
|
+
}
|
|
24
|
+
throw new Error(`Unsupported aggregate function: ${expr.$fn}`);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Builds the common prefix stages: $match + $group._id from groupBy fields.
|
|
28
|
+
* Shared by both full aggregate and count pipelines.
|
|
29
|
+
*/ function buildPrefix(query) {
|
|
30
|
+
const controls = query.controls || {};
|
|
31
|
+
const groupBy = controls.$groupBy ?? [];
|
|
32
|
+
const pipeline = [{ $match: buildMongoFilter(query.filter) }];
|
|
33
|
+
const groupId = {};
|
|
34
|
+
for (const field of groupBy) groupId[field] = `$${field}`;
|
|
35
|
+
return {
|
|
36
|
+
pipeline,
|
|
37
|
+
groupId,
|
|
38
|
+
groupBy,
|
|
39
|
+
controls
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
function buildAggregatePipeline(query) {
|
|
43
|
+
const { pipeline, groupId, groupBy, controls } = buildPrefix(query);
|
|
44
|
+
const groupStage = { _id: groupId };
|
|
45
|
+
const project = { _id: 0 };
|
|
46
|
+
const aggregates = controls.$select?.aggregates;
|
|
47
|
+
for (const field of groupBy) project[field] = `$_id.${field}`;
|
|
48
|
+
if (aggregates) for (const expr of aggregates) {
|
|
49
|
+
const alias = resolveAlias(expr);
|
|
50
|
+
groupStage[alias] = toAccumulator(expr);
|
|
51
|
+
project[alias] = 1;
|
|
52
|
+
}
|
|
53
|
+
pipeline.push({ $group: groupStage });
|
|
54
|
+
pipeline.push({ $project: project });
|
|
55
|
+
if (controls.$having) pipeline.push({ $match: buildMongoFilter(controls.$having) });
|
|
56
|
+
if (controls.$sort) pipeline.push({ $sort: controls.$sort });
|
|
57
|
+
if (controls.$skip) pipeline.push({ $skip: controls.$skip });
|
|
58
|
+
if (controls.$limit) pipeline.push({ $limit: controls.$limit });
|
|
59
|
+
return pipeline;
|
|
60
|
+
}
|
|
61
|
+
function buildCountPipeline(query) {
|
|
62
|
+
const { pipeline, groupId, groupBy, controls } = buildPrefix(query);
|
|
63
|
+
pipeline.push({ $group: { _id: groupId } });
|
|
64
|
+
if (controls.$having) {
|
|
65
|
+
const project = { _id: 0 };
|
|
66
|
+
for (const field of groupBy) project[field] = `$_id.${field}`;
|
|
67
|
+
pipeline.push({ $project: project });
|
|
68
|
+
pipeline.push({ $match: buildMongoFilter(controls.$having) });
|
|
69
|
+
}
|
|
70
|
+
pipeline.push({ $count: "count" });
|
|
71
|
+
return pipeline;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
//#endregion
|
|
75
|
+
export { buildAggregatePipeline, buildCountPipeline };
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const require_mongo_filter = require('./mongo-filter-C8w5by9H.cjs');
|
|
3
|
+
const __atscript_db_agg = require_mongo_filter.__toESM(require("@atscript/db/agg"));
|
|
4
|
+
|
|
5
|
+
//#region packages/db-mongo/src/agg.ts
|
|
6
|
+
/** Simple accumulators that map directly to `{ $<fn>: '$field' }`. */ const SIMPLE_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
|
+
*/ function toAccumulator(expr) {
|
|
15
|
+
const simple = SIMPLE_ACCUMULATORS[expr.$fn];
|
|
16
|
+
if (simple) return { [simple]: `$${expr.$field}` };
|
|
17
|
+
if (expr.$fn === "count") {
|
|
18
|
+
if (expr.$field === "*") return { $sum: 1 };
|
|
19
|
+
return { $sum: { $cond: [
|
|
20
|
+
{ $ne: [`$${expr.$field}`, null] },
|
|
21
|
+
1,
|
|
22
|
+
0
|
|
23
|
+
] } };
|
|
24
|
+
}
|
|
25
|
+
throw new Error(`Unsupported aggregate function: ${expr.$fn}`);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Builds the common prefix stages: $match + $group._id from groupBy fields.
|
|
29
|
+
* Shared by both full aggregate and count pipelines.
|
|
30
|
+
*/ function buildPrefix(query) {
|
|
31
|
+
const controls = query.controls || {};
|
|
32
|
+
const groupBy = controls.$groupBy ?? [];
|
|
33
|
+
const pipeline = [{ $match: require_mongo_filter.buildMongoFilter(query.filter) }];
|
|
34
|
+
const groupId = {};
|
|
35
|
+
for (const field of groupBy) groupId[field] = `$${field}`;
|
|
36
|
+
return {
|
|
37
|
+
pipeline,
|
|
38
|
+
groupId,
|
|
39
|
+
groupBy,
|
|
40
|
+
controls
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
function buildAggregatePipeline(query) {
|
|
44
|
+
const { pipeline, groupId, groupBy, controls } = buildPrefix(query);
|
|
45
|
+
const groupStage = { _id: groupId };
|
|
46
|
+
const project = { _id: 0 };
|
|
47
|
+
const aggregates = controls.$select?.aggregates;
|
|
48
|
+
for (const field of groupBy) project[field] = `$_id.${field}`;
|
|
49
|
+
if (aggregates) for (const expr of aggregates) {
|
|
50
|
+
const alias = (0, __atscript_db_agg.resolveAlias)(expr);
|
|
51
|
+
groupStage[alias] = toAccumulator(expr);
|
|
52
|
+
project[alias] = 1;
|
|
53
|
+
}
|
|
54
|
+
pipeline.push({ $group: groupStage });
|
|
55
|
+
pipeline.push({ $project: project });
|
|
56
|
+
if (controls.$having) pipeline.push({ $match: require_mongo_filter.buildMongoFilter(controls.$having) });
|
|
57
|
+
if (controls.$sort) pipeline.push({ $sort: controls.$sort });
|
|
58
|
+
if (controls.$skip) pipeline.push({ $skip: controls.$skip });
|
|
59
|
+
if (controls.$limit) pipeline.push({ $limit: controls.$limit });
|
|
60
|
+
return pipeline;
|
|
61
|
+
}
|
|
62
|
+
function buildCountPipeline(query) {
|
|
63
|
+
const { pipeline, groupId, groupBy, controls } = buildPrefix(query);
|
|
64
|
+
pipeline.push({ $group: { _id: groupId } });
|
|
65
|
+
if (controls.$having) {
|
|
66
|
+
const project = { _id: 0 };
|
|
67
|
+
for (const field of groupBy) project[field] = `$_id.${field}`;
|
|
68
|
+
pipeline.push({ $project: project });
|
|
69
|
+
pipeline.push({ $match: require_mongo_filter.buildMongoFilter(controls.$having) });
|
|
70
|
+
}
|
|
71
|
+
pipeline.push({ $count: "count" });
|
|
72
|
+
return pipeline;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
//#endregion
|
|
76
|
+
exports.buildAggregatePipeline = buildAggregatePipeline
|
|
77
|
+
exports.buildCountPipeline = buildCountPipeline
|
package/dist/agg.cjs
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
//#region rolldown:runtime
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
13
|
+
get: ((k) => from[k]).bind(null, key),
|
|
14
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
20
|
+
value: mod,
|
|
21
|
+
enumerable: true
|
|
22
|
+
}) : target, mod));
|
|
23
|
+
|
|
24
|
+
//#endregion
|
|
25
|
+
const __atscript_db_agg = __toESM(require("@atscript/db/agg"));
|
|
26
|
+
const __atscript_db = __toESM(require("@atscript/db"));
|
|
27
|
+
|
|
28
|
+
//#region packages/db-mongo/src/lib/mongo-filter.ts
|
|
29
|
+
const EMPTY = {};
|
|
30
|
+
const mongoVisitor = {
|
|
31
|
+
comparison(field, op, value) {
|
|
32
|
+
if (op === "$eq") return { [field]: value };
|
|
33
|
+
return { [field]: { [op]: value } };
|
|
34
|
+
},
|
|
35
|
+
and(children) {
|
|
36
|
+
if (children.length === 0) return EMPTY;
|
|
37
|
+
if (children.length === 1) return children[0];
|
|
38
|
+
return { $and: children };
|
|
39
|
+
},
|
|
40
|
+
or(children) {
|
|
41
|
+
if (children.length === 0) return { _impossible: true };
|
|
42
|
+
if (children.length === 1) return children[0];
|
|
43
|
+
return { $or: children };
|
|
44
|
+
},
|
|
45
|
+
not(child) {
|
|
46
|
+
return { $nor: [child] };
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
function buildMongoFilter(filter) {
|
|
50
|
+
if (!filter || Object.keys(filter).length === 0) return EMPTY;
|
|
51
|
+
return (0, __atscript_db.walkFilter)(filter, mongoVisitor) ?? EMPTY;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
//#endregion
|
|
55
|
+
//#region packages/db-mongo/src/agg.ts
|
|
56
|
+
/** Simple accumulators that map directly to `{ $<fn>: '$field' }`. */ const SIMPLE_ACCUMULATORS = {
|
|
57
|
+
sum: "$sum",
|
|
58
|
+
avg: "$avg",
|
|
59
|
+
min: "$min",
|
|
60
|
+
max: "$max"
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Maps an AggregateExpr to a MongoDB $group accumulator expression.
|
|
64
|
+
*/ function toAccumulator(expr) {
|
|
65
|
+
const simple = SIMPLE_ACCUMULATORS[expr.$fn];
|
|
66
|
+
if (simple) return { [simple]: `$${expr.$field}` };
|
|
67
|
+
if (expr.$fn === "count") {
|
|
68
|
+
if (expr.$field === "*") return { $sum: 1 };
|
|
69
|
+
return { $sum: { $cond: [
|
|
70
|
+
{ $ne: [`$${expr.$field}`, null] },
|
|
71
|
+
1,
|
|
72
|
+
0
|
|
73
|
+
] } };
|
|
74
|
+
}
|
|
75
|
+
throw new Error(`Unsupported aggregate function: ${expr.$fn}`);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Builds the common prefix stages: $match + $group._id from groupBy fields.
|
|
79
|
+
* Shared by both full aggregate and count pipelines.
|
|
80
|
+
*/ function buildPrefix(query) {
|
|
81
|
+
const controls = query.controls || {};
|
|
82
|
+
const groupBy = controls.$groupBy ?? [];
|
|
83
|
+
const pipeline = [{ $match: buildMongoFilter(query.filter) }];
|
|
84
|
+
const groupId = {};
|
|
85
|
+
for (const field of groupBy) groupId[field] = `$${field}`;
|
|
86
|
+
return {
|
|
87
|
+
pipeline,
|
|
88
|
+
groupId,
|
|
89
|
+
groupBy,
|
|
90
|
+
controls
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
function buildAggregatePipeline(query) {
|
|
94
|
+
const { pipeline, groupId, groupBy, controls } = buildPrefix(query);
|
|
95
|
+
const groupStage = { _id: groupId };
|
|
96
|
+
const project = { _id: 0 };
|
|
97
|
+
const aggregates = controls.$select?.aggregates;
|
|
98
|
+
for (const field of groupBy) project[field] = `$_id.${field}`;
|
|
99
|
+
if (aggregates) for (const expr of aggregates) {
|
|
100
|
+
const alias = (0, __atscript_db_agg.resolveAlias)(expr);
|
|
101
|
+
groupStage[alias] = toAccumulator(expr);
|
|
102
|
+
project[alias] = 1;
|
|
103
|
+
}
|
|
104
|
+
pipeline.push({ $group: groupStage });
|
|
105
|
+
pipeline.push({ $project: project });
|
|
106
|
+
if (controls.$having) pipeline.push({ $match: buildMongoFilter(controls.$having) });
|
|
107
|
+
if (controls.$sort) pipeline.push({ $sort: controls.$sort });
|
|
108
|
+
if (controls.$skip) pipeline.push({ $skip: controls.$skip });
|
|
109
|
+
if (controls.$limit) pipeline.push({ $limit: controls.$limit });
|
|
110
|
+
return pipeline;
|
|
111
|
+
}
|
|
112
|
+
function buildCountPipeline(query) {
|
|
113
|
+
const { pipeline, groupId, groupBy, controls } = buildPrefix(query);
|
|
114
|
+
pipeline.push({ $group: { _id: groupId } });
|
|
115
|
+
if (controls.$having) {
|
|
116
|
+
const project = { _id: 0 };
|
|
117
|
+
for (const field of groupBy) project[field] = `$_id.${field}`;
|
|
118
|
+
pipeline.push({ $project: project });
|
|
119
|
+
pipeline.push({ $match: buildMongoFilter(controls.$having) });
|
|
120
|
+
}
|
|
121
|
+
pipeline.push({ $count: "count" });
|
|
122
|
+
return pipeline;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
//#endregion
|
|
126
|
+
exports.buildAggregatePipeline = buildAggregatePipeline
|
|
127
|
+
exports.buildCountPipeline = buildCountPipeline
|
package/dist/agg.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { DbQuery } from '@atscript/db';
|
|
2
|
+
import { Document } from 'mongodb';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* MongoDB aggregation pipeline builder.
|
|
6
|
+
* Dynamically imported by MongoAdapter.aggregate() on first call.
|
|
7
|
+
*
|
|
8
|
+
* Constructs MongoDB aggregation pipelines from translated DbQuery objects
|
|
9
|
+
* containing $groupBy, $select (with AggregateExpr), $having, $sort, etc.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Builds a full MongoDB aggregation pipeline for GROUP BY queries.
|
|
14
|
+
*
|
|
15
|
+
* Pipeline: $match → $group → $project → $match(having) → $sort → $skip → $limit
|
|
16
|
+
*/
|
|
17
|
+
declare function buildAggregatePipeline(query: DbQuery): Document[];
|
|
18
|
+
/**
|
|
19
|
+
* Builds a count-only pipeline: returns the number of distinct groups.
|
|
20
|
+
*
|
|
21
|
+
* Pipeline: $match → $group (just _id) → $project → $match(having) → $count
|
|
22
|
+
*/
|
|
23
|
+
declare function buildCountPipeline(query: DbQuery): Document[];
|
|
24
|
+
|
|
25
|
+
export { buildAggregatePipeline, buildCountPipeline };
|
package/dist/agg.mjs
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { resolveAlias } from "@atscript/db/agg";
|
|
2
|
+
import { walkFilter } from "@atscript/db";
|
|
3
|
+
|
|
4
|
+
//#region packages/db-mongo/src/lib/mongo-filter.ts
|
|
5
|
+
const EMPTY = {};
|
|
6
|
+
const mongoVisitor = {
|
|
7
|
+
comparison(field, op, value) {
|
|
8
|
+
if (op === "$eq") return { [field]: value };
|
|
9
|
+
return { [field]: { [op]: value } };
|
|
10
|
+
},
|
|
11
|
+
and(children) {
|
|
12
|
+
if (children.length === 0) return EMPTY;
|
|
13
|
+
if (children.length === 1) return children[0];
|
|
14
|
+
return { $and: children };
|
|
15
|
+
},
|
|
16
|
+
or(children) {
|
|
17
|
+
if (children.length === 0) return { _impossible: true };
|
|
18
|
+
if (children.length === 1) return children[0];
|
|
19
|
+
return { $or: children };
|
|
20
|
+
},
|
|
21
|
+
not(child) {
|
|
22
|
+
return { $nor: [child] };
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
function buildMongoFilter(filter) {
|
|
26
|
+
if (!filter || Object.keys(filter).length === 0) return EMPTY;
|
|
27
|
+
return walkFilter(filter, mongoVisitor) ?? EMPTY;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
//#endregion
|
|
31
|
+
//#region packages/db-mongo/src/agg.ts
|
|
32
|
+
/** Simple accumulators that map directly to `{ $<fn>: '$field' }`. */ const SIMPLE_ACCUMULATORS = {
|
|
33
|
+
sum: "$sum",
|
|
34
|
+
avg: "$avg",
|
|
35
|
+
min: "$min",
|
|
36
|
+
max: "$max"
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* Maps an AggregateExpr to a MongoDB $group accumulator expression.
|
|
40
|
+
*/ function toAccumulator(expr) {
|
|
41
|
+
const simple = SIMPLE_ACCUMULATORS[expr.$fn];
|
|
42
|
+
if (simple) return { [simple]: `$${expr.$field}` };
|
|
43
|
+
if (expr.$fn === "count") {
|
|
44
|
+
if (expr.$field === "*") return { $sum: 1 };
|
|
45
|
+
return { $sum: { $cond: [
|
|
46
|
+
{ $ne: [`$${expr.$field}`, null] },
|
|
47
|
+
1,
|
|
48
|
+
0
|
|
49
|
+
] } };
|
|
50
|
+
}
|
|
51
|
+
throw new Error(`Unsupported aggregate function: ${expr.$fn}`);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Builds the common prefix stages: $match + $group._id from groupBy fields.
|
|
55
|
+
* Shared by both full aggregate and count pipelines.
|
|
56
|
+
*/ function buildPrefix(query) {
|
|
57
|
+
const controls = query.controls || {};
|
|
58
|
+
const groupBy = controls.$groupBy ?? [];
|
|
59
|
+
const pipeline = [{ $match: buildMongoFilter(query.filter) }];
|
|
60
|
+
const groupId = {};
|
|
61
|
+
for (const field of groupBy) groupId[field] = `$${field}`;
|
|
62
|
+
return {
|
|
63
|
+
pipeline,
|
|
64
|
+
groupId,
|
|
65
|
+
groupBy,
|
|
66
|
+
controls
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
function buildAggregatePipeline(query) {
|
|
70
|
+
const { pipeline, groupId, groupBy, controls } = buildPrefix(query);
|
|
71
|
+
const groupStage = { _id: groupId };
|
|
72
|
+
const project = { _id: 0 };
|
|
73
|
+
const aggregates = controls.$select?.aggregates;
|
|
74
|
+
for (const field of groupBy) project[field] = `$_id.${field}`;
|
|
75
|
+
if (aggregates) for (const expr of aggregates) {
|
|
76
|
+
const alias = resolveAlias(expr);
|
|
77
|
+
groupStage[alias] = toAccumulator(expr);
|
|
78
|
+
project[alias] = 1;
|
|
79
|
+
}
|
|
80
|
+
pipeline.push({ $group: groupStage });
|
|
81
|
+
pipeline.push({ $project: project });
|
|
82
|
+
if (controls.$having) pipeline.push({ $match: buildMongoFilter(controls.$having) });
|
|
83
|
+
if (controls.$sort) pipeline.push({ $sort: controls.$sort });
|
|
84
|
+
if (controls.$skip) pipeline.push({ $skip: controls.$skip });
|
|
85
|
+
if (controls.$limit) pipeline.push({ $limit: controls.$limit });
|
|
86
|
+
return pipeline;
|
|
87
|
+
}
|
|
88
|
+
function buildCountPipeline(query) {
|
|
89
|
+
const { pipeline, groupId, groupBy, controls } = buildPrefix(query);
|
|
90
|
+
pipeline.push({ $group: { _id: groupId } });
|
|
91
|
+
if (controls.$having) {
|
|
92
|
+
const project = { _id: 0 };
|
|
93
|
+
for (const field of groupBy) project[field] = `$_id.${field}`;
|
|
94
|
+
pipeline.push({ $project: project });
|
|
95
|
+
pipeline.push({ $match: buildMongoFilter(controls.$having) });
|
|
96
|
+
}
|
|
97
|
+
pipeline.push({ $count: "count" });
|
|
98
|
+
return pipeline;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
//#endregion
|
|
102
|
+
export { buildAggregatePipeline, buildCountPipeline };
|