@resolveio/server-lib 20.9.10 → 20.9.12

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.
@@ -62,6 +62,15 @@ var __read = (this && this.__read) || function (o, n) {
62
62
  }
63
63
  return ar;
64
64
  };
65
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
66
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
67
+ if (ar || !(i in from)) {
68
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
69
+ ar[i] = from[i];
70
+ }
71
+ }
72
+ return to.concat(ar || Array.prototype.slice.call(from));
73
+ };
65
74
  Object.defineProperty(exports, "__esModule", { value: true });
66
75
  exports.recordAggregateDependencies = exports.recordDependencyResult = exports.withDependencyTracking = void 0;
67
76
  var async_hooks_1 = require("async_hooks");
@@ -119,6 +128,7 @@ function recordDependencyResult(collection, result, options) {
119
128
  context.filters.set(collection, filterList);
120
129
  }
121
130
  filterList.push((0, common_1.deepCopy)(options.filter));
131
+ context.watchAllCollections.delete(collection);
122
132
  }
123
133
  if (options === null || options === void 0 ? void 0 : options.meta) {
124
134
  var metaList = context.queryMetadata.get(collection);
@@ -134,6 +144,7 @@ function recordDependencyResult(collection, result, options) {
134
144
  collectIdsFromFilter(options.filter, ids);
135
145
  }
136
146
  if (ids.size) {
147
+ context.watchAllCollections.delete(collection);
137
148
  var existing = context.dependencies.get(collection);
138
149
  if (!existing) {
139
150
  existing = new Set();
@@ -159,7 +170,7 @@ function recordDependencyResult(collection, result, options) {
159
170
  }
160
171
  exports.recordDependencyResult = recordDependencyResult;
161
172
  function recordAggregateDependencies(collection, pipeline, result) {
162
- var e_2, _a;
173
+ var e_2, _a, e_3, _b, e_4, _c;
163
174
  var context = dependencyStorage.getStore();
164
175
  if (!context) {
165
176
  return;
@@ -179,12 +190,61 @@ function recordAggregateDependencies(collection, pipeline, result) {
179
190
  var target = lookupTargets_1_1.value;
180
191
  var nestedValues = [];
181
192
  collectLookupValues(result, target.as, nestedValues);
193
+ var rootDocs = Array.isArray(result) ? result : [result];
194
+ var pipelineFilters = [];
195
+ var pipelineUnresolved = false;
196
+ try {
197
+ for (var rootDocs_1 = (e_3 = void 0, __values(rootDocs)), rootDocs_1_1 = rootDocs_1.next(); !rootDocs_1_1.done; rootDocs_1_1 = rootDocs_1.next()) {
198
+ var rootDoc = rootDocs_1_1.value;
199
+ var _d = buildLookupPipelineFilters(rootDoc, target), filtersFromPipeline = _d.filters, unresolved = _d.unresolved;
200
+ if (unresolved) {
201
+ pipelineUnresolved = true;
202
+ }
203
+ if (filtersFromPipeline === null || filtersFromPipeline === void 0 ? void 0 : filtersFromPipeline.length) {
204
+ pipelineFilters.push.apply(pipelineFilters, __spreadArray([], __read(filtersFromPipeline), false));
205
+ }
206
+ }
207
+ }
208
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
209
+ finally {
210
+ try {
211
+ if (rootDocs_1_1 && !rootDocs_1_1.done && (_b = rootDocs_1.return)) _b.call(rootDocs_1);
212
+ }
213
+ finally { if (e_3) throw e_3.error; }
214
+ }
182
215
  var filter = buildLookupFilter(result, target.localField, target.foreignField);
183
- if (nestedValues.length || filter) {
216
+ var uniquePipelineFilters = dedupeFilters(pipelineFilters);
217
+ if (nestedValues.length) {
184
218
  recordDependencyResult(target.from, nestedValues, filter ? { filter: filter } : undefined);
185
219
  }
186
- else {
187
- context.watchAllCollections.add(target.from);
220
+ if (uniquePipelineFilters.length) {
221
+ var pipelineMeta = extractAggregationMetaFromPipeline(target.pipeline);
222
+ try {
223
+ for (var uniquePipelineFilters_1 = (e_4 = void 0, __values(uniquePipelineFilters)), uniquePipelineFilters_1_1 = uniquePipelineFilters_1.next(); !uniquePipelineFilters_1_1.done; uniquePipelineFilters_1_1 = uniquePipelineFilters_1.next()) {
224
+ var pipelineFilter = uniquePipelineFilters_1_1.value;
225
+ recordDependencyResult(target.from, [], { filter: pipelineFilter, meta: pipelineMeta });
226
+ }
227
+ }
228
+ catch (e_4_1) { e_4 = { error: e_4_1 }; }
229
+ finally {
230
+ try {
231
+ if (uniquePipelineFilters_1_1 && !uniquePipelineFilters_1_1.done && (_c = uniquePipelineFilters_1.return)) _c.call(uniquePipelineFilters_1);
232
+ }
233
+ finally { if (e_4) throw e_4.error; }
234
+ }
235
+ continue;
236
+ }
237
+ if (!nestedValues.length && filter) {
238
+ recordDependencyResult(target.from, [], { filter: filter });
239
+ continue;
240
+ }
241
+ if (!nestedValues.length) {
242
+ if (pipelineUnresolved) {
243
+ context.watchAllCollections.add(target.from);
244
+ }
245
+ else if (!target.pipeline || !target.pipeline.length) {
246
+ context.watchAllCollections.add(target.from);
247
+ }
188
248
  }
189
249
  }
190
250
  }
@@ -198,7 +258,7 @@ function recordAggregateDependencies(collection, pipeline, result) {
198
258
  }
199
259
  exports.recordAggregateDependencies = recordAggregateDependencies;
200
260
  function extractLookupTargets(pipeline) {
201
- var e_3, _a;
261
+ var e_5, _a;
202
262
  var lookups = [];
203
263
  if (!Array.isArray(pipeline)) {
204
264
  return lookups;
@@ -213,23 +273,25 @@ function extractLookupTargets(pipeline) {
213
273
  from: lookup.from,
214
274
  as: lookup.as,
215
275
  localField: typeof lookup.localField === 'string' ? lookup.localField : undefined,
216
- foreignField: typeof lookup.foreignField === 'string' ? lookup.foreignField : undefined
276
+ foreignField: typeof lookup.foreignField === 'string' ? lookup.foreignField : undefined,
277
+ let: lookup.let && typeof lookup.let === 'object' ? lookup.let : undefined,
278
+ pipeline: Array.isArray(lookup.pipeline) ? lookup.pipeline : undefined
217
279
  });
218
280
  }
219
281
  }
220
282
  }
221
283
  }
222
- catch (e_3_1) { e_3 = { error: e_3_1 }; }
284
+ catch (e_5_1) { e_5 = { error: e_5_1 }; }
223
285
  finally {
224
286
  try {
225
287
  if (pipeline_1_1 && !pipeline_1_1.done && (_a = pipeline_1.return)) _a.call(pipeline_1);
226
288
  }
227
- finally { if (e_3) throw e_3.error; }
289
+ finally { if (e_5) throw e_5.error; }
228
290
  }
229
291
  return lookups;
230
292
  }
231
293
  function extractMatchFilter(pipeline) {
232
- var e_4, _a;
294
+ var e_6, _a;
233
295
  if (!Array.isArray(pipeline)) {
234
296
  return null;
235
297
  }
@@ -242,12 +304,12 @@ function extractMatchFilter(pipeline) {
242
304
  }
243
305
  }
244
306
  }
245
- catch (e_4_1) { e_4 = { error: e_4_1 }; }
307
+ catch (e_6_1) { e_6 = { error: e_6_1 }; }
246
308
  finally {
247
309
  try {
248
310
  if (pipeline_2_1 && !pipeline_2_1.done && (_a = pipeline_2.return)) _a.call(pipeline_2);
249
311
  }
250
- finally { if (e_4) throw e_4.error; }
312
+ finally { if (e_6) throw e_6.error; }
251
313
  }
252
314
  if (!filters.length) {
253
315
  return null;
@@ -258,7 +320,7 @@ function extractMatchFilter(pipeline) {
258
320
  return { $and: filters };
259
321
  }
260
322
  function extractAggregateMeta(pipeline) {
261
- var e_5, _a;
323
+ var e_7, _a;
262
324
  if (!Array.isArray(pipeline)) {
263
325
  return null;
264
326
  }
@@ -279,12 +341,53 @@ function extractAggregateMeta(pipeline) {
279
341
  }
280
342
  }
281
343
  }
282
- catch (e_5_1) { e_5 = { error: e_5_1 }; }
344
+ catch (e_7_1) { e_7 = { error: e_7_1 }; }
283
345
  finally {
284
346
  try {
285
347
  if (pipeline_3_1 && !pipeline_3_1.done && (_a = pipeline_3.return)) _a.call(pipeline_3);
286
348
  }
287
- finally { if (e_5) throw e_5.error; }
349
+ finally { if (e_7) throw e_7.error; }
350
+ }
351
+ var meta = {};
352
+ if (limit !== undefined) {
353
+ meta.limit = limit;
354
+ }
355
+ if (skip !== undefined) {
356
+ meta.skip = skip;
357
+ }
358
+ if (sort) {
359
+ meta.sort = sort;
360
+ }
361
+ return Object.keys(meta).length ? meta : null;
362
+ }
363
+ function extractAggregationMetaFromPipeline(pipeline) {
364
+ var e_8, _a;
365
+ if (!Array.isArray(pipeline)) {
366
+ return null;
367
+ }
368
+ var limit = undefined;
369
+ var skip = undefined;
370
+ var sort = null;
371
+ try {
372
+ for (var pipeline_4 = __values(pipeline), pipeline_4_1 = pipeline_4.next(); !pipeline_4_1.done; pipeline_4_1 = pipeline_4.next()) {
373
+ var stage = pipeline_4_1.value;
374
+ if ((stage === null || stage === void 0 ? void 0 : stage.$limit) !== undefined && typeof stage.$limit === 'number') {
375
+ limit = stage.$limit;
376
+ }
377
+ else if ((stage === null || stage === void 0 ? void 0 : stage.$skip) !== undefined && typeof stage.$skip === 'number') {
378
+ skip = stage.$skip;
379
+ }
380
+ else if ((stage === null || stage === void 0 ? void 0 : stage.$sort) && typeof stage.$sort === 'object') {
381
+ sort = (0, common_1.deepCopy)(stage.$sort);
382
+ }
383
+ }
384
+ }
385
+ catch (e_8_1) { e_8 = { error: e_8_1 }; }
386
+ finally {
387
+ try {
388
+ if (pipeline_4_1 && !pipeline_4_1.done && (_a = pipeline_4.return)) _a.call(pipeline_4);
389
+ }
390
+ finally { if (e_8) throw e_8.error; }
288
391
  }
289
392
  var meta = {};
290
393
  if (limit !== undefined) {
@@ -299,7 +402,7 @@ function extractAggregateMeta(pipeline) {
299
402
  return Object.keys(meta).length ? meta : null;
300
403
  }
301
404
  function collectLookupValues(source, path, output) {
302
- var e_6, _a;
405
+ var e_9, _a;
303
406
  if (!source) {
304
407
  return;
305
408
  }
@@ -310,12 +413,12 @@ function collectLookupValues(source, path, output) {
310
413
  collectLookupValues(item, path, output);
311
414
  }
312
415
  }
313
- catch (e_6_1) { e_6 = { error: e_6_1 }; }
416
+ catch (e_9_1) { e_9 = { error: e_9_1 }; }
314
417
  finally {
315
418
  try {
316
419
  if (source_1_1 && !source_1_1.done && (_a = source_1.return)) _a.call(source_1);
317
420
  }
318
- finally { if (e_6) throw e_6.error; }
421
+ finally { if (e_9) throw e_9.error; }
319
422
  }
320
423
  return;
321
424
  }
@@ -326,7 +429,7 @@ function collectLookupValues(source, path, output) {
326
429
  collectLookupPathValues(source, pathSegments, 0, output);
327
430
  }
328
431
  function collectLookupPathValues(value, path, index, output) {
329
- var e_7, _a;
432
+ var e_10, _a;
330
433
  if (!value) {
331
434
  return;
332
435
  }
@@ -347,12 +450,12 @@ function collectLookupPathValues(value, path, index, output) {
347
450
  }
348
451
  }
349
452
  }
