@omegup/msync 0.0.31 → 0.0.32-test1

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.
Files changed (89) hide show
  1. package/index.d.ts +2 -2
  2. package/index.esm.js +140 -114
  3. package/index.js +140 -114
  4. package/package.json +1 -1
  5. package/lib/accumulators/index.d.ts +0 -10
  6. package/lib/aggregate/$merge.d.ts +0 -10
  7. package/lib/aggregate/$upsert.d.ts +0 -11
  8. package/lib/aggregate/group/$group-merge.d.ts +0 -12
  9. package/lib/aggregate/group/index.d.ts +0 -2
  10. package/lib/aggregate/group/utils/sub-group.d.ts +0 -6
  11. package/lib/aggregate/group/utils/sub-merge.d.ts +0 -15
  12. package/lib/aggregate/index.d.ts +0 -8
  13. package/lib/aggregate/lookup/$lookup-delta.d.ts +0 -10
  14. package/lib/aggregate/lookup/$lookup-raw.d.ts +0 -9
  15. package/lib/aggregate/lookup/index.d.ts +0 -11
  16. package/lib/aggregate/match/$match-delta.d.ts +0 -3
  17. package/lib/aggregate/match/$match.d.ts +0 -3
  18. package/lib/aggregate/match/index.d.ts +0 -2
  19. package/lib/aggregate/mongo-stages.d.ts +0 -13
  20. package/lib/aggregate/out.d.ts +0 -34
  21. package/lib/aggregate/prefix.d.ts +0 -33
  22. package/lib/aggregate/raws.d.ts +0 -17
  23. package/lib/aggregate/set/$replace-with-each.d.ts +0 -12
  24. package/lib/aggregate/set/$set-delta.d.ts +0 -5
  25. package/lib/aggregate/set/$set.d.ts +0 -6
  26. package/lib/aggregate/set/index.d.ts +0 -1
  27. package/lib/aggregate/unwind/$unwind-delta.d.ts +0 -5
  28. package/lib/aggregate/unwind/index.d.ts +0 -5
  29. package/lib/boot/boot-simpl.d.ts +0 -14
  30. package/lib/boot/boot.d.ts +0 -36
  31. package/lib/boot/index.d.ts +0 -2
  32. package/lib/expression/arith.d.ts +0 -26
  33. package/lib/expression/array.d.ts +0 -25
  34. package/lib/expression/concat.d.ts +0 -30
  35. package/lib/expression/date.d.ts +0 -17
  36. package/lib/expression/expr-base.d.ts +0 -5
  37. package/lib/expression/index.d.ts +0 -7
  38. package/lib/expression/logic.d.ts +0 -22
  39. package/lib/expression/range.d.ts +0 -6
  40. package/lib/expression/val.d.ts +0 -25
  41. package/lib/field/field.d.ts +0 -25
  42. package/lib/field/index.d.ts +0 -1
  43. package/lib/machine.d.ts +0 -9
  44. package/lib/predicate/$eq.d.ts +0 -13
  45. package/lib/predicate/$expr.d.ts +0 -3
  46. package/lib/predicate/$in.d.ts +0 -2
  47. package/lib/predicate/index.d.ts +0 -3
  48. package/lib/predicate/utils.d.ts +0 -5
  49. package/lib/query/index.d.ts +0 -1
  50. package/lib/query/logic.d.ts +0 -11
  51. package/lib/stream/aggregate.d.ts +0 -7
  52. package/lib/types/accumulator.d.ts +0 -30
  53. package/lib/types/aggregate.d.ts +0 -9
  54. package/lib/types/expr.d.ts +0 -27
  55. package/lib/types/extern.d.ts +0 -3
  56. package/lib/types/index.d.ts +0 -8
  57. package/lib/types/lookup.d.ts +0 -19
  58. package/lib/types/machine.d.ts +0 -17
  59. package/lib/types/predicate.d.ts +0 -14
  60. package/lib/types/query.d.ts +0 -12
  61. package/lib/types/stream.d.ts +0 -67
  62. package/lib/update/index.d.ts +0 -1
  63. package/lib/update/updater.d.ts +0 -19
  64. package/lib/utils/before.d.ts +0 -3
  65. package/lib/utils/coll.d.ts +0 -5
  66. package/lib/utils/db-indexes.d.ts +0 -4
  67. package/lib/utils/guard.d.ts +0 -18
  68. package/lib/utils/index.d.ts +0 -3
  69. package/lib/utils/json.d.ts +0 -9
  70. package/lib/utils/log.d.ts +0 -1
  71. package/lib/utils/map-object.d.ts +0 -41
  72. package/lib/utils/merge/combiners.d.ts +0 -6
  73. package/lib/utils/merge/index.d.ts +0 -2
  74. package/lib/utils/merge/merge.d.ts +0 -7
  75. package/lib/utils/merge/next-winner.d.ts +0 -3
  76. package/lib/utils/merge/types.d.ts +0 -19
  77. package/lib/utils/merge/utils.d.ts +0 -5
  78. package/lib/utils/tear-down.d.ts +0 -2
  79. package/lib/watch.d.ts +0 -15
  80. package/test/mongodb.d.ts +0 -9
  81. package/test/uri.d.ts +0 -1
  82. package/types/class.d.ts +0 -1
  83. package/types/extern.d.ts +0 -3
  84. package/types/global.d.ts +0 -50
  85. package/types/hkt.d.ts +0 -56
  86. package/types/index.d.ts +0 -6
  87. package/types/json.d.ts +0 -54
  88. package/types/mongo.d.ts +0 -24
  89. package/types/view.d.ts +0 -35
