@prisma-next/mongo-query-ast 0.0.1 → 0.3.0-dev.147
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/exports/index.d.mts +763 -0
- package/dist/exports/index.d.mts.map +1 -0
- package/dist/exports/index.mjs +1374 -0
- package/dist/exports/index.mjs.map +1 -0
- package/package.json +5 -5
|
@@ -0,0 +1,1374 @@
|
|
|
1
|
+
//#region src/ast-node.ts
|
|
2
|
+
var MongoAstNode = class {
|
|
3
|
+
freeze() {
|
|
4
|
+
Object.freeze(this);
|
|
5
|
+
}
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
//#endregion
|
|
9
|
+
//#region src/aggregation-expressions.ts
|
|
10
|
+
function isExprArray(args) {
|
|
11
|
+
return Array.isArray(args);
|
|
12
|
+
}
|
|
13
|
+
function isRecordArgs(args) {
|
|
14
|
+
return !Array.isArray(args) && typeof args === "object" && !("accept" in args);
|
|
15
|
+
}
|
|
16
|
+
function freezeRecordArgs(record) {
|
|
17
|
+
const frozen = {};
|
|
18
|
+
for (const [key, val] of Object.entries(record)) frozen[key] = Array.isArray(val) ? Object.freeze([...val]) : val;
|
|
19
|
+
return Object.freeze(frozen);
|
|
20
|
+
}
|
|
21
|
+
function rewriteRecordArgs(record, rewriter) {
|
|
22
|
+
const result = {};
|
|
23
|
+
for (const [key, val] of Object.entries(record)) if (Array.isArray(val)) result[key] = val.map((v) => v.rewrite(rewriter));
|
|
24
|
+
else result[key] = val.rewrite(rewriter);
|
|
25
|
+
return result;
|
|
26
|
+
}
|
|
27
|
+
var MongoAggExprNode = class extends MongoAstNode {};
|
|
28
|
+
var MongoAggFieldRef = class MongoAggFieldRef extends MongoAggExprNode {
|
|
29
|
+
kind = "fieldRef";
|
|
30
|
+
path;
|
|
31
|
+
constructor(path) {
|
|
32
|
+
super();
|
|
33
|
+
if (!path) throw new Error("Field path must not be empty");
|
|
34
|
+
this.path = path;
|
|
35
|
+
this.freeze();
|
|
36
|
+
}
|
|
37
|
+
static of(path) {
|
|
38
|
+
return new MongoAggFieldRef(path);
|
|
39
|
+
}
|
|
40
|
+
accept(visitor) {
|
|
41
|
+
return visitor.fieldRef(this);
|
|
42
|
+
}
|
|
43
|
+
rewrite(rewriter) {
|
|
44
|
+
return rewriter.fieldRef ? rewriter.fieldRef(this) : this;
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
var MongoAggLiteral = class MongoAggLiteral extends MongoAggExprNode {
|
|
48
|
+
kind = "literal";
|
|
49
|
+
value;
|
|
50
|
+
constructor(value) {
|
|
51
|
+
super();
|
|
52
|
+
this.value = value;
|
|
53
|
+
this.freeze();
|
|
54
|
+
}
|
|
55
|
+
static of(value) {
|
|
56
|
+
return new MongoAggLiteral(value);
|
|
57
|
+
}
|
|
58
|
+
accept(visitor) {
|
|
59
|
+
return visitor.literal(this);
|
|
60
|
+
}
|
|
61
|
+
rewrite(rewriter) {
|
|
62
|
+
return rewriter.literal ? rewriter.literal(this) : this;
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
var MongoAggOperator = class MongoAggOperator extends MongoAggExprNode {
|
|
66
|
+
kind = "operator";
|
|
67
|
+
op;
|
|
68
|
+
args;
|
|
69
|
+
constructor(op, args) {
|
|
70
|
+
super();
|
|
71
|
+
this.op = op;
|
|
72
|
+
if (Array.isArray(args)) this.args = Object.freeze([...args]);
|
|
73
|
+
else if (isRecordArgs(args)) this.args = freezeRecordArgs(args);
|
|
74
|
+
else this.args = args;
|
|
75
|
+
this.freeze();
|
|
76
|
+
}
|
|
77
|
+
static of(op, args) {
|
|
78
|
+
return new MongoAggOperator(op, args);
|
|
79
|
+
}
|
|
80
|
+
static add(...args) {
|
|
81
|
+
return new MongoAggOperator("$add", args);
|
|
82
|
+
}
|
|
83
|
+
static subtract(left, right) {
|
|
84
|
+
return new MongoAggOperator("$subtract", [left, right]);
|
|
85
|
+
}
|
|
86
|
+
static multiply(...args) {
|
|
87
|
+
return new MongoAggOperator("$multiply", args);
|
|
88
|
+
}
|
|
89
|
+
static divide(dividend, divisor) {
|
|
90
|
+
return new MongoAggOperator("$divide", [dividend, divisor]);
|
|
91
|
+
}
|
|
92
|
+
static concat(...args) {
|
|
93
|
+
return new MongoAggOperator("$concat", args);
|
|
94
|
+
}
|
|
95
|
+
static toLower(expr) {
|
|
96
|
+
return new MongoAggOperator("$toLower", expr);
|
|
97
|
+
}
|
|
98
|
+
static toUpper(expr) {
|
|
99
|
+
return new MongoAggOperator("$toUpper", expr);
|
|
100
|
+
}
|
|
101
|
+
static size(expr) {
|
|
102
|
+
return new MongoAggOperator("$size", expr);
|
|
103
|
+
}
|
|
104
|
+
accept(visitor) {
|
|
105
|
+
return visitor.operator(this);
|
|
106
|
+
}
|
|
107
|
+
rewrite(rewriter) {
|
|
108
|
+
const { args } = this;
|
|
109
|
+
let rewrittenArgs;
|
|
110
|
+
if (isExprArray(args)) rewrittenArgs = args.map((a) => a.rewrite(rewriter));
|
|
111
|
+
else if (isRecordArgs(args)) rewrittenArgs = rewriteRecordArgs(args, rewriter);
|
|
112
|
+
else rewrittenArgs = args.rewrite(rewriter);
|
|
113
|
+
const rebuilt = new MongoAggOperator(this.op, rewrittenArgs);
|
|
114
|
+
return rewriter.operator ? rewriter.operator(rebuilt) : rebuilt;
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
var MongoAggAccumulator = class MongoAggAccumulator extends MongoAggExprNode {
|
|
118
|
+
kind = "accumulator";
|
|
119
|
+
op;
|
|
120
|
+
arg;
|
|
121
|
+
constructor(op, arg) {
|
|
122
|
+
super();
|
|
123
|
+
this.op = op;
|
|
124
|
+
if (arg !== null && isRecordArgs(arg)) this.arg = freezeRecordArgs(arg);
|
|
125
|
+
else this.arg = arg;
|
|
126
|
+
this.freeze();
|
|
127
|
+
}
|
|
128
|
+
static of(op, arg) {
|
|
129
|
+
return new MongoAggAccumulator(op, arg);
|
|
130
|
+
}
|
|
131
|
+
static sum(expr) {
|
|
132
|
+
return new MongoAggAccumulator("$sum", expr);
|
|
133
|
+
}
|
|
134
|
+
static avg(expr) {
|
|
135
|
+
return new MongoAggAccumulator("$avg", expr);
|
|
136
|
+
}
|
|
137
|
+
static min(expr) {
|
|
138
|
+
return new MongoAggAccumulator("$min", expr);
|
|
139
|
+
}
|
|
140
|
+
static max(expr) {
|
|
141
|
+
return new MongoAggAccumulator("$max", expr);
|
|
142
|
+
}
|
|
143
|
+
static first(expr) {
|
|
144
|
+
return new MongoAggAccumulator("$first", expr);
|
|
145
|
+
}
|
|
146
|
+
static last(expr) {
|
|
147
|
+
return new MongoAggAccumulator("$last", expr);
|
|
148
|
+
}
|
|
149
|
+
static push(expr) {
|
|
150
|
+
return new MongoAggAccumulator("$push", expr);
|
|
151
|
+
}
|
|
152
|
+
static addToSet(expr) {
|
|
153
|
+
return new MongoAggAccumulator("$addToSet", expr);
|
|
154
|
+
}
|
|
155
|
+
static count() {
|
|
156
|
+
return new MongoAggAccumulator("$count", null);
|
|
157
|
+
}
|
|
158
|
+
static stdDevPop(expr) {
|
|
159
|
+
return new MongoAggAccumulator("$stdDevPop", expr);
|
|
160
|
+
}
|
|
161
|
+
static stdDevSamp(expr) {
|
|
162
|
+
return new MongoAggAccumulator("$stdDevSamp", expr);
|
|
163
|
+
}
|
|
164
|
+
accept(visitor) {
|
|
165
|
+
return visitor.accumulator(this);
|
|
166
|
+
}
|
|
167
|
+
rewrite(rewriter) {
|
|
168
|
+
let rewrittenArg;
|
|
169
|
+
if (this.arg === null) rewrittenArg = null;
|
|
170
|
+
else if (isRecordArgs(this.arg)) rewrittenArg = rewriteRecordArgs(this.arg, rewriter);
|
|
171
|
+
else rewrittenArg = this.arg.rewrite(rewriter);
|
|
172
|
+
const rebuilt = new MongoAggAccumulator(this.op, rewrittenArg);
|
|
173
|
+
return rewriter.accumulator ? rewriter.accumulator(rebuilt) : rebuilt;
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
var MongoAggCond = class MongoAggCond extends MongoAggExprNode {
|
|
177
|
+
kind = "cond";
|
|
178
|
+
condition;
|
|
179
|
+
then_;
|
|
180
|
+
else_;
|
|
181
|
+
constructor(condition, then_, else_) {
|
|
182
|
+
super();
|
|
183
|
+
this.condition = condition;
|
|
184
|
+
this.then_ = then_;
|
|
185
|
+
this.else_ = else_;
|
|
186
|
+
this.freeze();
|
|
187
|
+
}
|
|
188
|
+
static of(condition, then_, else_) {
|
|
189
|
+
return new MongoAggCond(condition, then_, else_);
|
|
190
|
+
}
|
|
191
|
+
accept(visitor) {
|
|
192
|
+
return visitor.cond(this);
|
|
193
|
+
}
|
|
194
|
+
rewrite(rewriter) {
|
|
195
|
+
const rebuilt = new MongoAggCond(this.condition.rewrite(rewriter), this.then_.rewrite(rewriter), this.else_.rewrite(rewriter));
|
|
196
|
+
return rewriter.cond ? rewriter.cond(rebuilt) : rebuilt;
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
var MongoAggSwitch = class MongoAggSwitch extends MongoAggExprNode {
|
|
200
|
+
kind = "switch";
|
|
201
|
+
branches;
|
|
202
|
+
default_;
|
|
203
|
+
constructor(branches, default_) {
|
|
204
|
+
super();
|
|
205
|
+
this.branches = Object.freeze(branches.map((b) => Object.freeze({
|
|
206
|
+
case_: b.case_,
|
|
207
|
+
then_: b.then_
|
|
208
|
+
})));
|
|
209
|
+
this.default_ = default_;
|
|
210
|
+
this.freeze();
|
|
211
|
+
}
|
|
212
|
+
static of(branches, default_) {
|
|
213
|
+
return new MongoAggSwitch(branches, default_);
|
|
214
|
+
}
|
|
215
|
+
accept(visitor) {
|
|
216
|
+
return visitor.switch_(this);
|
|
217
|
+
}
|
|
218
|
+
rewrite(rewriter) {
|
|
219
|
+
const rebuilt = new MongoAggSwitch(this.branches.map((b) => ({
|
|
220
|
+
case_: b.case_.rewrite(rewriter),
|
|
221
|
+
then_: b.then_.rewrite(rewriter)
|
|
222
|
+
})), this.default_.rewrite(rewriter));
|
|
223
|
+
return rewriter.switch_ ? rewriter.switch_(rebuilt) : rebuilt;
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
var MongoAggArrayFilter = class MongoAggArrayFilter extends MongoAggExprNode {
|
|
227
|
+
kind = "filter";
|
|
228
|
+
input;
|
|
229
|
+
cond;
|
|
230
|
+
as;
|
|
231
|
+
constructor(input, cond, as) {
|
|
232
|
+
super();
|
|
233
|
+
this.input = input;
|
|
234
|
+
this.cond = cond;
|
|
235
|
+
this.as = as;
|
|
236
|
+
this.freeze();
|
|
237
|
+
}
|
|
238
|
+
static of(input, cond, as) {
|
|
239
|
+
return new MongoAggArrayFilter(input, cond, as);
|
|
240
|
+
}
|
|
241
|
+
accept(visitor) {
|
|
242
|
+
return visitor.filter(this);
|
|
243
|
+
}
|
|
244
|
+
rewrite(rewriter) {
|
|
245
|
+
const rebuilt = new MongoAggArrayFilter(this.input.rewrite(rewriter), this.cond.rewrite(rewriter), this.as);
|
|
246
|
+
return rewriter.filter ? rewriter.filter(rebuilt) : rebuilt;
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
var MongoAggMap = class MongoAggMap extends MongoAggExprNode {
|
|
250
|
+
kind = "map";
|
|
251
|
+
input;
|
|
252
|
+
in_;
|
|
253
|
+
as;
|
|
254
|
+
constructor(input, in_, as) {
|
|
255
|
+
super();
|
|
256
|
+
this.input = input;
|
|
257
|
+
this.in_ = in_;
|
|
258
|
+
this.as = as;
|
|
259
|
+
this.freeze();
|
|
260
|
+
}
|
|
261
|
+
static of(input, in_, as) {
|
|
262
|
+
return new MongoAggMap(input, in_, as);
|
|
263
|
+
}
|
|
264
|
+
accept(visitor) {
|
|
265
|
+
return visitor.map(this);
|
|
266
|
+
}
|
|
267
|
+
rewrite(rewriter) {
|
|
268
|
+
const rebuilt = new MongoAggMap(this.input.rewrite(rewriter), this.in_.rewrite(rewriter), this.as);
|
|
269
|
+
return rewriter.map ? rewriter.map(rebuilt) : rebuilt;
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
var MongoAggReduce = class MongoAggReduce extends MongoAggExprNode {
|
|
273
|
+
kind = "reduce";
|
|
274
|
+
input;
|
|
275
|
+
initialValue;
|
|
276
|
+
in_;
|
|
277
|
+
constructor(input, initialValue, in_) {
|
|
278
|
+
super();
|
|
279
|
+
this.input = input;
|
|
280
|
+
this.initialValue = initialValue;
|
|
281
|
+
this.in_ = in_;
|
|
282
|
+
this.freeze();
|
|
283
|
+
}
|
|
284
|
+
static of(input, initialValue, in_) {
|
|
285
|
+
return new MongoAggReduce(input, initialValue, in_);
|
|
286
|
+
}
|
|
287
|
+
accept(visitor) {
|
|
288
|
+
return visitor.reduce(this);
|
|
289
|
+
}
|
|
290
|
+
rewrite(rewriter) {
|
|
291
|
+
const rebuilt = new MongoAggReduce(this.input.rewrite(rewriter), this.initialValue.rewrite(rewriter), this.in_.rewrite(rewriter));
|
|
292
|
+
return rewriter.reduce ? rewriter.reduce(rebuilt) : rebuilt;
|
|
293
|
+
}
|
|
294
|
+
};
|
|
295
|
+
var MongoAggLet = class MongoAggLet extends MongoAggExprNode {
|
|
296
|
+
kind = "let";
|
|
297
|
+
vars;
|
|
298
|
+
in_;
|
|
299
|
+
constructor(vars, in_) {
|
|
300
|
+
super();
|
|
301
|
+
this.vars = Object.freeze({ ...vars });
|
|
302
|
+
this.in_ = in_;
|
|
303
|
+
this.freeze();
|
|
304
|
+
}
|
|
305
|
+
static of(vars, in_) {
|
|
306
|
+
return new MongoAggLet(vars, in_);
|
|
307
|
+
}
|
|
308
|
+
accept(visitor) {
|
|
309
|
+
return visitor.let_(this);
|
|
310
|
+
}
|
|
311
|
+
rewrite(rewriter) {
|
|
312
|
+
const rewrittenVars = {};
|
|
313
|
+
for (const [key, val] of Object.entries(this.vars)) rewrittenVars[key] = val.rewrite(rewriter);
|
|
314
|
+
const rebuilt = new MongoAggLet(rewrittenVars, this.in_.rewrite(rewriter));
|
|
315
|
+
return rewriter.let_ ? rewriter.let_(rebuilt) : rebuilt;
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
var MongoAggMergeObjects = class MongoAggMergeObjects extends MongoAggExprNode {
|
|
319
|
+
kind = "mergeObjects";
|
|
320
|
+
exprs;
|
|
321
|
+
constructor(exprs) {
|
|
322
|
+
super();
|
|
323
|
+
this.exprs = Object.freeze([...exprs]);
|
|
324
|
+
this.freeze();
|
|
325
|
+
}
|
|
326
|
+
static of(exprs) {
|
|
327
|
+
return new MongoAggMergeObjects(exprs);
|
|
328
|
+
}
|
|
329
|
+
accept(visitor) {
|
|
330
|
+
return visitor.mergeObjects(this);
|
|
331
|
+
}
|
|
332
|
+
rewrite(rewriter) {
|
|
333
|
+
const rebuilt = new MongoAggMergeObjects(this.exprs.map((e) => e.rewrite(rewriter)));
|
|
334
|
+
return rewriter.mergeObjects ? rewriter.mergeObjects(rebuilt) : rebuilt;
|
|
335
|
+
}
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
//#endregion
|
|
339
|
+
//#region src/commands.ts
|
|
340
|
+
var InsertOneCommand = class extends MongoAstNode {
|
|
341
|
+
kind = "insertOne";
|
|
342
|
+
collection;
|
|
343
|
+
document;
|
|
344
|
+
constructor(collection, document) {
|
|
345
|
+
super();
|
|
346
|
+
this.collection = collection;
|
|
347
|
+
this.document = document;
|
|
348
|
+
this.freeze();
|
|
349
|
+
}
|
|
350
|
+
};
|
|
351
|
+
var UpdateOneCommand = class extends MongoAstNode {
|
|
352
|
+
kind = "updateOne";
|
|
353
|
+
collection;
|
|
354
|
+
filter;
|
|
355
|
+
update;
|
|
356
|
+
constructor(collection, filter, update) {
|
|
357
|
+
super();
|
|
358
|
+
this.collection = collection;
|
|
359
|
+
this.filter = filter;
|
|
360
|
+
this.update = update;
|
|
361
|
+
this.freeze();
|
|
362
|
+
}
|
|
363
|
+
};
|
|
364
|
+
var DeleteOneCommand = class extends MongoAstNode {
|
|
365
|
+
kind = "deleteOne";
|
|
366
|
+
collection;
|
|
367
|
+
filter;
|
|
368
|
+
constructor(collection, filter) {
|
|
369
|
+
super();
|
|
370
|
+
this.collection = collection;
|
|
371
|
+
this.filter = filter;
|
|
372
|
+
this.freeze();
|
|
373
|
+
}
|
|
374
|
+
};
|
|
375
|
+
var InsertManyCommand = class extends MongoAstNode {
|
|
376
|
+
kind = "insertMany";
|
|
377
|
+
collection;
|
|
378
|
+
documents;
|
|
379
|
+
constructor(collection, documents) {
|
|
380
|
+
super();
|
|
381
|
+
this.collection = collection;
|
|
382
|
+
this.documents = documents;
|
|
383
|
+
this.freeze();
|
|
384
|
+
}
|
|
385
|
+
};
|
|
386
|
+
var UpdateManyCommand = class extends MongoAstNode {
|
|
387
|
+
kind = "updateMany";
|
|
388
|
+
collection;
|
|
389
|
+
filter;
|
|
390
|
+
update;
|
|
391
|
+
constructor(collection, filter, update) {
|
|
392
|
+
super();
|
|
393
|
+
this.collection = collection;
|
|
394
|
+
this.filter = filter;
|
|
395
|
+
this.update = update;
|
|
396
|
+
this.freeze();
|
|
397
|
+
}
|
|
398
|
+
};
|
|
399
|
+
var DeleteManyCommand = class extends MongoAstNode {
|
|
400
|
+
kind = "deleteMany";
|
|
401
|
+
collection;
|
|
402
|
+
filter;
|
|
403
|
+
constructor(collection, filter) {
|
|
404
|
+
super();
|
|
405
|
+
this.collection = collection;
|
|
406
|
+
this.filter = filter;
|
|
407
|
+
this.freeze();
|
|
408
|
+
}
|
|
409
|
+
};
|
|
410
|
+
var FindOneAndUpdateCommand = class extends MongoAstNode {
|
|
411
|
+
kind = "findOneAndUpdate";
|
|
412
|
+
collection;
|
|
413
|
+
filter;
|
|
414
|
+
update;
|
|
415
|
+
upsert;
|
|
416
|
+
constructor(collection, filter, update, upsert) {
|
|
417
|
+
super();
|
|
418
|
+
this.collection = collection;
|
|
419
|
+
this.filter = filter;
|
|
420
|
+
this.update = update;
|
|
421
|
+
this.upsert = upsert;
|
|
422
|
+
this.freeze();
|
|
423
|
+
}
|
|
424
|
+
};
|
|
425
|
+
var FindOneAndDeleteCommand = class extends MongoAstNode {
|
|
426
|
+
kind = "findOneAndDelete";
|
|
427
|
+
collection;
|
|
428
|
+
filter;
|
|
429
|
+
constructor(collection, filter) {
|
|
430
|
+
super();
|
|
431
|
+
this.collection = collection;
|
|
432
|
+
this.filter = filter;
|
|
433
|
+
this.freeze();
|
|
434
|
+
}
|
|
435
|
+
};
|
|
436
|
+
var AggregateCommand = class extends MongoAstNode {
|
|
437
|
+
kind = "aggregate";
|
|
438
|
+
collection;
|
|
439
|
+
pipeline;
|
|
440
|
+
constructor(collection, pipeline) {
|
|
441
|
+
super();
|
|
442
|
+
this.collection = collection;
|
|
443
|
+
this.pipeline = pipeline;
|
|
444
|
+
this.freeze();
|
|
445
|
+
}
|
|
446
|
+
};
|
|
447
|
+
|
|
448
|
+
//#endregion
|
|
449
|
+
//#region src/filter-expressions.ts
|
|
450
|
+
var MongoFilterExpression = class extends MongoAstNode {
|
|
451
|
+
not() {
|
|
452
|
+
return new MongoNotExpr(this);
|
|
453
|
+
}
|
|
454
|
+
};
|
|
455
|
+
var MongoFieldFilter = class MongoFieldFilter extends MongoFilterExpression {
|
|
456
|
+
kind = "field";
|
|
457
|
+
field;
|
|
458
|
+
op;
|
|
459
|
+
value;
|
|
460
|
+
constructor(field, op, value) {
|
|
461
|
+
super();
|
|
462
|
+
this.field = field;
|
|
463
|
+
this.op = op;
|
|
464
|
+
this.value = value;
|
|
465
|
+
this.freeze();
|
|
466
|
+
}
|
|
467
|
+
static of(field, op, value) {
|
|
468
|
+
return new MongoFieldFilter(field, op, value);
|
|
469
|
+
}
|
|
470
|
+
static eq(field, value) {
|
|
471
|
+
return new MongoFieldFilter(field, "$eq", value);
|
|
472
|
+
}
|
|
473
|
+
static neq(field, value) {
|
|
474
|
+
return new MongoFieldFilter(field, "$ne", value);
|
|
475
|
+
}
|
|
476
|
+
static gt(field, value) {
|
|
477
|
+
return new MongoFieldFilter(field, "$gt", value);
|
|
478
|
+
}
|
|
479
|
+
static lt(field, value) {
|
|
480
|
+
return new MongoFieldFilter(field, "$lt", value);
|
|
481
|
+
}
|
|
482
|
+
static gte(field, value) {
|
|
483
|
+
return new MongoFieldFilter(field, "$gte", value);
|
|
484
|
+
}
|
|
485
|
+
static lte(field, value) {
|
|
486
|
+
return new MongoFieldFilter(field, "$lte", value);
|
|
487
|
+
}
|
|
488
|
+
static in(field, values) {
|
|
489
|
+
return new MongoFieldFilter(field, "$in", values);
|
|
490
|
+
}
|
|
491
|
+
static isNull(field) {
|
|
492
|
+
return new MongoFieldFilter(field, "$eq", null);
|
|
493
|
+
}
|
|
494
|
+
static isNotNull(field) {
|
|
495
|
+
return new MongoFieldFilter(field, "$ne", null);
|
|
496
|
+
}
|
|
497
|
+
accept(visitor) {
|
|
498
|
+
return visitor.field(this);
|
|
499
|
+
}
|
|
500
|
+
rewrite(rewriter) {
|
|
501
|
+
return rewriter.field ? rewriter.field(this) : this;
|
|
502
|
+
}
|
|
503
|
+
};
|
|
504
|
+
var MongoAndExpr = class MongoAndExpr extends MongoFilterExpression {
|
|
505
|
+
kind = "and";
|
|
506
|
+
exprs;
|
|
507
|
+
constructor(exprs) {
|
|
508
|
+
super();
|
|
509
|
+
if (exprs.length === 0) throw new Error("$and requires at least one expression");
|
|
510
|
+
this.exprs = Object.freeze([...exprs]);
|
|
511
|
+
this.freeze();
|
|
512
|
+
}
|
|
513
|
+
static of(exprs) {
|
|
514
|
+
return new MongoAndExpr(exprs);
|
|
515
|
+
}
|
|
516
|
+
accept(visitor) {
|
|
517
|
+
return visitor.and(this);
|
|
518
|
+
}
|
|
519
|
+
rewrite(rewriter) {
|
|
520
|
+
const rewritten = new MongoAndExpr(this.exprs.map((e) => e.rewrite(rewriter)));
|
|
521
|
+
return rewriter.and ? rewriter.and(rewritten) : rewritten;
|
|
522
|
+
}
|
|
523
|
+
};
|
|
524
|
+
var MongoOrExpr = class MongoOrExpr extends MongoFilterExpression {
|
|
525
|
+
kind = "or";
|
|
526
|
+
exprs;
|
|
527
|
+
constructor(exprs) {
|
|
528
|
+
super();
|
|
529
|
+
if (exprs.length === 0) throw new Error("$or requires at least one expression");
|
|
530
|
+
this.exprs = Object.freeze([...exprs]);
|
|
531
|
+
this.freeze();
|
|
532
|
+
}
|
|
533
|
+
static of(exprs) {
|
|
534
|
+
return new MongoOrExpr(exprs);
|
|
535
|
+
}
|
|
536
|
+
accept(visitor) {
|
|
537
|
+
return visitor.or(this);
|
|
538
|
+
}
|
|
539
|
+
rewrite(rewriter) {
|
|
540
|
+
const rewritten = new MongoOrExpr(this.exprs.map((e) => e.rewrite(rewriter)));
|
|
541
|
+
return rewriter.or ? rewriter.or(rewritten) : rewritten;
|
|
542
|
+
}
|
|
543
|
+
};
|
|
544
|
+
var MongoNotExpr = class MongoNotExpr extends MongoFilterExpression {
|
|
545
|
+
kind = "not";
|
|
546
|
+
expr;
|
|
547
|
+
constructor(expr) {
|
|
548
|
+
super();
|
|
549
|
+
this.expr = expr;
|
|
550
|
+
this.freeze();
|
|
551
|
+
}
|
|
552
|
+
accept(visitor) {
|
|
553
|
+
return visitor.not(this);
|
|
554
|
+
}
|
|
555
|
+
rewrite(rewriter) {
|
|
556
|
+
const rewritten = new MongoNotExpr(this.expr.rewrite(rewriter));
|
|
557
|
+
return rewriter.not ? rewriter.not(rewritten) : rewritten;
|
|
558
|
+
}
|
|
559
|
+
};
|
|
560
|
+
var MongoExistsExpr = class MongoExistsExpr extends MongoFilterExpression {
|
|
561
|
+
kind = "exists";
|
|
562
|
+
field;
|
|
563
|
+
exists;
|
|
564
|
+
constructor(field, exists) {
|
|
565
|
+
super();
|
|
566
|
+
this.field = field;
|
|
567
|
+
this.exists = exists;
|
|
568
|
+
this.freeze();
|
|
569
|
+
}
|
|
570
|
+
static exists(field) {
|
|
571
|
+
return new MongoExistsExpr(field, true);
|
|
572
|
+
}
|
|
573
|
+
static notExists(field) {
|
|
574
|
+
return new MongoExistsExpr(field, false);
|
|
575
|
+
}
|
|
576
|
+
accept(visitor) {
|
|
577
|
+
return visitor.exists(this);
|
|
578
|
+
}
|
|
579
|
+
rewrite(rewriter) {
|
|
580
|
+
return rewriter.exists ? rewriter.exists(this) : this;
|
|
581
|
+
}
|
|
582
|
+
};
|
|
583
|
+
var MongoExprFilter = class MongoExprFilter extends MongoFilterExpression {
|
|
584
|
+
kind = "expr";
|
|
585
|
+
aggExpr;
|
|
586
|
+
constructor(aggExpr) {
|
|
587
|
+
super();
|
|
588
|
+
this.aggExpr = aggExpr;
|
|
589
|
+
this.freeze();
|
|
590
|
+
}
|
|
591
|
+
static of(aggExpr) {
|
|
592
|
+
return new MongoExprFilter(aggExpr);
|
|
593
|
+
}
|
|
594
|
+
accept(visitor) {
|
|
595
|
+
return visitor.expr(this);
|
|
596
|
+
}
|
|
597
|
+
rewrite(rewriter) {
|
|
598
|
+
return rewriter.expr ? rewriter.expr(this) : this;
|
|
599
|
+
}
|
|
600
|
+
};
|
|
601
|
+
|
|
602
|
+
//#endregion
|
|
603
|
+
//#region src/raw-commands.ts
|
|
604
|
+
var RawAggregateCommand = class extends MongoAstNode {
|
|
605
|
+
kind = "rawAggregate";
|
|
606
|
+
collection;
|
|
607
|
+
pipeline;
|
|
608
|
+
constructor(collection, pipeline) {
|
|
609
|
+
super();
|
|
610
|
+
this.collection = collection;
|
|
611
|
+
this.pipeline = pipeline;
|
|
612
|
+
this.freeze();
|
|
613
|
+
}
|
|
614
|
+
};
|
|
615
|
+
var RawInsertOneCommand = class extends MongoAstNode {
|
|
616
|
+
kind = "rawInsertOne";
|
|
617
|
+
collection;
|
|
618
|
+
document;
|
|
619
|
+
constructor(collection, document) {
|
|
620
|
+
super();
|
|
621
|
+
this.collection = collection;
|
|
622
|
+
this.document = document;
|
|
623
|
+
this.freeze();
|
|
624
|
+
}
|
|
625
|
+
};
|
|
626
|
+
var RawInsertManyCommand = class extends MongoAstNode {
|
|
627
|
+
kind = "rawInsertMany";
|
|
628
|
+
collection;
|
|
629
|
+
documents;
|
|
630
|
+
constructor(collection, documents) {
|
|
631
|
+
super();
|
|
632
|
+
this.collection = collection;
|
|
633
|
+
this.documents = documents;
|
|
634
|
+
this.freeze();
|
|
635
|
+
}
|
|
636
|
+
};
|
|
637
|
+
var RawUpdateOneCommand = class extends MongoAstNode {
|
|
638
|
+
kind = "rawUpdateOne";
|
|
639
|
+
collection;
|
|
640
|
+
filter;
|
|
641
|
+
update;
|
|
642
|
+
constructor(collection, filter, update) {
|
|
643
|
+
super();
|
|
644
|
+
this.collection = collection;
|
|
645
|
+
this.filter = filter;
|
|
646
|
+
this.update = update;
|
|
647
|
+
this.freeze();
|
|
648
|
+
}
|
|
649
|
+
};
|
|
650
|
+
var RawUpdateManyCommand = class extends MongoAstNode {
|
|
651
|
+
kind = "rawUpdateMany";
|
|
652
|
+
collection;
|
|
653
|
+
filter;
|
|
654
|
+
update;
|
|
655
|
+
constructor(collection, filter, update) {
|
|
656
|
+
super();
|
|
657
|
+
this.collection = collection;
|
|
658
|
+
this.filter = filter;
|
|
659
|
+
this.update = update;
|
|
660
|
+
this.freeze();
|
|
661
|
+
}
|
|
662
|
+
};
|
|
663
|
+
var RawDeleteOneCommand = class extends MongoAstNode {
|
|
664
|
+
kind = "rawDeleteOne";
|
|
665
|
+
collection;
|
|
666
|
+
filter;
|
|
667
|
+
constructor(collection, filter) {
|
|
668
|
+
super();
|
|
669
|
+
this.collection = collection;
|
|
670
|
+
this.filter = filter;
|
|
671
|
+
this.freeze();
|
|
672
|
+
}
|
|
673
|
+
};
|
|
674
|
+
var RawDeleteManyCommand = class extends MongoAstNode {
|
|
675
|
+
kind = "rawDeleteMany";
|
|
676
|
+
collection;
|
|
677
|
+
filter;
|
|
678
|
+
constructor(collection, filter) {
|
|
679
|
+
super();
|
|
680
|
+
this.collection = collection;
|
|
681
|
+
this.filter = filter;
|
|
682
|
+
this.freeze();
|
|
683
|
+
}
|
|
684
|
+
};
|
|
685
|
+
var RawFindOneAndUpdateCommand = class extends MongoAstNode {
|
|
686
|
+
kind = "rawFindOneAndUpdate";
|
|
687
|
+
collection;
|
|
688
|
+
filter;
|
|
689
|
+
update;
|
|
690
|
+
upsert;
|
|
691
|
+
constructor(collection, filter, update, upsert) {
|
|
692
|
+
super();
|
|
693
|
+
this.collection = collection;
|
|
694
|
+
this.filter = filter;
|
|
695
|
+
this.update = update;
|
|
696
|
+
this.upsert = upsert;
|
|
697
|
+
this.freeze();
|
|
698
|
+
}
|
|
699
|
+
};
|
|
700
|
+
var RawFindOneAndDeleteCommand = class extends MongoAstNode {
|
|
701
|
+
kind = "rawFindOneAndDelete";
|
|
702
|
+
collection;
|
|
703
|
+
filter;
|
|
704
|
+
constructor(collection, filter) {
|
|
705
|
+
super();
|
|
706
|
+
this.collection = collection;
|
|
707
|
+
this.filter = filter;
|
|
708
|
+
this.freeze();
|
|
709
|
+
}
|
|
710
|
+
};
|
|
711
|
+
|
|
712
|
+
//#endregion
|
|
713
|
+
//#region src/stages.ts
|
|
714
|
+
function isAggExpr(value) {
|
|
715
|
+
return typeof value === "object" && value !== null && "kind" in value;
|
|
716
|
+
}
|
|
717
|
+
function isAggExprNode(value) {
|
|
718
|
+
return "accept" in value && typeof value.accept === "function";
|
|
719
|
+
}
|
|
720
|
+
function rewriteGroupId(groupId, rewriter) {
|
|
721
|
+
if (groupId === null) return null;
|
|
722
|
+
if (isAggExprNode(groupId)) return groupId.rewrite(rewriter);
|
|
723
|
+
const result = {};
|
|
724
|
+
for (const [key, val] of Object.entries(groupId)) result[key] = val.rewrite(rewriter);
|
|
725
|
+
return result;
|
|
726
|
+
}
|
|
727
|
+
function rewriteExprRecord(fields, rewriter) {
|
|
728
|
+
const result = {};
|
|
729
|
+
for (const [key, val] of Object.entries(fields)) result[key] = val.rewrite(rewriter);
|
|
730
|
+
return result;
|
|
731
|
+
}
|
|
732
|
+
function rewriteAccumulatorRecord(accumulators, rewriter) {
|
|
733
|
+
const result = {};
|
|
734
|
+
for (const [key, acc] of Object.entries(accumulators)) result[key] = acc.rewrite(rewriter);
|
|
735
|
+
return result;
|
|
736
|
+
}
|
|
737
|
+
var MongoStageNode = class extends MongoAstNode {};
|
|
738
|
+
var MongoMatchStage = class MongoMatchStage extends MongoStageNode {
|
|
739
|
+
kind = "match";
|
|
740
|
+
filter;
|
|
741
|
+
constructor(filter) {
|
|
742
|
+
super();
|
|
743
|
+
this.filter = filter;
|
|
744
|
+
this.freeze();
|
|
745
|
+
}
|
|
746
|
+
accept(visitor) {
|
|
747
|
+
return visitor.match(this);
|
|
748
|
+
}
|
|
749
|
+
rewrite(context) {
|
|
750
|
+
return new MongoMatchStage(this.filter.rewrite(context.filter ?? {}));
|
|
751
|
+
}
|
|
752
|
+
};
|
|
753
|
+
var MongoProjectStage = class MongoProjectStage extends MongoStageNode {
|
|
754
|
+
kind = "project";
|
|
755
|
+
projection;
|
|
756
|
+
constructor(projection) {
|
|
757
|
+
super();
|
|
758
|
+
this.projection = Object.freeze({ ...projection });
|
|
759
|
+
this.freeze();
|
|
760
|
+
}
|
|
761
|
+
accept(visitor) {
|
|
762
|
+
return visitor.project(this);
|
|
763
|
+
}
|
|
764
|
+
rewrite(context) {
|
|
765
|
+
const rewriter = context.aggExpr;
|
|
766
|
+
if (!rewriter) return this;
|
|
767
|
+
let hasExpr = false;
|
|
768
|
+
for (const val of Object.values(this.projection)) if (isAggExpr(val)) {
|
|
769
|
+
hasExpr = true;
|
|
770
|
+
break;
|
|
771
|
+
}
|
|
772
|
+
if (!hasExpr) return this;
|
|
773
|
+
const newProjection = {};
|
|
774
|
+
for (const [key, val] of Object.entries(this.projection)) newProjection[key] = isAggExpr(val) ? val.rewrite(rewriter) : val;
|
|
775
|
+
return new MongoProjectStage(newProjection);
|
|
776
|
+
}
|
|
777
|
+
};
|
|
778
|
+
var MongoSortStage = class extends MongoStageNode {
|
|
779
|
+
kind = "sort";
|
|
780
|
+
sort;
|
|
781
|
+
constructor(sort) {
|
|
782
|
+
super();
|
|
783
|
+
this.sort = Object.freeze({ ...sort });
|
|
784
|
+
this.freeze();
|
|
785
|
+
}
|
|
786
|
+
accept(visitor) {
|
|
787
|
+
return visitor.sort(this);
|
|
788
|
+
}
|
|
789
|
+
rewrite(_context) {
|
|
790
|
+
return this;
|
|
791
|
+
}
|
|
792
|
+
};
|
|
793
|
+
var MongoLimitStage = class extends MongoStageNode {
|
|
794
|
+
kind = "limit";
|
|
795
|
+
limit;
|
|
796
|
+
constructor(limit) {
|
|
797
|
+
super();
|
|
798
|
+
if (!Number.isInteger(limit) || limit < 0) throw new RangeError("limit must be a non-negative integer");
|
|
799
|
+
this.limit = limit;
|
|
800
|
+
this.freeze();
|
|
801
|
+
}
|
|
802
|
+
accept(visitor) {
|
|
803
|
+
return visitor.limit(this);
|
|
804
|
+
}
|
|
805
|
+
rewrite(_context) {
|
|
806
|
+
return this;
|
|
807
|
+
}
|
|
808
|
+
};
|
|
809
|
+
var MongoSkipStage = class extends MongoStageNode {
|
|
810
|
+
kind = "skip";
|
|
811
|
+
skip;
|
|
812
|
+
constructor(skip) {
|
|
813
|
+
super();
|
|
814
|
+
if (!Number.isInteger(skip) || skip < 0) throw new RangeError("skip must be a non-negative integer");
|
|
815
|
+
this.skip = skip;
|
|
816
|
+
this.freeze();
|
|
817
|
+
}
|
|
818
|
+
accept(visitor) {
|
|
819
|
+
return visitor.skip(this);
|
|
820
|
+
}
|
|
821
|
+
rewrite(_context) {
|
|
822
|
+
return this;
|
|
823
|
+
}
|
|
824
|
+
};
|
|
825
|
+
var MongoLookupStage = class MongoLookupStage extends MongoStageNode {
|
|
826
|
+
kind = "lookup";
|
|
827
|
+
from;
|
|
828
|
+
localField;
|
|
829
|
+
foreignField;
|
|
830
|
+
as;
|
|
831
|
+
pipeline;
|
|
832
|
+
let_;
|
|
833
|
+
constructor(options) {
|
|
834
|
+
super();
|
|
835
|
+
const hasLocalField = options.localField !== void 0;
|
|
836
|
+
const hasForeignField = options.foreignField !== void 0;
|
|
837
|
+
const hasPipeline = !!options.pipeline;
|
|
838
|
+
if (hasLocalField !== hasForeignField) throw new Error("MongoLookupStage requires both localField and foreignField together");
|
|
839
|
+
if (!hasLocalField && !hasPipeline) throw new Error("MongoLookupStage requires either equality fields (localField/foreignField) or a pipeline");
|
|
840
|
+
if (options.let_ && !hasPipeline) throw new Error("MongoLookupStage let_ requires a pipeline");
|
|
841
|
+
this.from = options.from;
|
|
842
|
+
this.localField = options.localField;
|
|
843
|
+
this.foreignField = options.foreignField;
|
|
844
|
+
this.as = options.as;
|
|
845
|
+
this.pipeline = options.pipeline ? Object.freeze([...options.pipeline]) : void 0;
|
|
846
|
+
this.let_ = options.let_ ? Object.freeze({ ...options.let_ }) : void 0;
|
|
847
|
+
this.freeze();
|
|
848
|
+
}
|
|
849
|
+
accept(visitor) {
|
|
850
|
+
return visitor.lookup(this);
|
|
851
|
+
}
|
|
852
|
+
rewrite(context) {
|
|
853
|
+
if (!this.pipeline && !this.let_) return this;
|
|
854
|
+
const rewrittenLet = this.let_ && context.aggExpr ? rewriteExprRecord(this.let_, context.aggExpr) : this.let_;
|
|
855
|
+
const options = {
|
|
856
|
+
from: this.from,
|
|
857
|
+
as: this.as
|
|
858
|
+
};
|
|
859
|
+
if (this.localField !== void 0) options.localField = this.localField;
|
|
860
|
+
if (this.foreignField !== void 0) options.foreignField = this.foreignField;
|
|
861
|
+
if (this.pipeline) options.pipeline = this.pipeline.map((stage) => stage.rewrite(context));
|
|
862
|
+
if (rewrittenLet) options.let_ = { ...rewrittenLet };
|
|
863
|
+
return new MongoLookupStage(options);
|
|
864
|
+
}
|
|
865
|
+
};
|
|
866
|
+
var MongoUnwindStage = class extends MongoStageNode {
|
|
867
|
+
kind = "unwind";
|
|
868
|
+
path;
|
|
869
|
+
preserveNullAndEmptyArrays;
|
|
870
|
+
includeArrayIndex;
|
|
871
|
+
constructor(path, preserveNullAndEmptyArrays, includeArrayIndex) {
|
|
872
|
+
super();
|
|
873
|
+
this.path = path;
|
|
874
|
+
this.preserveNullAndEmptyArrays = preserveNullAndEmptyArrays;
|
|
875
|
+
this.includeArrayIndex = includeArrayIndex;
|
|
876
|
+
this.freeze();
|
|
877
|
+
}
|
|
878
|
+
accept(visitor) {
|
|
879
|
+
return visitor.unwind(this);
|
|
880
|
+
}
|
|
881
|
+
rewrite(_context) {
|
|
882
|
+
return this;
|
|
883
|
+
}
|
|
884
|
+
};
|
|
885
|
+
var MongoGroupStage = class MongoGroupStage extends MongoStageNode {
|
|
886
|
+
kind = "group";
|
|
887
|
+
groupId;
|
|
888
|
+
accumulators;
|
|
889
|
+
constructor(groupId, accumulators) {
|
|
890
|
+
super();
|
|
891
|
+
this.groupId = groupId;
|
|
892
|
+
this.accumulators = Object.freeze({ ...accumulators });
|
|
893
|
+
this.freeze();
|
|
894
|
+
}
|
|
895
|
+
accept(visitor) {
|
|
896
|
+
return visitor.group(this);
|
|
897
|
+
}
|
|
898
|
+
rewrite(context) {
|
|
899
|
+
const rewriter = context.aggExpr;
|
|
900
|
+
if (!rewriter) return this;
|
|
901
|
+
const newAccumulators = {};
|
|
902
|
+
for (const [key, acc] of Object.entries(this.accumulators)) newAccumulators[key] = acc.rewrite(rewriter);
|
|
903
|
+
return new MongoGroupStage(rewriteGroupId(this.groupId, rewriter), newAccumulators);
|
|
904
|
+
}
|
|
905
|
+
};
|
|
906
|
+
var MongoAddFieldsStage = class MongoAddFieldsStage extends MongoStageNode {
|
|
907
|
+
kind = "addFields";
|
|
908
|
+
fields;
|
|
909
|
+
constructor(fields) {
|
|
910
|
+
super();
|
|
911
|
+
this.fields = Object.freeze({ ...fields });
|
|
912
|
+
this.freeze();
|
|
913
|
+
}
|
|
914
|
+
accept(visitor) {
|
|
915
|
+
return visitor.addFields(this);
|
|
916
|
+
}
|
|
917
|
+
rewrite(context) {
|
|
918
|
+
const rewriter = context.aggExpr;
|
|
919
|
+
if (!rewriter) return this;
|
|
920
|
+
return new MongoAddFieldsStage(rewriteExprRecord(this.fields, rewriter));
|
|
921
|
+
}
|
|
922
|
+
};
|
|
923
|
+
var MongoReplaceRootStage = class MongoReplaceRootStage extends MongoStageNode {
|
|
924
|
+
kind = "replaceRoot";
|
|
925
|
+
newRoot;
|
|
926
|
+
constructor(newRoot) {
|
|
927
|
+
super();
|
|
928
|
+
this.newRoot = newRoot;
|
|
929
|
+
this.freeze();
|
|
930
|
+
}
|
|
931
|
+
accept(visitor) {
|
|
932
|
+
return visitor.replaceRoot(this);
|
|
933
|
+
}
|
|
934
|
+
rewrite(context) {
|
|
935
|
+
const rewriter = context.aggExpr;
|
|
936
|
+
if (!rewriter) return this;
|
|
937
|
+
return new MongoReplaceRootStage(this.newRoot.rewrite(rewriter));
|
|
938
|
+
}
|
|
939
|
+
};
|
|
940
|
+
var MongoCountStage = class extends MongoStageNode {
|
|
941
|
+
kind = "count";
|
|
942
|
+
field;
|
|
943
|
+
constructor(field) {
|
|
944
|
+
super();
|
|
945
|
+
this.field = field;
|
|
946
|
+
this.freeze();
|
|
947
|
+
}
|
|
948
|
+
accept(visitor) {
|
|
949
|
+
return visitor.count(this);
|
|
950
|
+
}
|
|
951
|
+
rewrite(_context) {
|
|
952
|
+
return this;
|
|
953
|
+
}
|
|
954
|
+
};
|
|
955
|
+
var MongoSortByCountStage = class MongoSortByCountStage extends MongoStageNode {
|
|
956
|
+
kind = "sortByCount";
|
|
957
|
+
expr;
|
|
958
|
+
constructor(expr) {
|
|
959
|
+
super();
|
|
960
|
+
this.expr = expr;
|
|
961
|
+
this.freeze();
|
|
962
|
+
}
|
|
963
|
+
accept(visitor) {
|
|
964
|
+
return visitor.sortByCount(this);
|
|
965
|
+
}
|
|
966
|
+
rewrite(context) {
|
|
967
|
+
const rewriter = context.aggExpr;
|
|
968
|
+
if (!rewriter) return this;
|
|
969
|
+
return new MongoSortByCountStage(this.expr.rewrite(rewriter));
|
|
970
|
+
}
|
|
971
|
+
};
|
|
972
|
+
var MongoSampleStage = class extends MongoStageNode {
|
|
973
|
+
kind = "sample";
|
|
974
|
+
size;
|
|
975
|
+
constructor(size) {
|
|
976
|
+
super();
|
|
977
|
+
if (!Number.isInteger(size) || size < 0) throw new RangeError("size must be a non-negative integer");
|
|
978
|
+
this.size = size;
|
|
979
|
+
this.freeze();
|
|
980
|
+
}
|
|
981
|
+
accept(visitor) {
|
|
982
|
+
return visitor.sample(this);
|
|
983
|
+
}
|
|
984
|
+
rewrite(_context) {
|
|
985
|
+
return this;
|
|
986
|
+
}
|
|
987
|
+
};
|
|
988
|
+
var MongoRedactStage = class MongoRedactStage extends MongoStageNode {
|
|
989
|
+
kind = "redact";
|
|
990
|
+
expr;
|
|
991
|
+
constructor(expr) {
|
|
992
|
+
super();
|
|
993
|
+
this.expr = expr;
|
|
994
|
+
this.freeze();
|
|
995
|
+
}
|
|
996
|
+
accept(visitor) {
|
|
997
|
+
return visitor.redact(this);
|
|
998
|
+
}
|
|
999
|
+
rewrite(context) {
|
|
1000
|
+
const rewriter = context.aggExpr;
|
|
1001
|
+
if (!rewriter) return this;
|
|
1002
|
+
return new MongoRedactStage(this.expr.rewrite(rewriter));
|
|
1003
|
+
}
|
|
1004
|
+
};
|
|
1005
|
+
var MongoOutStage = class extends MongoStageNode {
|
|
1006
|
+
kind = "out";
|
|
1007
|
+
collection;
|
|
1008
|
+
db;
|
|
1009
|
+
constructor(collection, db) {
|
|
1010
|
+
super();
|
|
1011
|
+
this.collection = collection;
|
|
1012
|
+
this.db = db;
|
|
1013
|
+
this.freeze();
|
|
1014
|
+
}
|
|
1015
|
+
accept(visitor) {
|
|
1016
|
+
return visitor.out(this);
|
|
1017
|
+
}
|
|
1018
|
+
rewrite(_context) {
|
|
1019
|
+
return this;
|
|
1020
|
+
}
|
|
1021
|
+
};
|
|
1022
|
+
var MongoUnionWithStage = class MongoUnionWithStage extends MongoStageNode {
|
|
1023
|
+
kind = "unionWith";
|
|
1024
|
+
collection;
|
|
1025
|
+
pipeline;
|
|
1026
|
+
constructor(collection, pipeline) {
|
|
1027
|
+
super();
|
|
1028
|
+
this.collection = collection;
|
|
1029
|
+
this.pipeline = pipeline ? Object.freeze([...pipeline]) : void 0;
|
|
1030
|
+
this.freeze();
|
|
1031
|
+
}
|
|
1032
|
+
accept(visitor) {
|
|
1033
|
+
return visitor.unionWith(this);
|
|
1034
|
+
}
|
|
1035
|
+
rewrite(context) {
|
|
1036
|
+
if (!this.pipeline) return this;
|
|
1037
|
+
return new MongoUnionWithStage(this.collection, this.pipeline.map((stage) => stage.rewrite(context)));
|
|
1038
|
+
}
|
|
1039
|
+
};
|
|
1040
|
+
var MongoBucketStage = class MongoBucketStage extends MongoStageNode {
|
|
1041
|
+
kind = "bucket";
|
|
1042
|
+
groupBy;
|
|
1043
|
+
boundaries;
|
|
1044
|
+
default_;
|
|
1045
|
+
output;
|
|
1046
|
+
constructor(options) {
|
|
1047
|
+
super();
|
|
1048
|
+
if (options.boundaries.length < 2) throw new RangeError("boundaries must contain at least 2 values");
|
|
1049
|
+
this.groupBy = options.groupBy;
|
|
1050
|
+
this.boundaries = Object.freeze([...options.boundaries]);
|
|
1051
|
+
this.default_ = options.default_;
|
|
1052
|
+
this.output = options.output ? Object.freeze({ ...options.output }) : void 0;
|
|
1053
|
+
this.freeze();
|
|
1054
|
+
}
|
|
1055
|
+
accept(visitor) {
|
|
1056
|
+
return visitor.bucket(this);
|
|
1057
|
+
}
|
|
1058
|
+
rewrite(context) {
|
|
1059
|
+
const rewriter = context.aggExpr;
|
|
1060
|
+
if (!rewriter) return this;
|
|
1061
|
+
const opts = {
|
|
1062
|
+
groupBy: this.groupBy.rewrite(rewriter),
|
|
1063
|
+
boundaries: this.boundaries
|
|
1064
|
+
};
|
|
1065
|
+
if (this.default_ !== void 0) opts.default_ = this.default_;
|
|
1066
|
+
if (this.output) opts.output = rewriteAccumulatorRecord(this.output, rewriter);
|
|
1067
|
+
return new MongoBucketStage(opts);
|
|
1068
|
+
}
|
|
1069
|
+
};
|
|
1070
|
+
var MongoBucketAutoStage = class MongoBucketAutoStage extends MongoStageNode {
|
|
1071
|
+
kind = "bucketAuto";
|
|
1072
|
+
groupBy;
|
|
1073
|
+
buckets;
|
|
1074
|
+
output;
|
|
1075
|
+
granularity;
|
|
1076
|
+
constructor(options) {
|
|
1077
|
+
super();
|
|
1078
|
+
if (!Number.isInteger(options.buckets) || options.buckets < 1) throw new RangeError("buckets must be a positive integer");
|
|
1079
|
+
this.groupBy = options.groupBy;
|
|
1080
|
+
this.buckets = options.buckets;
|
|
1081
|
+
this.output = options.output ? Object.freeze({ ...options.output }) : void 0;
|
|
1082
|
+
this.granularity = options.granularity;
|
|
1083
|
+
this.freeze();
|
|
1084
|
+
}
|
|
1085
|
+
accept(visitor) {
|
|
1086
|
+
return visitor.bucketAuto(this);
|
|
1087
|
+
}
|
|
1088
|
+
rewrite(context) {
|
|
1089
|
+
const rewriter = context.aggExpr;
|
|
1090
|
+
if (!rewriter) return this;
|
|
1091
|
+
const opts = {
|
|
1092
|
+
groupBy: this.groupBy.rewrite(rewriter),
|
|
1093
|
+
buckets: this.buckets
|
|
1094
|
+
};
|
|
1095
|
+
if (this.output) opts.output = rewriteAccumulatorRecord(this.output, rewriter);
|
|
1096
|
+
if (this.granularity !== void 0) opts.granularity = this.granularity;
|
|
1097
|
+
return new MongoBucketAutoStage(opts);
|
|
1098
|
+
}
|
|
1099
|
+
};
|
|
1100
|
+
var MongoGeoNearStage = class MongoGeoNearStage extends MongoStageNode {
|
|
1101
|
+
kind = "geoNear";
|
|
1102
|
+
near;
|
|
1103
|
+
distanceField;
|
|
1104
|
+
spherical;
|
|
1105
|
+
maxDistance;
|
|
1106
|
+
minDistance;
|
|
1107
|
+
query;
|
|
1108
|
+
key;
|
|
1109
|
+
distanceMultiplier;
|
|
1110
|
+
includeLocs;
|
|
1111
|
+
constructor(options) {
|
|
1112
|
+
super();
|
|
1113
|
+
this.near = options.near;
|
|
1114
|
+
this.distanceField = options.distanceField;
|
|
1115
|
+
this.spherical = options.spherical;
|
|
1116
|
+
this.maxDistance = options.maxDistance;
|
|
1117
|
+
this.minDistance = options.minDistance;
|
|
1118
|
+
this.query = options.query;
|
|
1119
|
+
this.key = options.key;
|
|
1120
|
+
this.distanceMultiplier = options.distanceMultiplier;
|
|
1121
|
+
this.includeLocs = options.includeLocs;
|
|
1122
|
+
this.freeze();
|
|
1123
|
+
}
|
|
1124
|
+
accept(visitor) {
|
|
1125
|
+
return visitor.geoNear(this);
|
|
1126
|
+
}
|
|
1127
|
+
rewrite(context) {
|
|
1128
|
+
if (!this.query || !context.filter) return this;
|
|
1129
|
+
const opts = {
|
|
1130
|
+
near: this.near,
|
|
1131
|
+
distanceField: this.distanceField
|
|
1132
|
+
};
|
|
1133
|
+
if (this.spherical !== void 0) opts.spherical = this.spherical;
|
|
1134
|
+
if (this.maxDistance !== void 0) opts.maxDistance = this.maxDistance;
|
|
1135
|
+
if (this.minDistance !== void 0) opts.minDistance = this.minDistance;
|
|
1136
|
+
opts.query = this.query.rewrite(context.filter);
|
|
1137
|
+
if (this.key !== void 0) opts.key = this.key;
|
|
1138
|
+
if (this.distanceMultiplier !== void 0) opts.distanceMultiplier = this.distanceMultiplier;
|
|
1139
|
+
if (this.includeLocs !== void 0) opts.includeLocs = this.includeLocs;
|
|
1140
|
+
return new MongoGeoNearStage(opts);
|
|
1141
|
+
}
|
|
1142
|
+
};
|
|
1143
|
+
var MongoFacetStage = class MongoFacetStage extends MongoStageNode {
|
|
1144
|
+
kind = "facet";
|
|
1145
|
+
facets;
|
|
1146
|
+
constructor(facets) {
|
|
1147
|
+
super();
|
|
1148
|
+
const frozen = {};
|
|
1149
|
+
for (const [key, pipeline] of Object.entries(facets)) frozen[key] = Object.freeze([...pipeline]);
|
|
1150
|
+
this.facets = Object.freeze(frozen);
|
|
1151
|
+
this.freeze();
|
|
1152
|
+
}
|
|
1153
|
+
accept(visitor) {
|
|
1154
|
+
return visitor.facet(this);
|
|
1155
|
+
}
|
|
1156
|
+
rewrite(context) {
|
|
1157
|
+
const newFacets = {};
|
|
1158
|
+
for (const [key, pipeline] of Object.entries(this.facets)) newFacets[key] = pipeline.map((stage) => stage.rewrite(context));
|
|
1159
|
+
return new MongoFacetStage(newFacets);
|
|
1160
|
+
}
|
|
1161
|
+
};
|
|
1162
|
+
var MongoGraphLookupStage = class MongoGraphLookupStage extends MongoStageNode {
|
|
1163
|
+
kind = "graphLookup";
|
|
1164
|
+
from;
|
|
1165
|
+
startWith;
|
|
1166
|
+
connectFromField;
|
|
1167
|
+
connectToField;
|
|
1168
|
+
as;
|
|
1169
|
+
maxDepth;
|
|
1170
|
+
depthField;
|
|
1171
|
+
restrictSearchWithMatch;
|
|
1172
|
+
constructor(options) {
|
|
1173
|
+
super();
|
|
1174
|
+
this.from = options.from;
|
|
1175
|
+
this.startWith = options.startWith;
|
|
1176
|
+
this.connectFromField = options.connectFromField;
|
|
1177
|
+
this.connectToField = options.connectToField;
|
|
1178
|
+
this.as = options.as;
|
|
1179
|
+
this.maxDepth = options.maxDepth;
|
|
1180
|
+
this.depthField = options.depthField;
|
|
1181
|
+
this.restrictSearchWithMatch = options.restrictSearchWithMatch;
|
|
1182
|
+
this.freeze();
|
|
1183
|
+
}
|
|
1184
|
+
accept(visitor) {
|
|
1185
|
+
return visitor.graphLookup(this);
|
|
1186
|
+
}
|
|
1187
|
+
rewrite(context) {
|
|
1188
|
+
const rewrittenStartWith = context.aggExpr ? this.startWith.rewrite(context.aggExpr) : this.startWith;
|
|
1189
|
+
const rewrittenMatch = this.restrictSearchWithMatch && context.filter ? this.restrictSearchWithMatch.rewrite(context.filter) : this.restrictSearchWithMatch;
|
|
1190
|
+
if (rewrittenStartWith === this.startWith && rewrittenMatch === this.restrictSearchWithMatch) return this;
|
|
1191
|
+
const opts = {
|
|
1192
|
+
from: this.from,
|
|
1193
|
+
startWith: rewrittenStartWith,
|
|
1194
|
+
connectFromField: this.connectFromField,
|
|
1195
|
+
connectToField: this.connectToField,
|
|
1196
|
+
as: this.as
|
|
1197
|
+
};
|
|
1198
|
+
if (this.maxDepth !== void 0) opts.maxDepth = this.maxDepth;
|
|
1199
|
+
if (this.depthField !== void 0) opts.depthField = this.depthField;
|
|
1200
|
+
if (rewrittenMatch) opts.restrictSearchWithMatch = rewrittenMatch;
|
|
1201
|
+
return new MongoGraphLookupStage(opts);
|
|
1202
|
+
}
|
|
1203
|
+
};
|
|
1204
|
+
var MongoMergeStage = class MongoMergeStage extends MongoStageNode {
|
|
1205
|
+
kind = "merge";
|
|
1206
|
+
into;
|
|
1207
|
+
on;
|
|
1208
|
+
whenMatched;
|
|
1209
|
+
whenNotMatched;
|
|
1210
|
+
constructor(options) {
|
|
1211
|
+
super();
|
|
1212
|
+
this.into = typeof options.into === "string" ? options.into : Object.freeze({ ...options.into });
|
|
1213
|
+
this.on = options.on === void 0 ? void 0 : typeof options.on === "string" ? options.on : Object.freeze([...options.on]);
|
|
1214
|
+
this.whenMatched = options.whenMatched === void 0 ? void 0 : typeof options.whenMatched === "string" ? options.whenMatched : Object.freeze([...options.whenMatched]);
|
|
1215
|
+
this.whenNotMatched = options.whenNotMatched;
|
|
1216
|
+
this.freeze();
|
|
1217
|
+
}
|
|
1218
|
+
accept(visitor) {
|
|
1219
|
+
return visitor.merge(this);
|
|
1220
|
+
}
|
|
1221
|
+
rewrite(context) {
|
|
1222
|
+
if (!Array.isArray(this.whenMatched)) return this;
|
|
1223
|
+
const opts = { into: this.into };
|
|
1224
|
+
if (this.on !== void 0) opts.on = this.on;
|
|
1225
|
+
opts.whenMatched = this.whenMatched.map((stage) => stage.rewrite(context));
|
|
1226
|
+
if (this.whenNotMatched !== void 0) opts.whenNotMatched = this.whenNotMatched;
|
|
1227
|
+
return new MongoMergeStage(opts);
|
|
1228
|
+
}
|
|
1229
|
+
};
|
|
1230
|
+
var MongoSetWindowFieldsStage = class MongoSetWindowFieldsStage extends MongoStageNode {
|
|
1231
|
+
kind = "setWindowFields";
|
|
1232
|
+
partitionBy;
|
|
1233
|
+
sortBy;
|
|
1234
|
+
output;
|
|
1235
|
+
constructor(options) {
|
|
1236
|
+
super();
|
|
1237
|
+
this.partitionBy = options.partitionBy;
|
|
1238
|
+
this.sortBy = options.sortBy ? Object.freeze({ ...options.sortBy }) : void 0;
|
|
1239
|
+
this.output = Object.freeze({ ...options.output });
|
|
1240
|
+
this.freeze();
|
|
1241
|
+
}
|
|
1242
|
+
accept(visitor) {
|
|
1243
|
+
return visitor.setWindowFields(this);
|
|
1244
|
+
}
|
|
1245
|
+
rewrite(context) {
|
|
1246
|
+
const rewriter = context.aggExpr;
|
|
1247
|
+
if (!rewriter) return this;
|
|
1248
|
+
const newOutput = {};
|
|
1249
|
+
for (const [key, wf] of Object.entries(this.output)) newOutput[key] = {
|
|
1250
|
+
...wf,
|
|
1251
|
+
operator: wf.operator.rewrite(rewriter)
|
|
1252
|
+
};
|
|
1253
|
+
const opts = { output: newOutput };
|
|
1254
|
+
if (this.partitionBy) opts.partitionBy = this.partitionBy.rewrite(rewriter);
|
|
1255
|
+
if (this.sortBy) opts.sortBy = { ...this.sortBy };
|
|
1256
|
+
return new MongoSetWindowFieldsStage(opts);
|
|
1257
|
+
}
|
|
1258
|
+
};
|
|
1259
|
+
var MongoDensifyStage = class extends MongoStageNode {
|
|
1260
|
+
kind = "densify";
|
|
1261
|
+
field;
|
|
1262
|
+
partitionByFields;
|
|
1263
|
+
range;
|
|
1264
|
+
constructor(options) {
|
|
1265
|
+
super();
|
|
1266
|
+
this.field = options.field;
|
|
1267
|
+
this.partitionByFields = options.partitionByFields ? Object.freeze([...options.partitionByFields]) : void 0;
|
|
1268
|
+
this.range = Object.freeze({ ...options.range });
|
|
1269
|
+
this.freeze();
|
|
1270
|
+
}
|
|
1271
|
+
accept(visitor) {
|
|
1272
|
+
return visitor.densify(this);
|
|
1273
|
+
}
|
|
1274
|
+
rewrite(_context) {
|
|
1275
|
+
return this;
|
|
1276
|
+
}
|
|
1277
|
+
};
|
|
1278
|
+
var MongoFillStage = class MongoFillStage extends MongoStageNode {
|
|
1279
|
+
kind = "fill";
|
|
1280
|
+
partitionBy;
|
|
1281
|
+
partitionByFields;
|
|
1282
|
+
sortBy;
|
|
1283
|
+
output;
|
|
1284
|
+
constructor(options) {
|
|
1285
|
+
super();
|
|
1286
|
+
this.partitionBy = options.partitionBy;
|
|
1287
|
+
this.partitionByFields = options.partitionByFields ? Object.freeze([...options.partitionByFields]) : void 0;
|
|
1288
|
+
this.sortBy = options.sortBy ? Object.freeze({ ...options.sortBy }) : void 0;
|
|
1289
|
+
this.output = Object.freeze({ ...options.output });
|
|
1290
|
+
this.freeze();
|
|
1291
|
+
}
|
|
1292
|
+
accept(visitor) {
|
|
1293
|
+
return visitor.fill(this);
|
|
1294
|
+
}
|
|
1295
|
+
rewrite(context) {
|
|
1296
|
+
const rewriter = context.aggExpr;
|
|
1297
|
+
if (!rewriter) return this;
|
|
1298
|
+
const newOutput = {};
|
|
1299
|
+
for (const [key, fo] of Object.entries(this.output)) newOutput[key] = fo.value ? {
|
|
1300
|
+
...fo,
|
|
1301
|
+
value: fo.value.rewrite(rewriter)
|
|
1302
|
+
} : fo;
|
|
1303
|
+
const opts = { output: newOutput };
|
|
1304
|
+
if (this.partitionBy) opts.partitionBy = this.partitionBy.rewrite(rewriter);
|
|
1305
|
+
if (this.partitionByFields) opts.partitionByFields = [...this.partitionByFields];
|
|
1306
|
+
if (this.sortBy) opts.sortBy = { ...this.sortBy };
|
|
1307
|
+
return new MongoFillStage(opts);
|
|
1308
|
+
}
|
|
1309
|
+
};
|
|
1310
|
+
var MongoSearchStage = class extends MongoStageNode {
|
|
1311
|
+
kind = "search";
|
|
1312
|
+
index;
|
|
1313
|
+
config;
|
|
1314
|
+
constructor(config, index) {
|
|
1315
|
+
super();
|
|
1316
|
+
this.config = Object.freeze({ ...config });
|
|
1317
|
+
this.index = index;
|
|
1318
|
+
this.freeze();
|
|
1319
|
+
}
|
|
1320
|
+
accept(visitor) {
|
|
1321
|
+
return visitor.search(this);
|
|
1322
|
+
}
|
|
1323
|
+
rewrite(_context) {
|
|
1324
|
+
return this;
|
|
1325
|
+
}
|
|
1326
|
+
};
|
|
1327
|
+
var MongoSearchMetaStage = class extends MongoStageNode {
|
|
1328
|
+
kind = "searchMeta";
|
|
1329
|
+
index;
|
|
1330
|
+
config;
|
|
1331
|
+
constructor(config, index) {
|
|
1332
|
+
super();
|
|
1333
|
+
this.config = Object.freeze({ ...config });
|
|
1334
|
+
this.index = index;
|
|
1335
|
+
this.freeze();
|
|
1336
|
+
}
|
|
1337
|
+
accept(visitor) {
|
|
1338
|
+
return visitor.searchMeta(this);
|
|
1339
|
+
}
|
|
1340
|
+
rewrite(_context) {
|
|
1341
|
+
return this;
|
|
1342
|
+
}
|
|
1343
|
+
};
|
|
1344
|
+
var MongoVectorSearchStage = class extends MongoStageNode {
|
|
1345
|
+
kind = "vectorSearch";
|
|
1346
|
+
index;
|
|
1347
|
+
path;
|
|
1348
|
+
queryVector;
|
|
1349
|
+
numCandidates;
|
|
1350
|
+
limit;
|
|
1351
|
+
filter;
|
|
1352
|
+
constructor(options) {
|
|
1353
|
+
super();
|
|
1354
|
+
if (!Number.isInteger(options.limit) || options.limit < 1) throw new RangeError("limit must be a positive integer");
|
|
1355
|
+
if (!Number.isInteger(options.numCandidates) || options.numCandidates < options.limit) throw new RangeError("numCandidates must be an integer >= limit");
|
|
1356
|
+
this.index = options.index;
|
|
1357
|
+
this.path = options.path;
|
|
1358
|
+
this.queryVector = Object.freeze([...options.queryVector]);
|
|
1359
|
+
this.numCandidates = options.numCandidates;
|
|
1360
|
+
this.limit = options.limit;
|
|
1361
|
+
this.filter = options.filter ? Object.freeze({ ...options.filter }) : void 0;
|
|
1362
|
+
this.freeze();
|
|
1363
|
+
}
|
|
1364
|
+
accept(visitor) {
|
|
1365
|
+
return visitor.vectorSearch(this);
|
|
1366
|
+
}
|
|
1367
|
+
rewrite(_context) {
|
|
1368
|
+
return this;
|
|
1369
|
+
}
|
|
1370
|
+
};
|
|
1371
|
+
|
|
1372
|
+
//#endregion
|
|
1373
|
+
export { AggregateCommand, DeleteManyCommand, DeleteOneCommand, FindOneAndDeleteCommand, FindOneAndUpdateCommand, InsertManyCommand, InsertOneCommand, MongoAddFieldsStage, MongoAggAccumulator, MongoAggArrayFilter, MongoAggCond, MongoAggFieldRef, MongoAggLet, MongoAggLiteral, MongoAggMap, MongoAggMergeObjects, MongoAggOperator, MongoAggReduce, MongoAggSwitch, MongoAndExpr, MongoBucketAutoStage, MongoBucketStage, MongoCountStage, MongoDensifyStage, MongoExistsExpr, MongoExprFilter, MongoFacetStage, MongoFieldFilter, MongoFillStage, MongoGeoNearStage, MongoGraphLookupStage, MongoGroupStage, MongoLimitStage, MongoLookupStage, MongoMatchStage, MongoMergeStage, MongoNotExpr, MongoOrExpr, MongoOutStage, MongoProjectStage, MongoRedactStage, MongoReplaceRootStage, MongoSampleStage, MongoSearchMetaStage, MongoSearchStage, MongoSetWindowFieldsStage, MongoSkipStage, MongoSortByCountStage, MongoSortStage, MongoUnionWithStage, MongoUnwindStage, MongoVectorSearchStage, RawAggregateCommand, RawDeleteManyCommand, RawDeleteOneCommand, RawFindOneAndDeleteCommand, RawFindOneAndUpdateCommand, RawInsertManyCommand, RawInsertOneCommand, RawUpdateManyCommand, RawUpdateOneCommand, UpdateManyCommand, UpdateOneCommand, isExprArray, isRecordArgs };
|
|
1374
|
+
//# sourceMappingURL=index.mjs.map
|