350
- catch (e_7_1) { e_7 = { error: e_7_1 }; }
453
+ catch (e_10_1) { e_10 = { error: e_10_1 }; }
351
454
  finally {
352
455
  try {
353
456
  if (current_1_1 && !current_1_1.done && (_a = current_1.return)) _a.call(current_1);
354
457
  }
355
- finally { if (e_7) throw e_7.error; }
458
+ finally { if (e_10) throw e_10.error; }
356
459
  }
357
460
  }
358
461
  else if (current && typeof current === 'object') {
@@ -378,7 +481,7 @@ function buildLookupFilter(result, localField, foreignField) {
378
481
  _a;
379
482
  }
380
483
  function collectFieldValues(source, path, index, output) {
381
- var e_8, _a, e_9, _b;
484
+ var e_11, _a, e_12, _b;
382
485
  if (!source) {
383
486
  return;
384
487
  }
@@ -389,12 +492,12 @@ function collectFieldValues(source, path, index, output) {
389
492
  collectFieldValues(item, path, index, output);
390
493
  }
391
494
  }
392
- catch (e_8_1) { e_8 = { error: e_8_1 }; }
495
+ catch (e_11_1) { e_11 = { error: e_11_1 }; }
393
496
  finally {
394
497
  try {
395
498
  if (source_2_1 && !source_2_1.done && (_a = source_2.return)) _a.call(source_2);
396
499
  }
397
- finally { if (e_8) throw e_8.error; }
500
+ finally { if (e_11) throw e_11.error; }
398
501
  }
399
502
  return;
400
503
  }