package/index.d.ts CHANGED
@@ -55,7 +55,7 @@ declare const root: <T extends O>() => Field<T, T, unknown>;
55
55
  declare const ctx: <T>() => <K extends string>(k: K) => Field<unknown, T, RORec<K, T>>;
56
56
 
57
57
  declare const QueryRaw: unique symbol;
58
- type QueryRaw<T, C> = RawObj & {
58
+ type QueryRaw<T, C = unknown> = RawObj & {
59
59
  [Type]?(x: typeof QueryRaw, y: T, c: C): void;
60
60
  };
61
61
  declare const Query: unique symbol;
@@ -478,7 +478,7 @@ declare const $set: <V extends O>() => <R extends O, C = unknown>(fields: MapO<V
478
478
  declare const $replaceWith: <T extends O, V extends O>(expr: Expr<V, T>) => DeltaStages<O, T, V> & LinStages<O, T, V>;
479
479
 
480
480
  type s$1 = string;
481
- declare const $unwindDelta: <K1 extends s$1, T extends doc, K2 extends s$1, U extends doc>(k1: AsLiteral<K1>, k2: AsLiteral<K2>, k: K1 | K2 | false) => RawStages<Delta<Rec<K1, T>>, Delta<Rec<K1, T> & Rec<K2, Arr<U>>>, Delta<Rec<K1, T> & Rec<K2, U> & ID>>;
481
+ declare const $unwindDelta: <K1 extends s$1, T extends doc, K2 extends s$1, U extends doc, Null extends null = never>(k1: AsLiteral<K1>, k2: AsLiteral<K2>, k: K1 | K2 | false, includeNull?: Null) => RawStages<Delta<Rec<K1, T>>, Delta<Rec<K1, T> & Rec<K2, Arr<U>>>, Delta<Rec<K1, T> & Rec<K2, U | Null> & ID>>;
482
482
 
483
483
  type s = string;
484
484
  declare const $unwind: <T extends doc, K extends s, U extends doc>(k: AsLiteral<K>, dict: RORec<K, "key">) => DeltaStages<O, T & Rec<K, Arr<U>>, T & Rec<K, U>>;
package/index.esm.js CHANGED
@@ -465,7 +465,12 @@ const $replaceWith1 = (expr) => f => {
465
465
  { $replaceWith: parts.reduce((v, k) => ({ [k]: v }), expr.raw(f()).get()) },
466
466
  ]);
467
467
  };
