@omegup/msync 0.1.9 → 0.1.11

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 (92) hide show
  1. package/index.d.ts +6 -4
  2. package/index.esm.js +211 -212
  3. package/index.js +211 -212
  4. package/package.json +1 -1
  5. package/lib/accumulators/index.d.ts +0 -10
  6. package/lib/aggregate/$merge.d.ts +0 -27
  7. package/lib/aggregate/$upsert.d.ts +0 -15
  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 -18
  12. package/lib/aggregate/index.d.ts +0 -9
  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 -12
  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 -37
  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 -13
  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 -6
  29. package/lib/boot/boot-simpl.d.ts +0 -14
  30. package/lib/boot/boot-utils.d.ts +0 -26
  31. package/lib/boot/boot.d.ts +0 -16
  32. package/lib/boot/first-stages.d.ts +0 -12
  33. package/lib/boot/index.d.ts +0 -3
  34. package/lib/boot/single.d.ts +0 -11
  35. package/lib/expression/arith.d.ts +0 -32
  36. package/lib/expression/array.d.ts +0 -30
  37. package/lib/expression/concat.d.ts +0 -34
  38. package/lib/expression/date.d.ts +0 -17
  39. package/lib/expression/expr-base.d.ts +0 -5
  40. package/lib/expression/index.d.ts +0 -7
  41. package/lib/expression/logic.d.ts +0 -23
  42. package/lib/expression/range.d.ts +0 -11
  43. package/lib/expression/val.d.ts +0 -25
  44. package/lib/field/field.d.ts +0 -25
  45. package/lib/field/index.d.ts +0 -1
  46. package/lib/machine.d.ts +0 -9
  47. package/lib/predicate/$eq.d.ts +0 -24
  48. package/lib/predicate/$expr.d.ts +0 -3
  49. package/lib/predicate/$in.d.ts +0 -2
  50. package/lib/predicate/index.d.ts +0 -3
  51. package/lib/predicate/utils.d.ts +0 -5
  52. package/lib/query/index.d.ts +0 -1
  53. package/lib/query/logic.d.ts +0 -11
  54. package/lib/stream/aggregate.d.ts +0 -7
  55. package/lib/types/accumulator.d.ts +0 -30
  56. package/lib/types/aggregate.d.ts +0 -9
  57. package/lib/types/expr.d.ts +0 -27
  58. package/lib/types/extern.d.ts +0 -3
  59. package/lib/types/index.d.ts +0 -8
  60. package/lib/types/lookup.d.ts +0 -19
  61. package/lib/types/machine.d.ts +0 -17
  62. package/lib/types/predicate.d.ts +0 -11
  63. package/lib/types/query.d.ts +0 -12
  64. package/lib/types/stream.d.ts +0 -71
  65. package/lib/update/index.d.ts +0 -1
  66. package/lib/update/updater.d.ts +0 -19
  67. package/lib/utils/before.d.ts +0 -3
  68. package/lib/utils/coll.d.ts +0 -5
  69. package/lib/utils/db-indexes.d.ts +0 -4
  70. package/lib/utils/guard.d.ts +0 -19
  71. package/lib/utils/index.d.ts +0 -4
  72. package/lib/utils/json.d.ts +0 -9
  73. package/lib/utils/log.d.ts +0 -1
  74. package/lib/utils/map-object.d.ts +0 -41
  75. package/lib/utils/merge/combiners.d.ts +0 -6
  76. package/lib/utils/merge/index.d.ts +0 -2
  77. package/lib/utils/merge/merge.d.ts +0 -7
  78. package/lib/utils/merge/next-winner.d.ts +0 -3
  79. package/lib/utils/merge/types.d.ts +0 -19
  80. package/lib/utils/merge/utils.d.ts +0 -4
  81. package/lib/utils/tear-down.d.ts +0 -2
  82. package/lib/watch.d.ts +0 -15
  83. package/test/mongodb.d.ts +0 -9
  84. package/test/uri.d.ts +0 -1
  85. package/types/class.d.ts +0 -1
  86. package/types/extern.d.ts +0 -3
  87. package/types/global.d.ts +0 -50
  88. package/types/hkt.d.ts +0 -56
  89. package/types/index.d.ts +0 -6
  90. package/types/json.d.ts +0 -54
  91. package/types/mongo.d.ts +0 -24
  92. package/types/view.d.ts +0 -35