@@ -418,12 +521,12 @@ function collectFieldValues(source, path, index, output) {
418
521
  collectFieldValues(item, path, index + 1, output);
419
522
  }
420
523
  }
421
- catch (e_9_1) { e_9 = { error: e_9_1 }; }
524
+ catch (e_12_1) { e_12 = { error: e_12_1 }; }
422
525
  finally {
423
526
  try {
424
527
  if (current_2_1 && !current_2_1.done && (_b = current_2.return)) _b.call(current_2);
425
528
  }
426
- finally { if (e_9) throw e_9.error; }
529
+ finally { if (e_12) throw e_12.error; }
427
530
  }
428
531
  }
429
532
  else {
@@ -431,7 +534,7 @@ function collectFieldValues(source, path, index, output) {
431
534
  }
432
535
  }
433
536
  function appendFilterValue(output, value) {
434
- var e_10, _a;
537
+ var e_13, _a;
435
538
  if (value === undefined || value === null) {
436
539
  return;
437
540
  }
@@ -442,19 +545,19 @@ function appendFilterValue(output, value) {
442
545
  appendFilterValue(output, item);
443
546
  }
444
547
  }
445
- catch (e_10_1) { e_10 = { error: e_10_1 }; }
548
+ catch (e_13_1) { e_13 = { error: e_13_1 }; }
446
549
  finally {
447
550
  try {
448
551
  if (value_1_1 && !value_1_1.done && (_a = value_1.return)) _a.call(value_1);
449
552
  }
450
- finally { if (e_10) throw e_10.error; }
553
+ finally { if (e_13) throw e_13.error; }
451
554
  }