468
- const $unwind1 = (k) => f => asStages([{ $unwind: `$${f().of(k).str()}` }]);
468
+ const $unwind1 = (k, includeNull) => f => {
469
+ const path = `$${f().of(k).str()}`;
470
+ return asStages([
471
+ { $unwind: path },
472
+ ]);
473
+ };
469
474
  const $group1 = (id, args) => (f) => asStages([
470
475
  {
471
476
  $group: {
@@ -496,7 +501,7 @@ const $simpleLookup1 = (args) => f => {
496
501
  const $match_ = (query) => $match1(query)(root);
497
502
  const $set_ = (updater) => $set1(updater)(root);
498
503
  const $replaceWith_ = (expr) => $replaceWith1(expr)(root);
499
- const $unwind_ = (k) => $unwind1(k)(root);
504
+ const $unwind_ = (k, includeNull) => $unwind1(k)(root);
500
505
  const $group_ = () => (id, args) => $group1(id, args)(root);
501
506
  const $project_ = $project1;
502
507
  const $simpleLookup_ = (args) => $simpleLookup1(args)(root);
@@ -612,29 +617,6 @@ const $groupMerge = (id, args, out, gid, extra, idPrefix = '') => ({
612
617
  const $groupId = (id, args, out, extra) => $groupMerge(id, args, { into: out, whenNotMatched: 'fail' }, '_id', extra);
613
618
  const $group = (id, args, out, extra, idPrefix = '') => $groupMerge(id, args, { into: out, whenNotMatched: 'insert' }, '_grp', extra, idPrefix);
614
619
 
615
- const $expr = (expr) => ({
616
- raw: f => ({ $expr: expr.raw(f).get() }),
617
- });
618
-
619
- const $lookupRaw = ({ field1, field2 }, { coll, exec, input }, k2, k) => (f) => {
620
- root().of('_id').expr();
621
- root().of(k2).of('_id').expr();
622
- return link()
623
- .with($simpleLookup1({
624
- coll,
625
- k: k2,
626
- vars: map1('local', root().with(field1).expr()),
627
- pipeline: link()
628
- .with(input)
629
- .with($match_($expr(eq(ctx()('local').expr())(root().of('before').with(field2).expr()))))
630
- .with(exec)
631
- .with($replaceWith_(root().of('before').expr())).stages,
632
- })(f))
633
- .with($unwind1(k2)(f))
634
- .with(link().stages
635
- ).stages;
636
- };
637
-
638
620
  const deltaExpr = (expr) => (field) => {
639
621
  return ite(eqTyped($ifNull(root().of(field).expr(), nil), nil), nil, expr(field));
640
622
  };
@@ -664,7 +646,82 @@ const $setDelta = (updater) => {
664
646
  .with($setEach(k => to(nil), dict)).stages;
665
647
  };
666
648
 
667
- const $unwindDelta = (k1, k2, k) => {
649
+ const $unwindDelta = (k1, k2, k, includeNull) => {
650
+ const outer = includeNull === null;
651
+ const newItems = {
652
+ $filter: {
653
+ input: { $ifNull: [`$after.${k2}`, []] },
654
+ as: 'a',
655
+ cond: {
656
+ $not: {
657
+ $in: [
658
+ '$$a._id',
659
+ {
660
+ $map: {
661
+ input: { $ifNull: [`$before.${k2}`, []] },
662
+ as: 'b',
663
+ in: '$$b._id',
664
+ },
665
+ },
666
+ ],
667
+ },
668
+ },
669
+ },
670
+ };
671
+ const oldItems = {
672
+ $filter: {
673
+ input: {
674
+ $map: {
675
+ input: { $ifNull: [`$before.${k2}`, []] },
676
+ as: 'b',
677
+ in: {
678
+ before: '$$b',
679
+ after: {
680
+ $ifNull: [
681
+ {
682
+ $first: {
683
+ $filter: {
684
+ input: `$after.${k2}`,
685
+ as: 'a',
686
+ cond: { $eq: ['$$a._id', '$$b._id'] },
687
+ },
688
+ },
689
+ },
690
+ null,
691
+ ],
692
+ },
693
+ },
694
+ },
695
+ },
696
+ as: 'a',
697
+ cond: { $ne: ['$$a.before', '$$a.after'] },
698
+ },
699
+ };
700
+ const ifNull = (k, part, str = `$${[k1, k2].sort()[0]}.${part}._id`) => outer && k == k2 ? { $ifNull: [str, 'null'] } : str;
701
+ const interDot = ([a, b]) => [a, '.', b];
702
+ const partReplace = (part) => ({
703
+ $cond: {
704
+ if: { $or: [{ $eq: [`$${k1}.${part}`, null] }, { $eq: [`$${k2}.${part}`, null] }] },
705
+ then: null,
706
+ else: {
707
+ _id: k
708
+ ? `$${k}.${part}._id`
709
+ : {
710
+ $concat: interDot([k1, k2].sort().map(k => ifNull(k, part))),
711
+ },
712
+ [k1]: `$${k1}.${part}`,
713
+ [k2]: outer
714
+ ? {
715
+ $cond: {
716
+ if: `$${k2}.${part}._id`,
717
+ then: `$${k2}.${part}`,
718
+ else: null,
719
+ },
720
+ }
721
+ : `$${k2}.${part}`,
722
+ },
723
+ },
724
+ });
668
725
  const stages = link()
669
726
  .with(asStages([
670
727
  {
@@ -675,51 +732,34 @@ const $unwindDelta = (k1, k2, k) => {
675
732
  },
676
733
  [k2]: {
677
734
  $concatArrays: [
678
- {
679
- $map: {
680
- input: { $ifNull: [`$before.${k2}`, []] },
681
- as: 'b',
682
- in: {
683
- before: '$$b',
684
- after: {
685
- $ifNull: [
686
- {
687
- $first: {
688
- $filter: {
689
- input: `$after.${k2}`,
690
- as: 'a',
691
- cond: { $eq: ['$$a._id', '$$b._id'] },
692
- },
693
- },
694
- },
695
- null,
696
- ],
735
+ outer
736
+ ? {
737
+ $cond: {
738
+ if: { $eq: [`$before.${k2}`, []] },
739
+ then: {
740
+ $cond: {
741
+ if: { $eq: [`$after.${k2}`, []] },
742
+ then: [],
743
+ else: [{ before: {}, after: null }],
744
+ },
697
745
  },
746
+ else: oldItems,
698
747
  },
699
- },
700
- },
748
+ }
749
+ : oldItems,
701
750
  {
702
751
  $map: {
703
- input: {
704
- $filter: {
705
- input: { $ifNull: [`$after.${k2}`, []] },
706
- as: 'a',
707
- cond: {
708
- $not: {
709
- $in: [
710
- '$$a._id',
711
- {
712
- $map: {
713
- input: { $ifNull: [`$before.${k2}`, []] },
714
- as: 'b',
715
- in: '$$b._id',
716
- },
717
- },
718
- ],
752
+ input: outer
753
+ ? {
754
+ $cond: {
755
+ if: {
756
+ $and: [{ $ne: [`$before.${k2}`, []] }, { $eq: [`$after.${k2}`, []] }],
719
757
  },
758
+ then: [{}],
759
+ else: newItems,
720
760
  },
721
- },
722
- },
761
+ }
762
+ : newItems,
723
763
  as: 'a',
724
764
  in: { before: null, after: '$$a' },
725
765
  },
@@ -733,45 +773,9 @@ const $unwindDelta = (k1, k2, k) => {
733
773
  .with(asStages([
734
774
  {
735
775
  $replaceWith: {
736
- _id: "$_id",
737
- before: {
738
- $cond: {
739
- if: { $or: [{ $eq: [`$${k1}.before`, null] }, { $eq: [`$${k2}.before`, null] }] },
740
- then: null,
741
- else: {
742
- _id: {
743
- $concat: k
744
- ? `$${k}.before._id`
745
- : [
746
- `$${[k1, k2].sort()[0]}.before._id`,
747
- '.',
748
- `$${[k1, k2].sort()[1]}.before._id`,
749
- ],
750
- },
751
- [k1]: `$${k1}.before`,
752
- [k2]: `$${k2}.before`,
753
- },
754
- },
755
- },
756
- after: {
757
- $cond: {
758
- if: { $or: [{ $eq: [`$${k1}.after`, null] }, { $eq: [`$${k2}.after`, null] }] },
759
- then: null,
760
- else: {
761
- _id: {
762
- $concat: k
763
- ? `$${k}.after._id`
764
- : [
765
- `$${[k1, k2].sort()[0]}.after._id`,
766
- '.',
767
- `$${[k1, k2].sort()[1]}.after._id`,
768
- ],
769
- },
770
- [k1]: `$${k1}.after`,
771
- [k2]: `$${k2}.after`,
772
- },
773
- },
774
- },
776
+ _id: '$_id',
777
+ before: partReplace('before'),
778
+ after: partReplace('after'),
775
779
  },
776
780
  },
777
781
  ])).stages;
@@ -791,7 +795,7 @@ const $unwind = (k, dict) => ({
791
795
  raw: $unwind1(k),
792
796
  });
793
797
 
794
- const $lookupDelta = ({ field1, field2 }, { coll, exec, input }, k1, k2, k) => {
798
+ const $lookupDelta = ({ field1, field2 }, { coll, exec, input }, k1, k2, k, includeNull) => {
795
799
  return link()
796
800
  .with($replaceWithDelta(field(map1(k1, root().expr()))))
797
801
  .with($simpleLookup_({
@@ -820,8 +824,30 @@ const $lookupDelta = ({ field1, field2 }, { coll, exec, input }, k1, k2, k) => {
820
824
  const a = root().of(f1).of('before').expr();
821
825
  const part = root().of(f);
822
826
  return ite(eq(root().of(f).expr())(nil), nil, field(omit.backward(mergeExpr(omit.forward(map1(k2, a)), map1(k1, part.of(k1).expr())))));
823
- }))
824
- .with($unwindDelta(k1, k2, k)).stages;
827
+ })).with($unwindDelta(k1, k2, k, includeNull)).stages;
828
+ };
829
+
830
+ const $expr = (expr) => ({
831
+ raw: f => ({ $expr: expr.raw(f).get() }),
832
+ });
833
+
834
+ const $lookupRaw = ({ field1, field2 }, { coll, exec, input }, k2, k) => (f) => {
835
+ root().of('_id').expr();
836
+ root().of(k2).of('_id').expr();
837
+ return link()
838
+ .with($simpleLookup1({
839
+ coll,
840
+ k: k2,
841
+ vars: map1('local', root().with(field1).expr()),
842
+ pipeline: link()
843
+ .with(input)
844
+ .with($match_($expr(eq(ctx()('local').expr())(root().of('before').with(field2).expr()))))
845
+ .with(exec)
846
+ .with($replaceWith_(root().of('before').expr())).stages,
847
+ })(f))
848
+ .with($unwind1(k2)(f))
849
+ .with(link().stages
850
+ ).stages;
825
851
  };
826
852
 
827
853
  const asBefore = (f) => f(() => root().of('before'));
@@ -937,9 +963,9 @@ const runCont = async ({ next }, cb) => {
937
963
  };
938
964
 
939
965
  const merge = ({ lsource: L, rsource: R, }) => mergeIterators({ sources: { L, R } });
940
- const join = ({ lField, rField, left, right, as }, leftSnapshot, rightSnapshot, stagesUntilNextLookup) => {
941
- createIndex(leftSnapshot.coll, { [lField.str()]: 1 }).catch(e => e.code == 86 || Promise.reject(e));
942
- createIndex(rightSnapshot.coll, { [rField.str()]: 1 }).catch(e => e.code == 86 || Promise.reject(e));
966
+ const join = ({ lField, rField, left, right, as }, leftSnapshot, rightSnapshot, stagesUntilNextLookup, includeNull) => {
967
+ createIndex(leftSnapshot.coll, { [`before.${lField.str()}`]: 1 }).catch(e => e.code == 86 || Promise.reject(e));
968
+ createIndex(rightSnapshot.coll, { [`before.${rField.str()}`]: 1 }).catch(e => e.code == 86 || Promise.reject(e));
943
969
  const rightJoinField = { field1: lField, field2: rField };
944
970
  const joinId = 'left';
945
971
  const joinR_Snapshot = asBefore($lookupRaw(rightJoinField, rightSnapshot, as));
@@ -952,7 +978,7 @@ const join = ({ lField, rField, left, right, as }, leftSnapshot, rightSnapshot,
952
978
  out: (finalInput) => {
953
979
  const leftJoinField = { field1: rField, field2: lField };
954
980
  const joinL_Delta = $lookupDelta(leftJoinField, leftSnapshot, 'right', 'left', joinId);
955
- const joinR_Delta = $lookupDelta(rightJoinField, rightSnapshot, 'left', 'right', joinId);
981
+ const joinR_Delta = $lookupDelta(rightJoinField, rightSnapshot, 'left', 'right', joinId, includeNull);
956
982
  const mergeForeignIntoDoc = concatStages($replaceWithDelta(mergeObjects(root().of('left').expr(), fieldM({ a: root().of('right').expr(), b: root().of('_id').expr() }, dictId))), stagesUntilNextLookup.delta);
957
983
  const lRunnerInput = concatStages(joinR_Delta, mergeForeignIntoDoc);
958
984
  const rRunnerInput = concatStages(joinL_Delta, mergeForeignIntoDoc);
@@ -1186,7 +1212,7 @@ const makeWatchStream = (db, { collection, projection: p, hardMatch: m }, startA
1186
1212
  };
1187
1213
 
1188
1214
  const actions = {
1189
- updateMany: (c, args) => [c.updateMany(...args), [`db['${c.collectionName}'].updateMany(`, ...args, ')']],
1215
+ updateMany: (c, args) => [c.updateMany(...args), [`db['${c.collectionName}'].updateMany(...`, args, ')']],
1190
1216
  };
1191
1217
  const streamNames = {};
1192
1218
  const executes$1 = (view, input, streamName) => {
@@ -1266,9 +1292,9 @@ const executes$1 = (view, input, streamName) => {
1266
1292
  params: p,
1267
1293
  };
1268
1294
  const [action, out] = actions[method](collection, params);
1269
- log('teardown', `db.['${snapshotCollection.collectionName}'].drop()`, ...out);
1295
+ log('teardown', `db['${snapshotCollection.collectionName}'].drop()`, ...out);
1270
1296
  await Promise.all([snapshotCollection.drop(), action]);
1271
- log('teardown done', `db.['${snapshotCollection.collectionName}'].drop()`, ...out);
1297
+ log('teardown done', `db['${snapshotCollection.collectionName}'].drop()`, ...out);
1272
1298
  };
1273
1299
  if (exists && !same)
1274
1300
  await handleTeardown(exists);