package/index.d.ts CHANGED
@@ -412,7 +412,7 @@ declare const $entries: <D extends O, V, C = unknown>(expr: Expr<Rec<string, Rec
412
412
 
413
413
  type MergeInto<T extends O, Out extends O, E = unknown> = {
414
414
  whenNotMatched: 'insert';
415
- into: RWCollection<T, Out>;
415
+ into: RWCollection<T, Out> & E;
416
416
  } | {
417
417
  whenNotMatched: 'discard' | 'fail';
418
418
  into: ReadonlyCollection<Out> & E;
@@ -455,12 +455,14 @@ type Loose<Grp, VV, GG extends string> = Prepare<Grp, GG> & Par<V<VV, GG>>;
455
455
  type Strict<Grp, VV, GG extends string, EE> = Prepare<Grp, GG> & V<VV, GG> & Omit<EE, IdAndTsKeys | GI$1<GG> | keyof V<VV, GG>>;
456
456
  type V_Grp<VV, GG extends string, Grp> = Rec<GI$1<GG>, Grp> & V<VV, GG>;
457
457
  type Extra<EE, VV, GG extends string> = Omit<EE, IdAndTsKeys | GI$1<GG> | keyof V<VV, GG>>;
458
+ type OrReplace<T, V> = T | Replace<T, V>;
459
+ type MergedInput<Out, VV, Grp, GG extends string, EE> = OrReplace<Replace<Replace<Out, V<VV, GG>>, Extra<EE, VV, GG>> & Model, TS & ID & V_Grp<VV, GG, Grp> & Extra<EE, VV, GG>>;
458
460
 
459
461
  type Denied<GID = never> = keyof (TS & ID) | GID;
460
462
  type GI<GG> = Exclude<GG, keyof TS>;
461
- declare const $groupMerge: <T extends O, Grp extends notArr, V extends O, GG extends string, EE = {}, Out extends Loose<Grp, V, GG> = Loose<Grp, V, GG>>(id: Expr<Grp, T>, args: DeltaAccumulators<T, O & Omit<V, Denied<GI<GG>>>>, out: MergeInto<Strict<Grp, V, GG, EE>, Out, WriteonlyCollection<Replace<Out, Strict<Grp, V, GG, EE>>>>, gid: AsLiteral<GI<GG>>, extra: ExprsExact<Extra<EE, V, GG>, V_Grp<V, GG, Grp>>, idPrefix?: string) => StreamRunnerParam<Delta<T>, "out">;
462
- declare const $groupId: <T extends O, V extends O, EE = {}, Out extends Loose<string, V, "_id"> = Loose<string, V, "_id">>(id: Expr<string, T>, args: DeltaAccumulators<T, O & Omit<V, Denied>>, out: RWCollection<Replace<Out, Strict<string, V, "_id", EE>>, Out>, extra: ExprsExact<Omit<EE, IdAndTsKeys | keyof Omit<V, IdAndTsKeys>>, doc & Omit<V, IdAndTsKeys>>) => StreamRunnerParam<Delta<T>, "out">;
463
- declare const $group: <T extends O, Grp extends notArr, V extends O, EE = {}, Out extends Loose<Grp, V, "_grp"> = Loose<Grp, V, "_grp">>(id: Expr<Grp, T>, args: DeltaAccumulators<T, O & Omit<V, Denied<"_grp">>>, out: RWCollection<Strict<Grp, V, "_grp", EE>, Out>, extra: ExprsExact<Omit<EE, IdAndTsKeys | "_grp" | Exclude<keyof V, IdAndTsKeys | "_grp">>, Rec<"_grp", Grp> & Omit<V, IdAndTsKeys | "_grp">>, idPrefix?: string) => StreamRunnerParam<Delta<T>, "out">;
463
+ declare const $groupMerge: <T extends O, Grp extends notArr, V extends O, GG extends string, EE = {}, Out extends Loose<Grp, V, GG> = Loose<Grp, V, GG>>(id: Expr<Grp, T>, args: DeltaAccumulators<T, O & Omit<V, Denied<GI<GG>>>>, out: MergeInto<Strict<Grp, V, GG, EE>, Out, WriteonlyCollection<MergedInput<Out, V, Grp, GG, EE>>>, gid: AsLiteral<GI<GG>>, extra: ExprsExact<Extra<EE, V, GG>, V_Grp<V, GG, Grp>>, idPrefix?: string) => StreamRunnerParam<Delta<T>, "out">;
464
+ declare const $groupId: <T extends O, V extends O, EE = {}, Out extends Loose<string, V, "_id"> = Loose<string, V, "_id">>(id: Expr<string, T>, args: DeltaAccumulators<T, O & Omit<V, Denied>>, out: RWCollection<MergedInput<Out, V, string, "_id", EE>, Out>, extra: ExprsExact<Omit<EE, IdAndTsKeys | keyof Omit<V, IdAndTsKeys>>, doc & Omit<V, IdAndTsKeys>>) => StreamRunnerParam<Delta<T>, "out">;
465
+ declare const $group: <T extends O, Grp extends notArr, V extends O, EE = {}, Out extends Loose<Grp, V, "_grp"> = Loose<Grp, V, "_grp">>(id: Expr<Grp, T>, args: DeltaAccumulators<T, O & Omit<V, Denied<"_grp">>>, out: RWCollection<MergedInput<Out, V, Grp, "_grp", EE> | Strict<Grp, V, "_grp", EE>, Out>, extra: ExprsExact<Omit<EE, IdAndTsKeys | "_grp" | Exclude<keyof V, IdAndTsKeys | "_grp">>, Rec<"_grp", Grp> & Omit<V, IdAndTsKeys | "_grp">>, idPrefix?: string) => StreamRunnerParam<Delta<T>, "out">;
464
466
 
465
467
  type Params<As extends string, LQ extends O, RQ extends O, RE extends RQ, S extends notArr> = {
466
468
  localField: Field<LQ, S>;
package/index.esm.js CHANGED
@@ -544,134 +544,6 @@ const subGroup = (id, args, addGrp) => {
544
544
  .with(replaceWith(field(addGrp(mapExact(args, (_, k) => $getField(root().expr(), k)))))).stages;
545
545
  };
546
546
 
547
- const subUpdater = (a, f) => ({ raw: (g) => a.raw(g.with(f)) });
548
- const set = () => (fields) => ({
549
- raw: f => {
550
- return Object.entries(mapExactToObject0(fields, v => v)).flatMap(([k, v]) => v.raw(f).map(([l, v]) => [`.${k}${l}`, v]));
551
- },
552
- });
553
- const weaken = (updater) => ({ raw: f => updater.raw(f) });
554
- const to = (expr) => ({
555
- raw: f => [['', expr.raw(f).get()]],
556
- });
557
-
558
- const dbcoll = (x) => ({
559
- db: x.dbName,
560
- coll: x.collectionName,
561
- });
562
-
563
- const $merge_ = ({ into, on, whenNotMatched, ...notMatched }) => asStages([
564
- {
565
- $merge: {
566
- into: dbcoll(into),
567
- on: on.str(),
568
- ...(whenNotMatched && { whenNotMatched }),
569
- ...(notMatched.stages && {
570
- whenMatched: notMatched.whenMatched,
571
- ...(notMatched.stages === 'ctx' && { let: rawVars(notMatched.vars, root()) }),
572
- }),
573
- },
574
- },
575
- ]);
576
- const $merge2 = (args) => $merge_(args);
577
-
578
- const subMerge = (args, out, gid, extra, idPrefix, first) => {
579
- const doubleReplace = (x) => x;
580
- const mergeAggregates = $set_(set()(mapExact0(args, (v, k) => to(first
581
- ? ctx()('new').of(k).expr()
582
- : v.merge(root().of(k).expr(), ctx()('new').of(k).expr())))));
583
- const gidPath = root().of(gid).expr();
584
- const mapId = (k, v) => map1(k, v);
585
- const F1 = {
586
- _id: ['_id', to(idPrefix ? concat$1(val(idPrefix), $rand) : $rand)],
587
- touchedAt: ['touchedAt', to(current)],
588
- };
589
- const F2 = mapId(gid, to(gidPath));
590
- const addExtraAndMerge = {
591
- ...mapExact0(extra, to),
592
- ...F1,
593
- ...F2,
594
- };
595
- const addTSAndExtra = {
596
- ...mapExact0(extra, to),
597
- touchedAt: ['touchedAt', to(current)],
598
- };
599
- const updater = set()(addTSAndExtra);
600
- return (link()
601
- .with($set_(set()(addExtraAndMerge)))
602
- .with($merge_({
603
- ...out,
604
- on: root().of(gid),
605
- vars: { new: ['new', root().expr()] },
606
- stages: 'ctx',
607
- whenMatched: link()
608
- .with(mergeAggregates)
609
- .with(doubleReplace($set_(updater))).stages,
610
- })).stages);
611
- };
612
-
613
- const addGrp = (gid) => (expr) => {
614
- const omit = omitPick();
615
- return omit.backward(mergeExpr(omit.forward(expr), map1(gid, root().of('_id').expr())));
616
- };
617
- const $groupMerge = (id, args, out, gid, extra, idPrefix = '') => ({
618
- raw: (first) => link()
619
- .with(subGroup(id, args, addGrp(gid)))
620
- .with(subMerge(args, out, gid, extra, idPrefix, first)).stages,
621
- teardown: c => c(out.whenNotMatched === 'insert'
622
- ? {
623
- collection: out.into,
624
- method: 'deleteMany',
625
- params: [{}],
626
- }
627
- : {
628
- collection: out.into,
629
- method: 'updateMany',
630
- params: [
631
- {},
632
- [
633
- {
634
- $unset: Object.keys({
635
- ...mapExactToObject(extra, () => 1),
636
- ...mapExactToObject(args, () => 1),
637
- }),
638
- },
639
- ],
640
- ],
641
- }),
642
- });
643
- const $groupId = (id, args, out, extra) => $groupMerge(id, args, { into: out, whenNotMatched: 'fail' }, '_id', extra);
644
- const $group = (id, args, out, extra, idPrefix = '') => $groupMerge(id, args, { into: out, whenNotMatched: 'insert' }, '_grp', extra, idPrefix);
645
-
646
- const deltaExpr = (expr) => (field) => {
647
- return ite(eqTyped($ifNull(root().of(field).expr(), nil), nil), nil, expr(field));
648
- };
649
- const $setEach1 = (updater, dict) => {
650
- return $set1(set()(mapExact1(dict, updater)));
651
- };
652
- const $setEach = (updater, dict) => $setEach1(updater, dict)(root);
653
- const $replaceWithEach1 = (t) => {
654
- return $setEach(k => to(t(k)), {
655
- after: ['after', 'after'],
656
- before: ['before', 'before'],
657
- });
658
- };
659
- const $replaceWithEach = (expr) => $replaceWithEach1(deltaExpr(expr));
660
-
661
- const $replaceWithDelta = (expr) => $replaceWithEach((field) => sub(expr, root().of(field)));
662
- const $setDelta = (updater) => {
663
- const dict = {
664
- after1: ['after1', 'after'],
665
- before1: ['before1', 'before'],
666
- };
667
- const update = (k) => subUpdater(weaken(updater), root().of(k));
668
- return link()
669
- .with($setEach(k => to(root().of(k).expr()), dict))
670
- .with($setEach(update, dict))
671
- .with($replaceWithEach(field => root().of(`${field}1`).expr()))
672
- .with($setEach(k => to(nil), dict)).stages;
673
- };
674
-
675
547
  const dayAndMonthPart = (date) => asExpr({
676
548
  raw: f => asExprRaw({ $dateToString: { date: date.raw(f).get(), format: '%m-%d' } }),
677
549
  });
@@ -908,6 +780,217 @@ const $map0 = ({ as, expr, input, }) => asExpr({
908
780
  const $map1 = (ex, map) => $map0({ input: ex, as: 'item', expr: map(ctx()('item')) });
909
781
  const $map = (ex, map) => $map1(ex, i => map(i.expr()));
910
782
 
783
+ const subUpdater = (a, f) => ({ raw: (g) => a.raw(g.with(f)) });
784
+ const set = () => (fields) => ({
785
+ raw: f => {
786
+ return Object.entries(mapExactToObject0(fields, v => v)).flatMap(([k, v]) => v.raw(f).map(([l, v]) => [`.${k}${l}`, v]));
787
+ },
788
+ });
789
+ const weaken = (updater) => ({ raw: f => updater.raw(f) });
790
+ const to = (expr) => ({
791
+ raw: f => [['', expr.raw(f).get()]],
792
+ });
793
+
794
+ const dbcoll = (x) => ({
795
+ db: x.dbName,
796
+ coll: x.collectionName,
797
+ });
798
+
799
+ const $merge_ = ({ into, on, whenNotMatched, ...notMatched }) => asStages([
800
+ {
801
+ $merge: {
802
+ into: dbcoll(into),
803
+ on: on.str(),
804
+ ...(whenNotMatched && { whenNotMatched }),
805
+ ...(notMatched.stages && {
806
+ whenMatched: notMatched.whenMatched,
807
+ ...(notMatched.stages === 'ctx' && { let: rawVars(notMatched.vars, root()) }),
808
+ }),
809
+ },
810
+ },
811
+ ]);
812
+ const $merge2 = (args) => $merge_(args);
813
+
814
+ const getWhenMatched = (whenNotMatched) => {
815
+ const setDeleted = whenNotMatched === 'discard';
816
+ const orNull = (e) => (setDeleted ? $ifNull(e, nil) : e);
817
+ const preMergeOld = setDeleted
818
+ ? mergeObjects(root().of('old').expr(), field({
819
+ deletedAt: ['deletedAt', orNull(root().of('old').of('deletedAt').expr())],
820
+ touchedAt: ['touchedAt', root().of('merged').of('touchedAt').expr()],
821
+ }))
822
+ : root().of('old').expr();
823
+ const same = eq(preMergeOld)(root().of('merged').expr());
824
+ const newOrOld = whenNotMatched ===
825
+ 'insert'
826
+ ? ctx()('new')
827
+ : root();
828
+ const merged = mergeObjects(root().expr(), ctx()('new').expr(), field({
829
+ deletedAt: ['deletedAt', orNull(newOrOld.of('deletedAt').expr())],
830
+ }));
831
+ return link()
832
+ .with($replaceWith_(field({
833
+ old: ['old', root().expr()],
834
+ merged: ['merged', merged],
835
+ })))
836
+ .with($replaceWith_(ite(same, root().of('old').expr(), root().of('merged').expr()))).stages;
837
+ };
838
+ const $mergeX = (out, keys, f, map, ext) => {
839
+ const patch = mapExact(keys, v => sub(v, f));
840
+ const filter = mapExactToObject(ext, v => ({ $eq: v }));
841
+ const setDeleted = out.whenNotMatched === 'discard';
842
+ const replacer = map(field(omitPick().backward(spread(patch, {
843
+ _id: ['_id', f.of('_id').expr()],
844
+ touchedAt: ['touchedAt', current],
845
+ }))));
846
+ const sss = setDeleted
847
+ ? link()
848
+ .with($set_(set()({
849
+ deletedAt: ['deletedAt', to(current)],
850
+ })))
851
+ .with($merge_({
852
+ into: out.coll,
853
+ on: root().of('_id'),
854
+ whenNotMatched: 'insert',
855
+ stages: true,
856
+ whenMatched: getWhenMatched(out.whenNotMatched),
857
+ })).stages
858
+ : link().with($merge_({
859
+ into: out.coll,
860
+ on: root().of('_id'),
861
+ whenNotMatched: 'fail',
862
+ stages: true,
863
+ whenMatched: getWhenMatched(out.whenNotMatched),
864
+ })).stages;
865
+ const teardown = (coll) => ({
866
+ collection: coll,
867
+ method: 'updateMany',
868
+ params: [
869
+ filter,
870
+ [
871
+ {
872
+ $unset: Object.keys(mapExactToObject(keys, () => 1)),
873
+ },
874
+ ],
875
+ ],
876
+ });
877
+ return {
878
+ raw: (first) => link().with($replaceWith_(replacer)).with(sss)
879
+ .stages,
880
+ teardown: c => (setDeleted ? c(teardown(out.coll)) : c(teardown(out.coll))),
881
+ };
882
+ };
883
+ const $mergeId = () => (out, keys, id, ext) => {
884
+ const omRORec = omitRORec();
885
+ return $mergeX(out, keys, root().of('after'), or => {
886
+ return ite(eqTyped(root().of('after').expr(), nil), field(omRORec.backward(spread(mapExact(keys, () => nil), {
887
+ _id: ['_id', id],
888
+ touchedAt: ['touchedAt', current],
889
+ }))), or);
890
+ }, ext);
891
+ };
892
+ const $simpleMergePart = () => (out, keys, ext) => $mergeX(out, keys, root(), id$1, ext);
893
+ const $simpleMerge = () => (out, keys, whenNotMatched = 'fail') => $mergeX({ coll: out, whenNotMatched }, keys, root(), id$1, {});
894
+ const $mergePart = () => (out, keys, ext) => $mergeId()({ coll: out, whenNotMatched: 'fail' }, keys, assertNotNull$1(root().of('before').of('_id').expr()), ext);
895
+ const $merge = () => (out, keys) => $mergePart()(out, keys, {});
896
+ const assertNotNull$1 = (expr) => expr;
897
+
898
+ const subMerge = (args, out, gid, extra, idPrefix, first) => {
899
+ const e = extra;
900
+ const mergeAggregates = $set_(set()(mapExact0(args, (v, k) => to(first
901
+ ? ctx()('new').of(k).expr()
902
+ : v.merge(root().of(k).expr(), ctx()('new').of(k).expr())))));
903
+ const gidPath = root().of(gid).expr();
904
+ const mapId = (k, v) => map1(k, v);
905
+ const F1 = {
906
+ _id: ['_id', to(idPrefix ? concat$1(val(idPrefix), $rand) : $rand)],
907
+ touchedAt: ['touchedAt', to(current)],
908
+ };
909
+ const F2 = mapId(gid, to(gidPath));
910
+ const addExtraAndMerge = {
911
+ ...mapExact0(extra, to),
912
+ ...F1,
913
+ ...F2,
914
+ };
915
+ const addExtra = mapExact0(e, to);
916
+ const updater = set()(addExtra);
917
+ return link()
918
+ .with($set_(set()(addExtraAndMerge)))
919
+ .with($merge_({
920
+ ...out,
921
+ whenNotMatched: 'fail',
922
+ on: root().of(gid),
923
+ vars: { new: ['new', root().expr()] },
924
+ stages: 'ctx',
925
+ whenMatched: link()
926
+ .with(mergeAggregates)
927
+ .with($set_(updater))
928
+ .with(getWhenMatched(out.whenNotMatched)).stages,
929
+ })).stages;
930
+ };
931
+
932
+ const addGrp = (gid) => (expr) => {
933
+ const omit = omitPick();
934
+ return omit.backward(mergeExpr(omit.forward(expr), map1(gid, root().of('_id').expr())));
935
+ };
936
+ const $groupMerge = (id, args, out, gid, extra, idPrefix = '') => ({
937
+ raw: (first) => link()
938
+ .with(subGroup(id, args, addGrp(gid)))
939
+ .with(subMerge(args, out, gid, extra, idPrefix, first)).stages,
940
+ teardown: c => c(out.whenNotMatched === 'insert'
941
+ ? {
942
+ collection: out.into,
943
+ method: 'deleteMany',
944
+ params: [{}],
945
+ }
946
+ : {
947
+ collection: out.into,
948
+ method: 'updateMany',
949
+ params: [
950
+ {},
951
+ [
952
+ {
953
+ $unset: Object.keys({
954
+ ...mapExactToObject(extra, () => 1),
955
+ ...mapExactToObject(args, () => 1),
956
+ }),
957
+ },
958
+ ],
959
+ ],
960
+ }),
961
+ });
962
+ const $groupId = (id, args, out, extra) => $groupMerge(id, args, { into: out, whenNotMatched: 'fail' }, '_id', extra);
963
+ const $group = (id, args, out, extra, idPrefix = '') => $groupMerge(id, args, { into: out, whenNotMatched: 'insert' }, '_grp', extra, idPrefix);
964
+
965
+ const deltaExpr = (expr) => (field) => {
966
+ return ite(eqTyped($ifNull(root().of(field).expr(), nil), nil), nil, expr(field));
967
+ };
968
+ const $setEach1 = (updater, dict) => {
969
+ return $set1(set()(mapExact1(dict, updater)));
970
+ };
971
+ const $setEach = (updater, dict) => $setEach1(updater, dict)(root);
972
+ const $replaceWithEach1 = (t) => {
973
+ return $setEach(k => to(t(k)), {
974
+ after: ['after', 'after'],
975
+ before: ['before', 'before'],
976
+ });
977
+ };
978
+ const $replaceWithEach = (expr) => $replaceWithEach1(deltaExpr(expr));
979
+
980
+ const $replaceWithDelta = (expr) => $replaceWithEach((field) => sub(expr, root().of(field)));
981
+ const $setDelta = (updater) => {
982
+ const dict = {
983
+ after1: ['after1', 'after'],
984
+ before1: ['before1', 'before'],
985
+ };
986
+ const update = (k) => subUpdater(weaken(updater), root().of(k));
987
+ return link()
988
+ .with($setEach(k => to(root().of(k).expr()), dict))
989
+ .with($setEach(update, dict))
990
+ .with($replaceWithEach(field => root().of(`${field}1`).expr()))
991
+ .with($setEach(k => to(nil), dict)).stages;
992
+ };
993
+
911
994
  const operator = () => (op) => (operand) => {
912
995
  return {
913
996
  raw: { [op]: operand },
@@ -1365,90 +1448,6 @@ const $replaceWith = (expr) => ({
1365
1448
  lin: $replaceWith_(expr),
1366
1449
  });
1367
1450
 
1368
- const getWhenMatched = (whenNotMatched) => {
1369
- const setDeleted = whenNotMatched === 'discard';
1370
- const orNull = (e) => (setDeleted ? $ifNull(e, nil) : e);
1371
- const preMergeOld = setDeleted
1372
- ? mergeObjects(root().of('old').expr(), field({
1373
- deletedAt: ['deletedAt', orNull(root().of('old').of('deletedAt').expr())],
1374
- touchedAt: ['touchedAt', root().of('merged').of('touchedAt').expr()],
1375
- }))
1376
- : root().of('old').expr();
1377
- const same = eq(preMergeOld)(root().of('merged').expr());
1378
- const newOrOld = whenNotMatched ===
1379
- 'insert'
1380
- ? ctx()('new')
1381
- : root();
1382
- const merged = mergeObjects(root().expr(), ctx()('new').expr(), field({
1383
- deletedAt: ['deletedAt', orNull(newOrOld.of('deletedAt').expr())],
1384
- }));
1385
- return link()
1386
- .with($replaceWith_(field({
1387
- old: ['old', root().expr()],
1388
- merged: ['merged', merged],
1389
- })))
1390
- .with($replaceWith_(ite(same, root().of('old').expr(), root().of('merged').expr()))).stages;
1391
- };
1392
- const $mergeX = (out, keys, f, map, ext) => {
1393
- const patch = mapExact(keys, v => sub(v, f));
1394
- const filter = mapExactToObject(ext, v => ({ $eq: v }));
1395
- const setDeleted = out.whenNotMatched === 'discard';
1396
- const replacer = map(field(omitPick().backward(spread(patch, {
1397
- _id: ['_id', f.of('_id').expr()],
1398
- touchedAt: ['touchedAt', current],
1399
- }))));
1400
- const sss = setDeleted
1401
- ? link()
1402
- .with($set_(set()({
1403
- deletedAt: ['deletedAt', to(current)],
1404
- })))
1405
- .with($merge_({
1406
- into: out.coll,
1407
- on: root().of('_id'),
1408
- whenNotMatched: 'insert',
1409
- stages: true,
1410
- whenMatched: getWhenMatched(out.whenNotMatched),
1411
- })).stages
1412
- : link().with($merge_({
1413
- into: out.coll,
1414
- on: root().of('_id'),
1415
- whenNotMatched: 'fail',
1416
- stages: true,
1417
- whenMatched: getWhenMatched(out.whenNotMatched),
1418
- })).stages;
1419
- const teardown = (coll) => ({
1420
- collection: coll,
1421
- method: 'updateMany',
1422
- params: [
1423
- filter,
1424
- [
1425
- {
1426
- $unset: Object.keys(mapExactToObject(keys, () => 1)),
1427
- },
1428
- ],
1429
- ],
1430
- });
1431
- return {
1432
- raw: (first) => link().with($replaceWith_(replacer)).with(sss)
1433
- .stages,
1434
- teardown: c => (setDeleted ? c(teardown(out.coll)) : c(teardown(out.coll))),
1435
- };
1436
- };
1437
- const $mergeId = () => (out, keys, id, ext) => {
1438
- const omRORec = omitRORec();
1439
- return $mergeX(out, keys, root().of('after'), or => {
1440
- return ite(eqTyped(root().of('after').expr(), nil), field(omRORec.backward(spread(mapExact(keys, () => nil), {
1441
- _id: ['_id', id],
1442
- touchedAt: ['touchedAt', current],
1443
- }))), or);
1444
- }, ext);
1445
- };
1446
- const $simpleMergePart = () => (out, keys, ext) => $mergeX(out, keys, root(), id$1, ext);
1447
- const $simpleMerge = () => (out, keys, whenNotMatched = 'fail') => $mergeX({ coll: out, whenNotMatched }, keys, root(), id$1, {});
1448
- const $mergePart = () => (out, keys, ext) => $mergeId()({ coll: out, whenNotMatched: 'fail' }, keys, assertNotNull$1(root().of('before').of('_id').expr()), ext);
1449
- const $merge = () => (out, keys) => $mergePart()(out, keys, {});
1450
- const assertNotNull$1 = (expr) => expr;
1451
-
1452
1451
  const $insertX = (out, expr, map, ext, extExpr) => {
1453
1452
  const filter = mapExactToObject(ext, v => ({ $eq: v }));
1454
1453
  return {