452
555
  return;
453
556
  }
454
557
  output.push(value);
455
558
  }
456
559
  function dedupeFilterValues(values) {
457
- var e_11, _a;
560
+ var e_14, _a;
458
561
  var unique = new Map();
459
562
  try {
460
563
  for (var values_1 = __values(values), values_1_1 = values_1.next(); !values_1_1.done; values_1_1 = values_1.next()) {
@@ -465,12 +568,12 @@ function dedupeFilterValues(values) {
465
568
  }
466
569
  }
467
570
  }
468
- catch (e_11_1) { e_11 = { error: e_11_1 }; }
571
+ catch (e_14_1) { e_14 = { error: e_14_1 }; }
469
572
  finally {
470
573
  try {
471
574
  if (values_1_1 && !values_1_1.done && (_a = values_1.return)) _a.call(values_1);
472
575
  }
473
- finally { if (e_11) throw e_11.error; }
576
+ finally { if (e_14) throw e_14.error; }
474
577
  }
475
578
  return Array.from(unique.values());
476
579
  }
@@ -498,7 +601,7 @@ function serializeFilterValue(value) {
498
601
  return String(value);
499
602
  }
500
603
  function collectIdsFromResult(source, ids, visited) {
501
- var e_12, _a, e_13, _b;
604
+ var e_15, _a, e_16, _b;
502
605
  if (visited === void 0) { visited = new WeakSet(); }
503
606
  if (!source) {
504
607
  return;
@@ -510,12 +613,12 @@ function collectIdsFromResult(source, ids, visited) {
510
613
  collectIdsFromResult(item, ids, visited);
511
614
  }
512
615
  }
513
- catch (e_12_1) { e_12 = { error: e_12_1 }; }
616
+ catch (e_15_1) { e_15 = { error: e_15_1 }; }
514
617
  finally {
515
618
  try {
516
619
  if (source_3_1 && !source_3_1.done && (_a = source_3.return)) _a.call(source_3);
517
620
  }
518
- finally { if (e_12) throw e_12.error; }
621
+ finally { if (e_15) throw e_15.error; }
519
622
  }
520
623
  return;
521
624
  }
@@ -539,16 +642,16 @@ function collectIdsFromResult(source, ids, visited) {
539
642
  }
540
643
  }
541
644
  }
542
- catch (e_13_1) { e_13 = { error: e_13_1 }; }
645
+ catch (e_16_1) { e_16 = { error: e_16_1 }; }
543
646
  finally {
544
647
  try {
545
648
  if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
546
649
  }
547
- finally { if (e_13) throw e_13.error; }
650
+ finally { if (e_16) throw e_16.error; }
548
651
  }
549
652
  }
