@atscript/db-mongo 0.1.166 → 0.1.168
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 +74 -58
- package/dist/agg.d.cts +6 -2
- package/dist/agg.d.mts +6 -2
- package/dist/agg.mjs +76 -60
- package/dist/index.cjs +52 -7
- package/dist/index.d.cts +17 -0
- package/dist/index.d.mts +17 -0
- package/dist/index.mjs +52 -7
- package/dist/{mongo-accumulator-BW-RNFg0.cjs → mongo-accumulator-BOwAFmrt.cjs} +196 -41
- package/dist/{mongo-accumulator-DMPFOscc.mjs → mongo-accumulator-D4drEz0-.mjs} +191 -36
- package/package.json +9 -9
|
@@ -127,6 +127,181 @@ function exprToMongo(node, operand) {
|
|
|
127
127
|
default: return args.reduceRight((rest, arg) => ({ $ifNull: [arg, rest] }));
|
|
128
128
|
}
|
|
129
129
|
}
|
|
130
|
+
const TRUE_COND = { $literal: true };
|
|
131
|
+
const FALSE_COND = { $literal: false };
|
|
132
|
+
/**
|
|
133
|
+
* A condition's value inside an expression: a `$`-string would read as a
|
|
134
|
+
* field path and an object as an expression object — both stay literals.
|
|
135
|
+
*/
|
|
136
|
+
function valueOf(value) {
|
|
137
|
+
return value !== null && typeof value === "object" && !(value instanceof Date) ? { $literal: value } : literal(value);
|
|
138
|
+
}
|
|
139
|
+
const not = (cond) => ({ $not: [cond] });
|
|
140
|
+
const allOf = (items) => items.length === 0 ? TRUE_COND : items.length === 1 ? items[0] : { $and: items };
|
|
141
|
+
const anyOf = (items) => items.length === 0 ? FALSE_COND : items.length === 1 ? items[0] : { $or: items };
|
|
142
|
+
/** `test(x)` for a scalar `x`; for an array, true when ANY element passes (the query filter's array reading). */
|
|
143
|
+
function eachValue(x, test) {
|
|
144
|
+
return { $cond: [
|
|
145
|
+
{ $isArray: x },
|
|
146
|
+
{ $anyElementTrue: [{ $map: {
|
|
147
|
+
input: x,
|
|
148
|
+
as: "v",
|
|
149
|
+
in: test("$$v")
|
|
150
|
+
} }] },
|
|
151
|
+
test(x)
|
|
152
|
+
] };
|
|
153
|
+
}
|
|
154
|
+
/** A `$regex` value — a RegExp, a `/pattern/flags` string or a bare pattern — as pattern + flags. */
|
|
155
|
+
function parseRegexString(value) {
|
|
156
|
+
if (value instanceof RegExp) return {
|
|
157
|
+
pattern: value.source,
|
|
158
|
+
flags: value.flags
|
|
159
|
+
};
|
|
160
|
+
const str = String(value);
|
|
161
|
+
const match = str.match(/^\/(.+)\/([gimsuy]*)$/);
|
|
162
|
+
if (match) return {
|
|
163
|
+
pattern: match[1],
|
|
164
|
+
flags: match[2]
|
|
165
|
+
};
|
|
166
|
+
return {
|
|
167
|
+
pattern: str,
|
|
168
|
+
flags: ""
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
/** `$regexMatch` options: the PCRE flags it accepts (`g` / `u` / `y` mean nothing there). */
|
|
172
|
+
function regexParts(value) {
|
|
173
|
+
const { pattern, flags } = parseRegexString(value);
|
|
174
|
+
const options = flags.replace(/[^imsx]/g, "");
|
|
175
|
+
return options ? {
|
|
176
|
+
regex: pattern,
|
|
177
|
+
options
|
|
178
|
+
} : { regex: pattern };
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* A pattern over the DECIMAL TEXT of an integer operand (`INTEGER_REGEX_OP`)
|
|
182
|
+
* — the query filter's and a `$where` condition's rendering. The inner
|
|
183
|
+
* `$convert` to long is required (a double prints as `1e+16`); a
|
|
184
|
+
* non-numeric / missing value converts to null → `$regexMatch` is false.
|
|
185
|
+
*/
|
|
186
|
+
function integerRegexExpr(input, value) {
|
|
187
|
+
return { $regexMatch: {
|
|
188
|
+
input: { $toString: { $convert: {
|
|
189
|
+
input,
|
|
190
|
+
to: "long",
|
|
191
|
+
onError: null,
|
|
192
|
+
onNull: null
|
|
193
|
+
} } },
|
|
194
|
+
...regexParts(value)
|
|
195
|
+
} };
|
|
196
|
+
}
|
|
197
|
+
/** A pattern match: a string value (or an array holding one) that matches; any other value does not. */
|
|
198
|
+
function regexCond(x, value) {
|
|
199
|
+
const parts = regexParts(value);
|
|
200
|
+
return eachValue(x, (v) => ({ $cond: [
|
|
201
|
+
{ $eq: [{ $type: v }, "string"] },
|
|
202
|
+
{ $regexMatch: {
|
|
203
|
+
input: v,
|
|
204
|
+
...parts
|
|
205
|
+
} },
|
|
206
|
+
false
|
|
207
|
+
] }));
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* `x = value` as the query filter reads it: `null` matches null, missing and
|
|
211
|
+
* an array holding null; a RegExp matches like `$regex`; any other value
|
|
212
|
+
* matches an equal value or an array holding one.
|
|
213
|
+
*/
|
|
214
|
+
function eqCond(x, value) {
|
|
215
|
+
if (value === null || value === void 0) return { $cond: [
|
|
216
|
+
{ $isArray: x },
|
|
217
|
+
{ $in: [null, x] },
|
|
218
|
+
isNullExpr(x)
|
|
219
|
+
] };
|
|
220
|
+
if (value instanceof RegExp) return regexCond(x, value);
|
|
221
|
+
const v = valueOf(value);
|
|
222
|
+
if (Array.isArray(value)) return { $eq: [x, v] };
|
|
223
|
+
return { $cond: [
|
|
224
|
+
{ $isArray: x },
|
|
225
|
+
{ $in: [v, x] },
|
|
226
|
+
{ $eq: [x, v] }
|
|
227
|
+
] };
|
|
228
|
+
}
|
|
229
|
+
/** The comparison-class test of a range operand: only values of the operand's BSON class compare. */
|
|
230
|
+
function sameClass(v, value) {
|
|
231
|
+
if (typeof value === "number" || typeof value === "bigint") return { $isNumber: v };
|
|
232
|
+
return { $eq: [{ $type: v }, { $type: valueOf(value) }] };
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* `x <op> value` for `$gt` / `$gte` / `$lt` / `$lte` as the query filter reads
|
|
236
|
+
* it: only a value of the operand's type class compares (no cross-type order),
|
|
237
|
+
* an array matches when an element does; against `null`, `$gte` / `$lte` are
|
|
238
|
+
* `= null` and `$gt` / `$lt` match nothing.
|
|
239
|
+
*/
|
|
240
|
+
function rangeCond(x, op, value) {
|
|
241
|
+
if (value === null || value === void 0) return op === "$gte" || op === "$lte" ? eqCond(x, null) : FALSE_COND;
|
|
242
|
+
const v = valueOf(value);
|
|
243
|
+
return eachValue(x, (el) => ({ $and: [sameClass(el, value), { [op]: [el, v] }] }));
|
|
244
|
+
}
|
|
245
|
+
function comparisonCond(x, op, value) {
|
|
246
|
+
switch (op) {
|
|
247
|
+
case "$eq": return eqCond(x, value);
|
|
248
|
+
case "$ne": return not(eqCond(x, value));
|
|
249
|
+
case "$exists": return value ? not(eqCond(x, null)) : eqCond(x, null);
|
|
250
|
+
case "$in":
|
|
251
|
+
case "$nin": {
|
|
252
|
+
const any = anyOf((Array.isArray(value) ? value : [value]).map((v) => eqCond(x, v)));
|
|
253
|
+
return op === "$in" ? any : not(any);
|
|
254
|
+
}
|
|
255
|
+
case "$regex": return regexCond(x, value);
|
|
256
|
+
case INTEGER_REGEX_OP: return integerRegexExpr(x, value);
|
|
257
|
+
case "$gt":
|
|
258
|
+
case "$gte":
|
|
259
|
+
case "$lt":
|
|
260
|
+
case "$lte": return rangeCond(x, op, value);
|
|
261
|
+
default: throw new DbError("INVALID_QUERY", [{
|
|
262
|
+
path: "$select",
|
|
263
|
+
message: `Operator "${op}" is not supported in a $where condition`
|
|
264
|
+
}]);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
const condVisitor = {
|
|
268
|
+
comparison: (field, op, value) => comparisonCond(`$${field}`, op, value),
|
|
269
|
+
and: (children) => allOf(children),
|
|
270
|
+
or: (children) => anyOf(children),
|
|
271
|
+
not: (child) => not(child),
|
|
272
|
+
relation: (field) => {
|
|
273
|
+
throw new DbError("INVALID_QUERY", [{
|
|
274
|
+
path: field,
|
|
275
|
+
message: "Relational predicates are not supported in a $where condition"
|
|
276
|
+
}]);
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
/**
|
|
280
|
+
* The condition of a conditional aggregate (`$where`: a uniqu filter,
|
|
281
|
+
* translated — physical names, storage values) as an aggregation expression
|
|
282
|
+
* that holds exactly for the documents the same filter returns on this
|
|
283
|
+
* adapter (since 0.1.168) — `aggregate(f, x | w)` aggregates what
|
|
284
|
+
* `aggregate(f ∧ w, x)` does:
|
|
285
|
+
*
|
|
286
|
+
* - `= v` / `$in` match an equal value or an array holding one; `= null`
|
|
287
|
+
* matches null, missing and an array holding null;
|
|
288
|
+
* - `$ne` / `$nin` / `$not` are the plain negation (so they match null and
|
|
289
|
+
* missing values);
|
|
290
|
+
* - `$gt` / `$gte` / `$lt` / `$lte` compare values of the operand's type class
|
|
291
|
+
* only (an array: any element); `$gt` / `$lt` against `null` match nothing;
|
|
292
|
+
* - `$exists` is "holds a value"; `$regex` → `$regexMatch` over string values
|
|
293
|
+
* (MongoDB 4.2+);
|
|
294
|
+
* - an empty `$or` / `$in` is false, an empty `$and` / `$nin` true.
|
|
295
|
+
*
|
|
296
|
+
* Collation: values compare byte-wise — like the filter of a grouped
|
|
297
|
+
* aggregate on this adapter, whose pipeline runs without an operation-wide
|
|
298
|
+
* collation. Never returns a bare boolean (a constant is `{ $literal: … }`).
|
|
299
|
+
*
|
|
300
|
+
* @since 0.1.168
|
|
301
|
+
*/
|
|
302
|
+
function whereToExpr(where) {
|
|
303
|
+
return walkFilter(where, condVisitor) ?? TRUE_COND;
|
|
304
|
+
}
|
|
130
305
|
//#endregion
|
|
131
306
|
//#region src/lib/mongo-sort.ts
|
|
132
307
|
/**
|
|
@@ -247,22 +422,6 @@ function correlate(prefix, pairs) {
|
|
|
247
422
|
//#endregion
|
|
248
423
|
//#region src/lib/mongo-filter.ts
|
|
249
424
|
const EMPTY = {};
|
|
250
|
-
function parseRegexString(value) {
|
|
251
|
-
if (value instanceof RegExp) return {
|
|
252
|
-
pattern: value.source,
|
|
253
|
-
flags: value.flags
|
|
254
|
-
};
|
|
255
|
-
const str = String(value);
|
|
256
|
-
const match = str.match(/^\/(.+)\/([gimsuy]*)$/);
|
|
257
|
-
if (match) return {
|
|
258
|
-
pattern: match[1],
|
|
259
|
-
flags: match[2]
|
|
260
|
-
};
|
|
261
|
-
return {
|
|
262
|
-
pattern: str,
|
|
263
|
-
flags: ""
|
|
264
|
-
};
|
|
265
|
-
}
|
|
266
425
|
/**
|
|
267
426
|
* Earth radius in meters used by MongoDB's `$centerSphere` radians conversion
|
|
268
427
|
* (Mongo documents dividing by 6378.1 km).
|
|
@@ -279,20 +438,7 @@ const mongoVisitor = {
|
|
|
279
438
|
$options: flags
|
|
280
439
|
} } : { [field]: { $regex: pattern } };
|
|
281
440
|
}
|
|
282
|
-
if (op === INTEGER_REGEX_OP) {
|
|
283
|
-
const { pattern, flags } = parseRegexString(value);
|
|
284
|
-
const options = flags.replace(/[^imsx]/g, "");
|
|
285
|
-
return { $expr: { $regexMatch: {
|
|
286
|
-
input: { $toString: { $convert: {
|
|
287
|
-
input: `$${field}`,
|
|
288
|
-
to: "long",
|
|
289
|
-
onError: null,
|
|
290
|
-
onNull: null
|
|
291
|
-
} } },
|
|
292
|
-
regex: pattern,
|
|
293
|
-
...options ? { options } : {}
|
|
294
|
-
} } };
|
|
295
|
-
}
|
|
441
|
+
if (op === INTEGER_REGEX_OP) return { $expr: integerRegexExpr(`$${field}`, value) };
|
|
296
442
|
if (op === "$geoWithin") {
|
|
297
443
|
const { center, radius } = value;
|
|
298
444
|
return { [field]: { $geoWithin: { $centerSphere: [center, radius / EARTH_RADIUS_M] } } };
|
|
@@ -588,7 +734,10 @@ function stagesOf(plan, unset) {
|
|
|
588
734
|
* skip null / missing values);
|
|
589
735
|
* - `countDistinct(f)` → `{ $addToSet: { $ifNull: [f, "$$REMOVE"] } }`, a
|
|
590
736
|
* SET of the non-null values (`$$REMOVE` adds nothing) the caller turns
|
|
591
|
-
* into its size with {@link distinctCountExpr}
|
|
737
|
+
* into its size with {@link distinctCountExpr}; over a field list
|
|
738
|
+
* (`countDistinct` only — the caller rejects any other `fn`), the SET of
|
|
739
|
+
* the rows' value arrays, a row with any null / missing field (or failing
|
|
740
|
+
* `where`) adding nothing.
|
|
592
741
|
*
|
|
593
742
|
* `where` (a conditional aggregate's row predicate, an aggregation
|
|
594
743
|
* expression) swaps the source for `{ $cond: [where, src, null] }` — the
|
|
@@ -596,25 +745,31 @@ function stagesOf(plan, unset) {
|
|
|
596
745
|
* the counts `{ $sum: { $cond: [where (and not null), 1, 0] } }`.
|
|
597
746
|
*
|
|
598
747
|
* @param fn - The aggregate function (re-asserted: `INVALID_QUERY` when unknown).
|
|
599
|
-
* @param src - The source operand (`"$path"`),
|
|
748
|
+
* @param src - The source operand (`"$path"` or an expression), `"*"` for
|
|
749
|
+
* `count(*)`, or a list of `"$path"` operands for `countDistinct`.
|
|
600
750
|
* @param where - Row predicate of a conditional aggregate.
|
|
601
751
|
* @param path - Error path of the re-assertion.
|
|
602
752
|
*/
|
|
603
753
|
function buildAccumulator(fn, src, where, path) {
|
|
604
754
|
assertAggregateFn(fn, path);
|
|
755
|
+
if (Array.isArray(src)) return { $addToSet: { $cond: [
|
|
756
|
+
{ $and: [...src.map((field) => notNullExpr(field)), ...where !== void 0 ? [where] : []] },
|
|
757
|
+
src,
|
|
758
|
+
"$$REMOVE"
|
|
759
|
+
] } };
|
|
605
760
|
if (fn === "count") {
|
|
606
|
-
if (src === "*") return { $sum: where ? { $cond: [
|
|
761
|
+
if (src === "*") return { $sum: where !== void 0 ? { $cond: [
|
|
607
762
|
where,
|
|
608
763
|
1,
|
|
609
764
|
0
|
|
610
765
|
] } : 1 };
|
|
611
766
|
return { $sum: { $cond: [
|
|
612
|
-
where ? { $and: [where, notNullExpr(src)] } : notNullExpr(src),
|
|
767
|
+
where !== void 0 ? { $and: [where, notNullExpr(src)] } : notNullExpr(src),
|
|
613
768
|
1,
|
|
614
769
|
0
|
|
615
770
|
] } };
|
|
616
771
|
}
|
|
617
|
-
const value = where ? { $cond: [
|
|
772
|
+
const value = where !== void 0 ? { $cond: [
|
|
618
773
|
where,
|
|
619
774
|
src,
|
|
620
775
|
null
|
|
@@ -630,4 +785,4 @@ function distinctCountExpr(set) {
|
|
|
630
785
|
return { $size: set };
|
|
631
786
|
}
|
|
632
787
|
//#endregion
|
|
633
|
-
export { buildMongoQuery as a, planStages as c, rowOrderStages as d, sortStages as f,
|
|
788
|
+
export { buildMongoQuery as a, planStages as c, rowOrderStages as d, sortStages as f, whereToExpr as g, queryNodeToExpr as h, buildMongoFilter as i, correlate as l, orNull as m, distinctCountExpr as n, collationOfAdapter as o, exprToMongo as p, REL_FILTER_TEMP_PREFIX as r, mongoFilterStages as s, buildAccumulator as t, hasNullsPlacement as u };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atscript/db-mongo",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.168",
|
|
4
4
|
"description": "Mongodb plugin for atscript.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"atscript",
|
|
@@ -46,19 +46,19 @@
|
|
|
46
46
|
"access": "public"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
|
-
"@atscript/core": "^0.2.
|
|
50
|
-
"@atscript/typescript": "^0.2.
|
|
51
|
-
"@uniqu/core": "^0.
|
|
49
|
+
"@atscript/core": "^0.2.4",
|
|
50
|
+
"@atscript/typescript": "^0.2.4",
|
|
51
|
+
"@uniqu/core": "^0.2.0",
|
|
52
52
|
"mongodb": "^6.17.0",
|
|
53
53
|
"mongodb-memory-server-core": "^10.0.0",
|
|
54
|
-
"unplugin-atscript": "^0.2.
|
|
54
|
+
"unplugin-atscript": "^0.2.4"
|
|
55
55
|
},
|
|
56
56
|
"peerDependencies": {
|
|
57
|
-
"@atscript/core": "^0.2.
|
|
58
|
-
"@atscript/typescript": "^0.2.
|
|
59
|
-
"@uniqu/core": "^0.
|
|
57
|
+
"@atscript/core": "^0.2.4",
|
|
58
|
+
"@atscript/typescript": "^0.2.4",
|
|
59
|
+
"@uniqu/core": "^0.2.0",
|
|
60
60
|
"mongodb": "^6.17.0",
|
|
61
|
-
"@atscript/db": "^0.1.
|
|
61
|
+
"@atscript/db": "^0.1.168"
|
|
62
62
|
},
|
|
63
63
|
"scripts": {
|
|
64
64
|
"build": "vp pack",
|