550
653
  function collectIdsFromFilter(filter, ids) {
551
- var e_14, _a, e_15, _b, e_16, _c;
654
+ var e_17, _a, e_18, _b, e_19, _c;
552
655
  if (!filter) {
553
656
  return;
554
657
  }
@@ -559,12 +662,12 @@ function collectIdsFromFilter(filter, ids) {
559
662
  collectIdsFromFilter(item, ids);
560
663
  }
561
664
  }
562
- catch (e_14_1) { e_14 = { error: e_14_1 }; }
665
+ catch (e_17_1) { e_17 = { error: e_17_1 }; }
563
666
  finally {
564
667
  try {
565
668
  if (filter_1_1 && !filter_1_1.done && (_a = filter_1.return)) _a.call(filter_1);
566
669
  }
567
- finally { if (e_14) throw e_14.error; }
670
+ finally { if (e_17) throw e_17.error; }
568
671
  }
569
672
  return;
570
673
  }
@@ -581,17 +684,17 @@ function collectIdsFromFilter(filter, ids) {
581
684
  if (key === '$or' || key === '$and' || key === '$nor') {
582
685
  if (Array.isArray(value)) {
583
686
  try {
584
- for (var value_2 = (e_16 = void 0, __values(value)), value_2_1 = value_2.next(); !value_2_1.done; value_2_1 = value_2.next()) {
687
+ for (var value_2 = (e_19 = void 0, __values(value)), value_2_1 = value_2.next(); !value_2_1.done; value_2_1 = value_2.next()) {
585
688
  var item = value_2_1.value;
586
689
  collectIdsFromFilter(item, ids);
587
690
  }
588
691
  }
589
- catch (e_16_1) { e_16 = { error: e_16_1 }; }
692
+ catch (e_19_1) { e_19 = { error: e_19_1 }; }
590
693
  finally {
591
694
  try {
592
695
  if (value_2_1 && !value_2_1.done && (_c = value_2.return)) _c.call(value_2);
593
696
  }
594
- finally { if (e_16) throw e_16.error; }
697
+ finally { if (e_19) throw e_19.error; }
595
698
  }
596
699
  }
597
700
  }
@@ -600,16 +703,16 @@ function collectIdsFromFilter(filter, ids) {
600
703
  }
601
704
  }
602
705
  }
603
- catch (e_15_1) { e_15 = { error: e_15_1 }; }
706
+ catch (e_18_1) { e_18 = { error: e_18_1 }; }
604
707
  finally {
605
708
  try {
606
709
  if (_e && !_e.done && (_b = _d.return)) _b.call(_d);
607
710
  }
608
- finally { if (e_15) throw e_15.error; }
711
+ finally { if (e_18) throw e_18.error; }
609
712
  }
610
713
  }
611
714
  function collectIdsFromFilterValue(value, ids) {
612
- var e_17, _a, e_18, _b;
715
+ var e_20, _a, e_21, _b;
613
716
  if (value === null || value === undefined) {
614
717
  return;
615
718
  }
@@ -620,12 +723,12 @@ function collectIdsFromFilterValue(value, ids) {
620
723
  collectIdsFromFilterValue(item, ids);
621
724
  }
622
725
  }
623
- catch (e_17_1) { e_17 = { error: e_17_1 }; }
726
+ catch (e_20_1) { e_20 = { error: e_20_1 }; }
624
727
  finally {
625
728
  try {
626
729
  if (value_3_1 && !value_3_1.done && (_a = value_3.return)) _a.call(value_3);
627
730
  }
628
- finally { if (e_17) throw e_17.error; }
731
+ finally { if (e_20) throw e_20.error; }
629
732
  }
630
733
  return;
631
734
  }
@@ -647,12 +750,12 @@ function collectIdsFromFilterValue(value, ids) {
647
750
  }
648
751
  }
649
752
  }
650
- catch (e_18_1) { e_18 = { error: e_18_1 }; }
753
+ catch (e_21_1) { e_21 = { error: e_21_1 }; }
651
754
  finally {
652
755
  try {
653
756
  if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
654
757
  }
655
- finally { if (e_18) throw e_18.error; }
758
+ finally { if (e_21) throw e_21.error; }
656
759
  }
657
760
  return;
658
761
  }
@@ -690,7 +793,7 @@ function normalizeId(value) {
690
793
  return null;
691
794
  }
692
795
  function cloneDependencyMap(original) {
693
- var e_19, _a;
796
+ var e_22, _a;
694
797
  var cloned = new Map();
695
798
  try {
696
799
  for (var _b = __values(original.entries()), _c = _b.next(); !_c.done; _c = _b.next()) {
@@ -698,17 +801,17 @@ function cloneDependencyMap(original) {
698
801
  cloned.set(collection, new Set(ids));
699
802
  }
700
803
  }
701
- catch (e_19_1) { e_19 = { error: e_19_1 }; }
804
+ catch (e_22_1) { e_22 = { error: e_22_1 }; }
702
805
  finally {
703
806
  try {
704
807
  if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
705
808
  }
706
- finally { if (e_19) throw e_19.error; }
809
+ finally { if (e_22) throw e_22.error; }
707
810
  }
708
811
  return cloned;
709
812
  }
710
813
  function cloneFilterMap(original) {
711
- var e_20, _a;
814
+ var e_23, _a;
712
815
  var cloned = new Map();
713
816
  try {
714
817
  for (var _b = __values(original.entries()), _c = _b.next(); !_c.done; _c = _b.next()) {
@@ -716,17 +819,17 @@ function cloneFilterMap(original) {
716
819
  cloned.set(collection, filters.map(function (filter) { return (0, common_1.deepCopy)(filter); }));
717
820
  }
718
821
  }
719
- catch (e_20_1) { e_20 = { error: e_20_1 }; }
822
+ catch (e_23_1) { e_23 = { error: e_23_1 }; }
720
823
  finally {
721
824
  try {
722
825
  if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
723
826
  }
724
- finally { if (e_20) throw e_20.error; }
827
+ finally { if (e_23) throw e_23.error; }
725
828
  }
726
829
  return cloned;
727
830
  }
728
831
  function cloneQueryMetaMap(original) {
729
- var e_21, _a;
832
+ var e_24, _a;
730
833
  var cloned = new Map();
731
834
  try {
732
835
  for (var _b = __values(original.entries()), _c = _b.next(); !_c.done; _c = _b.next()) {
@@ -734,14 +837,281 @@ function cloneQueryMetaMap(original) {
734
837
  cloned.set(collection, metaList.map(function (meta) { return (0, common_1.deepCopy)(meta); }));
735
838
  }
736
839
  }
737
- catch (e_21_1) { e_21 = { error: e_21_1 }; }
840
+ catch (e_24_1) { e_24 = { error: e_24_1 }; }
738
841
  finally {
739
842
  try {
740
843
  if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
741
844
  }
742
- finally { if (e_21) throw e_21.error; }
845
+ finally { if (e_24) throw e_24.error; }
743
846
  }
744
847
  return cloned;
745
848
  }
849
+ function buildLookupPipelineFilters(rootDoc, target) {
850
+ var e_25, _a;
851
+ if (!target.pipeline || !target.pipeline.length) {
852
+ return { filters: [], unresolved: false };
853
+ }
854
+ var matchStages = target.pipeline.map(function (stage) { return ({ stage: stage, base: stage.$match }); }).filter(function (entry) { return entry.base; });
855
+ if (!matchStages.length) {
856
+ return buildLookupPipelineFromStack(target.pipeline, [], rootDoc, target.let);
857
+ }
858
+ var letValuesResult = resolveLetVariables(rootDoc, target.let);
859
+ if (!letValuesResult.resolved) {
860
+ return { filters: [], unresolved: true };
861
+ }
862
+ var combinedMatches = [];
863
+ try {
864
+ for (var matchStages_1 = __values(matchStages), matchStages_1_1 = matchStages_1.next(); !matchStages_1_1.done; matchStages_1_1 = matchStages_1.next()) {
865
+ var base = matchStages_1_1.value.base;
866
+ var substitutionState = { unresolved: false };
867
+ var substituted = substituteLetVariables(base, letValuesResult.values, rootDoc, substitutionState);
868
+ if (substitutionState.unresolved || containsUnresolvedLetReference(substituted)) {
869
+ return { filters: [], unresolved: true };
870
+ }
871
+ combinedMatches.push(substituted);
872
+ }
873
+ }
874
+ catch (e_25_1) { e_25 = { error: e_25_1 }; }
875
+ finally {
876
+ try {
877
+ if (matchStages_1_1 && !matchStages_1_1.done && (_a = matchStages_1.return)) _a.call(matchStages_1);
878
+ }
879
+ finally { if (e_25) throw e_25.error; }
880
+ }
881
+ if (!combinedMatches.length) {
882
+ return buildLookupPipelineFromStack(target.pipeline, [], rootDoc, target.let);
883
+ }
884
+ if (combinedMatches.length === 1) {
885
+ return { filters: [combinedMatches[0]], unresolved: false };
886
+ }
887
+ return { filters: [{ $and: combinedMatches }], unresolved: false };
888
+ }
889
+ function buildLookupPipelineFromStack(pipeline, initialFilters, rootDoc, letMap) {
890
+ var currentFilters = initialFilters.slice();
891
+ var unresolved = false;
892
+ var entries = pipeline.map(function (stage) { return stage ? Object.keys(stage)[0] : null; });
893
+ for (var i = 0; i < pipeline.length; i++) {
894
+ var stage = pipeline[i];
895
+ var stageType = entries[i];
896
+ if (stageType === '$match' && stage.$match) {
897
+ var letValuesResult = resolveLetVariables(rootDoc, letMap);
898
+ if (!letValuesResult.resolved) {
899
+ unresolved = true;
900
+ break;
901
+ }
902
+ var substitutionState = { unresolved: false };
903
+ var substituted = substituteLetVariables(stage.$match, letValuesResult.values, rootDoc, substitutionState);
904
+ if (substitutionState.unresolved || containsUnresolvedLetReference(substituted)) {
905
+ unresolved = true;
906
+ break;
907
+ }
908
+ currentFilters.push(substituted);
909
+ }
910
+ else if (stageType === '$match') {
911
+ unresolved = true;
912
+ break;
913
+ }
914
+ else if (stageType === '$facet') {
915
+ unresolved = true;
916
+ break;
917
+ }
918
+ }
919
+ if (!currentFilters.length) {
920
+ return { filters: [], unresolved: unresolved };
921
+ }
922
+ if (currentFilters.length === 1) {
923
+ return { filters: currentFilters, unresolved: unresolved };
924
+ }
925
+ return { filters: [{ $and: currentFilters }], unresolved: unresolved };
926
+ }
927
+ function resolveLetVariables(rootDoc, letMap) {
928
+ var e_26, _a;
929
+ if (!letMap || typeof letMap !== 'object') {
930
+ return { resolved: true, values: {} };
931
+ }
932
+ var values = {};
933
+ try {
934
+ for (var _b = __values(Object.entries(letMap)), _c = _b.next(); !_c.done; _c = _b.next()) {
935
+ var _d = __read(_c.value, 2), key = _d[0], expression = _d[1];
936
+ var evaluation = evaluateLetExpression(rootDoc, expression);
937
+ if (!evaluation.resolved) {
938
+ return { resolved: false, values: null };
939
+ }
940
+ values[key] = evaluation.value;
941
+ }
942
+ }
943
+ catch (e_26_1) { e_26 = { error: e_26_1 }; }
944
+ finally {
945
+ try {
946
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
947
+ }
948
+ finally { if (e_26) throw e_26.error; }
949
+ }
950
+ return { resolved: true, values: values };
951
+ }
952
+ function evaluateLetExpression(rootDoc, expression) {
953
+ if (typeof expression === 'string') {
954
+ if (expression.startsWith('$$ROOT.')) {
955
+ var path = expression.slice('$$ROOT.'.length);
956
+ return { resolved: true, value: getValueByPath(rootDoc, path) };
957
+ }
958
+ else if (expression === '$$ROOT') {
959
+ return { resolved: true, value: rootDoc };
960
+ }
961
+ else if (expression.startsWith('$$')) {
962
+ return { resolved: false, value: null };
963
+ }
964
+ else if (expression.startsWith('$')) {
965
+ var path = expression.slice(1);
966
+ return { resolved: true, value: getValueByPath(rootDoc, path) };
967
+ }
968
+ else {
969
+ return { resolved: true, value: expression };
970
+ }
971
+ }
972
+ else if (Array.isArray(expression)) {
973
+ return { resolved: false, value: null };
974
+ }
975
+ else if (expression && typeof expression === 'object') {
976
+ return { resolved: false, value: null };
977
+ }
978
+ return { resolved: true, value: expression };
979
+ }
980
+ function substituteLetVariables(node, letValues, rootDoc, state) {
981
+ var e_27, _a;
982
+ if (Array.isArray(node)) {
983
+ return node.map(function (item) { return substituteLetVariables(item, letValues, rootDoc, state); });
984
+ }
985
+ if (node && typeof node === 'object') {
986
+ var output = {};
987
+ try {
988
+ for (var _b = __values(Object.entries(node)), _c = _b.next(); !_c.done; _c = _b.next()) {
989
+ var _d = __read(_c.value, 2), key = _d[0], value = _d[1];
990
+ output[key] = substituteLetVariables(value, letValues, rootDoc, state);
991
+ }
992
+ }
993
+ catch (e_27_1) { e_27 = { error: e_27_1 }; }
994
+ finally {
995
+ try {
996
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
997
+ }
998
+ finally { if (e_27) throw e_27.error; }
999
+ }
1000
+ return output;
1001
+ }
1002
+ if (typeof node === 'string') {
1003
+ if (node.startsWith('$$ROOT.')) {
1004
+ var path = node.slice('$$ROOT.'.length);
1005
+ return getValueByPath(rootDoc, path);
1006
+ }
1007
+ if (node === '$$ROOT') {
1008
+ return rootDoc;
1009
+ }
1010
+ if (node.startsWith('$$')) {
1011
+ var varName = node.slice(2);
1012
+ if (Object.prototype.hasOwnProperty.call(letValues, varName)) {
1013
+ return letValues[varName];
1014
+ }
1015
+ state.unresolved = true;
1016
+ return node;
1017
+ }
1018
+ }
1019
+ return node;
1020
+ }
1021
+ function containsUnresolvedLetReference(node) {
1022
+ if (typeof node === 'string') {
1023
+ return node.includes('$$');
1024
+ }
1025
+ if (Array.isArray(node)) {
1026
+ return node.some(function (item) { return containsUnresolvedLetReference(item); });
1027
+ }
1028
+ if (node && typeof node === 'object') {
1029
+ return Object.values(node).some(function (value) { return containsUnresolvedLetReference(value); });
1030
+ }
1031
+ return false;
1032
+ }
1033
+ function getValueByPath(source, path) {
1034
+ var e_28, _a;
1035
+ if (!source || !path) {
1036
+ return undefined;
1037
+ }
1038
+ var segments = path.split('.');
1039
+ var current = source;
1040
+ try {
1041
+ for (var segments_1 = __values(segments), segments_1_1 = segments_1.next(); !segments_1_1.done; segments_1_1 = segments_1.next()) {
1042
+ var segment = segments_1_1.value;
1043
+ if (current === null || current === undefined) {
1044
+ return undefined;
1045
+ }
1046
+ if (Array.isArray(current)) {
1047
+ var index = parseInt(segment, 10);
1048
+ if (isNaN(index) || index < 0 || index >= current.length) {
1049
+ return undefined;
1050
+ }
1051
+ current = current[index];
1052
+ }
1053
+ else {
1054
+ current = current[segment];
1055
+ }
1056
+ }
1057
+ }
1058
+ catch (e_28_1) { e_28 = { error: e_28_1 }; }
1059
+ finally {
1060
+ try {
1061
+ if (segments_1_1 && !segments_1_1.done && (_a = segments_1.return)) _a.call(segments_1);
1062
+ }
1063
+ finally { if (e_28) throw e_28.error; }
1064
+ }
1065
+ return current;
1066
+ }
1067
+ function canonicalizeFilter(filter) {
1068
+ return JSON.stringify(sortKeys(filter));
1069
+ }
1070
+ function sortKeys(value) {
1071
+ var e_29, _a;
1072
+ if (Array.isArray(value)) {
1073
+ return value.map(function (item) { return sortKeys(item); });
1074
+ }
1075
+ if (value && typeof value === 'object') {
1076
+ var sorted = {};
1077
+ var keys = Object.keys(value).sort();
1078
+ try {
1079
+ for (var keys_1 = __values(keys), keys_1_1 = keys_1.next(); !keys_1_1.done; keys_1_1 = keys_1.next()) {
1080
+ var key = keys_1_1.value;
1081
+ sorted[key] = sortKeys(value[key]);
1082
+ }
1083
+ }
1084
+ catch (e_29_1) { e_29 = { error: e_29_1 }; }
1085
+ finally {
1086
+ try {
1087
+ if (keys_1_1 && !keys_1_1.done && (_a = keys_1.return)) _a.call(keys_1);
1088
+ }
1089
+ finally { if (e_29) throw e_29.error; }
1090
+ }
1091
+ return sorted;
1092
+ }
1093
+ return value;
1094
+ }
1095
+ function dedupeFilters(filters) {
1096
+ var e_30, _a;
1097
+ var map = new Map();
1098
+ try {
1099
+ for (var filters_1 = __values(filters), filters_1_1 = filters_1.next(); !filters_1_1.done; filters_1_1 = filters_1.next()) {
1100
+ var filter = filters_1_1.value;
1101
+ var key = canonicalizeFilter(filter);
1102
+ if (!map.has(key)) {
1103
+ map.set(key, filter);
1104
+ }
1105
+ }
1106
+ }
1107
+ catch (e_30_1) { e_30 = { error: e_30_1 }; }
1108
+ finally {
1109
+ try {
1110
+ if (filters_1_1 && !filters_1_1.done && (_a = filters_1.return)) _a.call(filters_1);
1111
+ }
1112
+ finally { if (e_30) throw e_30.error; }
1113
+ }
1114
+ return Array.from(map.values());
1115
+ }
746
1116
 
747
1117
  //# sourceMappingURL=subscription-dependency-context.js.map