@omegup/msync 0.1.8 → 0.1.10

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 +39 -27
  2. package/index.esm.js +213 -226
  3. package/index.js +213 -225
  4. package/package.json +1 -1
  5. package/lib/accumulators/index.d.ts +0 -10
  6. package/lib/aggregate/$merge.d.ts +0 -19
  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 -69
  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 -3
  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.js CHANGED
@@ -546,134 +546,6 @@ const subGroup = (id, args, addGrp) => {
546
546
  .with(replaceWith(field(addGrp(mapExact(args, (_, k) => $getField(root().expr(), k)))))).stages;
547
547
  };
548
548
 
549
- const subUpdater = (a, f) => ({ raw: (g) => a.raw(g.with(f)) });
550
- const set = () => (fields) => ({
551
- raw: f => {
552
- return Object.entries(mapExactToObject0(fields, v => v)).flatMap(([k, v]) => v.raw(f).map(([l, v]) => [`.${k}${l}`, v]));
553
- },
554
- });
555
- const weaken = (updater) => ({ raw: f => updater.raw(f) });
556
- const to = (expr) => ({
557
- raw: f => [['', expr.raw(f).get()]],
558
- });
559
-
560
- const dbcoll = (x) => ({
561
- db: x.dbName,
562
- coll: x.collectionName,
563
- });
564
-
565
- const $merge_ = ({ into, on, whenNotMatched, ...notMatched }) => asStages([
566
- {
567
- $merge: {
568
- into: dbcoll(into),
569
- on: on.str(),
570
- ...(whenNotMatched && { whenNotMatched }),
571
- ...(notMatched.stages && {
572
- whenMatched: notMatched.whenMatched,
573
- ...(notMatched.stages === 'ctx' && { let: rawVars(notMatched.vars, root()) }),
574
- }),
575
- },
576
- },
577
- ]);
578
- const $merge2 = (args) => $merge_(args);
579
-
580
- const subMerge = (args, out, gid, extra, idPrefix, first) => {
581
- const doubleReplace = (x) => x;
582
- const mergeAggregates = $set_(set()(mapExact0(args, (v, k) => to(first
583
- ? ctx()('new').of(k).expr()
584
- : v.merge(root().of(k).expr(), ctx()('new').of(k).expr())))));
585
- const gidPath = root().of(gid).expr();
586
- const mapId = (k, v) => map1(k, v);
587
- const F1 = {
588
- _id: ['_id', to(idPrefix ? concat$1(val(idPrefix), $rand) : $rand)],
589
- touchedAt: ['touchedAt', to(current)],
590
- };
591
- const F2 = mapId(gid, to(gidPath));
592
- const addExtraAndMerge = {
593
- ...mapExact0(extra, to),
594
- ...F1,
595
- ...F2,
596
- };
597
- const addTSAndExtra = {
598
- ...mapExact0(extra, to),
599
- touchedAt: ['touchedAt', to(current)],
600
- };
601
- const updater = set()(addTSAndExtra);
602
- return (link()
603
- .with($set_(set()(addExtraAndMerge)))
604
- .with($merge_({
605
- ...out,
606
- on: root().of(gid),
607
- vars: { new: ['new', root().expr()] },
608
- stages: 'ctx',
609
- whenMatched: link()
610
- .with(mergeAggregates)
611
- .with(doubleReplace($set_(updater))).stages,
612
- })).stages);
613
- };
614
-
615
- const addGrp = (gid) => (expr) => {
616
- const omit = omitPick();
617
- return omit.backward(mergeExpr(omit.forward(expr), map1(gid, root().of('_id').expr())));
618
- };
619
- const $groupMerge = (id, args, out, gid, extra, idPrefix = '') => ({
620
- raw: (first) => link()
621
- .with(subGroup(id, args, addGrp(gid)))
622
- .with(subMerge(args, out, gid, extra, idPrefix, first)).stages,
623
- teardown: c => c(out.whenNotMatched === 'insert'
624
- ? {
625
- collection: out.into,
626
- method: 'deleteMany',
627
- params: [{}],
628
- }
629
- : {
630
- collection: out.into,
631
- method: 'updateMany',
632
- params: [
633
- {},
634
- [
635
- {
636
- $unset: Object.keys({
637
- ...mapExactToObject(extra, () => 1),
638
- ...mapExactToObject(args, () => 1),
639
- }),
640
- },
641
- ],
642
- ],
643
- }),
644
- });
645
- const $groupId = (id, args, out, extra) => $groupMerge(id, args, { into: out, whenNotMatched: 'fail' }, '_id', extra);
646
- const $group = (id, args, out, extra, idPrefix = '') => $groupMerge(id, args, { into: out, whenNotMatched: 'insert' }, '_grp', extra, idPrefix);
647
-
648
- const deltaExpr = (expr) => (field) => {
649
- return ite(eqTyped($ifNull(root().of(field).expr(), nil), nil), nil, expr(field));
650
- };
651
- const $setEach1 = (updater, dict) => {
652
- return $set1(set()(mapExact1(dict, updater)));
653
- };
654
- const $setEach = (updater, dict) => $setEach1(updater, dict)(root);
655
- const $replaceWithEach1 = (t) => {
656
- return $setEach(k => to(t(k)), {
657
- after: ['after', 'after'],
658
- before: ['before', 'before'],
659
- });
660
- };
661
- const $replaceWithEach = (expr) => $replaceWithEach1(deltaExpr(expr));
662
-
663
- const $replaceWithDelta = (expr) => $replaceWithEach((field) => sub(expr, root().of(field)));
664
- const $setDelta = (updater) => {
665
- const dict = {
666
- after1: ['after1', 'after'],
667
- before1: ['before1', 'before'],
668
- };
669
- const update = (k) => subUpdater(weaken(updater), root().of(k));
670
- return link()
671
- .with($setEach(k => to(root().of(k).expr()), dict))
672
- .with($setEach(update, dict))
673
- .with($replaceWithEach(field => root().of(`${field}1`).expr()))
674
- .with($setEach(k => to(nil), dict)).stages;
675
- };
676
-
677
549
  const dayAndMonthPart = (date) => asExpr({
678
550
  raw: f => asExprRaw({ $dateToString: { date: date.raw(f).get(), format: '%m-%d' } }),
679
551
  });
@@ -910,6 +782,217 @@ const $map0 = ({ as, expr, input, }) => asExpr({
910
782
  const $map1 = (ex, map) => $map0({ input: ex, as: 'item', expr: map(ctx()('item')) });
911
783
  const $map = (ex, map) => $map1(ex, i => map(i.expr()));
912
784
 
785
+ const subUpdater = (a, f) => ({ raw: (g) => a.raw(g.with(f)) });
786
+ const set = () => (fields) => ({
787
+ raw: f => {
788
+ return Object.entries(mapExactToObject0(fields, v => v)).flatMap(([k, v]) => v.raw(f).map(([l, v]) => [`.${k}${l}`, v]));
789
+ },
790
+ });
791
+ const weaken = (updater) => ({ raw: f => updater.raw(f) });
792
+ const to = (expr) => ({
793
+ raw: f => [['', expr.raw(f).get()]],
794
+ });
795
+
796
+ const dbcoll = (x) => ({
797
+ db: x.dbName,
798
+ coll: x.collectionName,
799
+ });
800
+
801
+ const $merge_ = ({ into, on, whenNotMatched, ...notMatched }) => asStages([
802
+ {
803
+ $merge: {
804
+ into: dbcoll(into),
805
+ on: on.str(),
806
+ ...(whenNotMatched && { whenNotMatched }),
807
+ ...(notMatched.stages && {
808
+ whenMatched: notMatched.whenMatched,
809
+ ...(notMatched.stages === 'ctx' && { let: rawVars(notMatched.vars, root()) }),
810
+ }),
811
+ },
812
+ },
813
+ ]);
814
+ const $merge2 = (args) => $merge_(args);
815
+
816
+ const getWhenMatched = (whenNotMatched) => {
817
+ const setDeleted = whenNotMatched === 'discard';
818
+ const orNull = (e) => (setDeleted ? $ifNull(e, nil) : e);
819
+ const preMergeOld = setDeleted
820
+ ? mergeObjects(root().of('old').expr(), field({
821
+ deletedAt: ['deletedAt', orNull(root().of('old').of('deletedAt').expr())],
822
+ touchedAt: ['touchedAt', root().of('merged').of('touchedAt').expr()],
823
+ }))
824
+ : root().of('old').expr();
825
+ const same = eq(preMergeOld)(root().of('merged').expr());
826
+ const newOrOld = whenNotMatched ===
827
+ 'insert'
828
+ ? ctx()('new')
829
+ : root();
830
+ const merged = mergeObjects(root().expr(), ctx()('new').expr(), field({
831
+ deletedAt: ['deletedAt', orNull(newOrOld.of('deletedAt').expr())],
832
+ }));
833
+ return link()
834
+ .with($replaceWith_(field({
835
+ old: ['old', root().expr()],
836
+ merged: ['merged', merged],
837
+ })))
838
+ .with($replaceWith_(ite(same, root().of('old').expr(), root().of('merged').expr()))).stages;
839
+ };
840
+ const $mergeX = (out, keys, f, map, ext) => {
841
+ const patch = mapExact(keys, v => sub(v, f));
842
+ const filter = mapExactToObject(ext, v => ({ $eq: v }));
843
+ const setDeleted = out.whenNotMatched === 'discard';
844
+ const replacer = map(field(omitPick().backward(spread(patch, {
845
+ _id: ['_id', f.of('_id').expr()],
846
+ touchedAt: ['touchedAt', current],
847
+ }))));
848
+ const sss = setDeleted
849
+ ? link()
850
+ .with($set_(set()({
851
+ deletedAt: ['deletedAt', to(current)],
852
+ })))
853
+ .with($merge_({
854
+ into: out.coll,
855
+ on: root().of('_id'),
856
+ whenNotMatched: 'insert',
857
+ stages: true,
858
+ whenMatched: getWhenMatched(out.whenNotMatched),
859
+ })).stages
860
+ : link().with($merge_({
861
+ into: out.coll,
862
+ on: root().of('_id'),
863
+ whenNotMatched: 'fail',
864
+ stages: true,
865
+ whenMatched: getWhenMatched(out.whenNotMatched),
866
+ })).stages;
867
+ const teardown = (coll) => ({
868
+ collection: coll,
869
+ method: 'updateMany',
870
+ params: [
871
+ filter,
872
+ [
873
+ {
874
+ $unset: Object.keys(mapExactToObject(keys, () => 1)),
875
+ },
876
+ ],
877
+ ],
878
+ });
879
+ return {
880
+ raw: (first) => link().with($replaceWith_(replacer)).with(sss)
881
+ .stages,
882
+ teardown: c => (setDeleted ? c(teardown(out.coll)) : c(teardown(out.coll))),
883
+ };
884
+ };
885
+ const $mergeId = () => (out, keys, id, ext) => {
886
+ const omRORec = omitRORec();
887
+ return $mergeX(out, keys, root().of('after'), or => {
888
+ return ite(eqTyped(root().of('after').expr(), nil), field(omRORec.backward(spread(mapExact(keys, () => nil), {
889
+ _id: ['_id', id],
890
+ touchedAt: ['touchedAt', current],
891
+ }))), or);
892
+ }, ext);
893
+ };
894
+ const $simpleMergePart = () => (out, keys, ext) => $mergeX(out, keys, root(), id$1, ext);
895
+ const $simpleMerge = () => (out, keys, whenNotMatched = 'fail') => $mergeX({ coll: out, whenNotMatched }, keys, root(), id$1, {});
896
+ const $mergePart = () => (out, keys, ext) => $mergeId()({ coll: out, whenNotMatched: 'fail' }, keys, assertNotNull$1(root().of('before').of('_id').expr()), ext);
897
+ const $merge = () => (out, keys) => $mergePart()(out, keys, {});
898
+ const assertNotNull$1 = (expr) => expr;
899
+
900
+ const subMerge = (args, out, gid, extra, idPrefix, first) => {
901
+ const e = extra;
902
+ const mergeAggregates = $set_(set()(mapExact0(args, (v, k) => to(first
903
+ ? ctx()('new').of(k).expr()
904
+ : v.merge(root().of(k).expr(), ctx()('new').of(k).expr())))));
905
+ const gidPath = root().of(gid).expr();
906
+ const mapId = (k, v) => map1(k, v);
907
+ const F1 = {
908
+ _id: ['_id', to(idPrefix ? concat$1(val(idPrefix), $rand) : $rand)],
909
+ touchedAt: ['touchedAt', to(current)],
910
+ };
911
+ const F2 = mapId(gid, to(gidPath));
912
+ const addExtraAndMerge = {
913
+ ...mapExact0(extra, to),
914
+ ...F1,
915
+ ...F2,
916
+ };
917
+ const addExtra = mapExact0(e, to);
918
+ const updater = set()(addExtra);
919
+ return link()
920
+ .with($set_(set()(addExtraAndMerge)))
921
+ .with($merge_({
922
+ into: null,
923
+ whenNotMatched: 'fail',
924
+ on: root().of(gid),
925
+ vars: { new: ['new', root().expr()] },
926
+ stages: 'ctx',
927
+ whenMatched: link()
928
+ .with(mergeAggregates)
929
+ .with($set_(updater))
930
+ .with(getWhenMatched(out.whenNotMatched)).stages,
931
+ })).stages;
932
+ };
933
+
934
+ const addGrp = (gid) => (expr) => {
935
+ const omit = omitPick();
936
+ return omit.backward(mergeExpr(omit.forward(expr), map1(gid, root().of('_id').expr())));
937
+ };
938
+ const $groupMerge = (id, args, out, gid, extra, idPrefix = '') => ({
939
+ raw: (first) => link()
940
+ .with(subGroup(id, args, addGrp(gid)))
941
+ .with(subMerge(args, out, gid, extra, idPrefix, first)).stages,
942
+ teardown: c => c(out.whenNotMatched === 'insert'
943
+ ? {
944
+ collection: out.into,
945
+ method: 'deleteMany',
946
+ params: [{}],
947
+ }
948
+ : {
949
+ collection: out.into,
950
+ method: 'updateMany',
951
+ params: [
952
+ {},
953
+ [
954
+ {
955
+ $unset: Object.keys({
956
+ ...mapExactToObject(extra, () => 1),
957
+ ...mapExactToObject(args, () => 1),
958
+ }),
959
+ },
960
+ ],
961
+ ],
962
+ }),
963
+ });
964
+ const $groupId = (id, args, out, extra) => $groupMerge(id, args, { into: out, whenNotMatched: 'fail' }, '_id', extra);
965
+ const $group = (id, args, out, extra, idPrefix = '') => $groupMerge(id, args, { into: out, whenNotMatched: 'insert' }, '_grp', extra, idPrefix);
966
+
967
+ const deltaExpr = (expr) => (field) => {
968
+ return ite(eqTyped($ifNull(root().of(field).expr(), nil), nil), nil, expr(field));
969
+ };
970
+ const $setEach1 = (updater, dict) => {
971
+ return $set1(set()(mapExact1(dict, updater)));
972
+ };
973
+ const $setEach = (updater, dict) => $setEach1(updater, dict)(root);
974
+ const $replaceWithEach1 = (t) => {
975
+ return $setEach(k => to(t(k)), {
976
+ after: ['after', 'after'],
977
+ before: ['before', 'before'],
978
+ });
979
+ };
980
+ const $replaceWithEach = (expr) => $replaceWithEach1(deltaExpr(expr));
981
+
982
+ const $replaceWithDelta = (expr) => $replaceWithEach((field) => sub(expr, root().of(field)));
983
+ const $setDelta = (updater) => {
984
+ const dict = {
985
+ after1: ['after1', 'after'],
986
+ before1: ['before1', 'before'],
987
+ };
988
+ const update = (k) => subUpdater(weaken(updater), root().of(k));
989
+ return link()
990
+ .with($setEach(k => to(root().of(k).expr()), dict))
991
+ .with($setEach(update, dict))
992
+ .with($replaceWithEach(field => root().of(`${field}1`).expr()))
993
+ .with($setEach(k => to(nil), dict)).stages;
994
+ };
995
+
913
996
  const operator = () => (op) => (operand) => {
914
997
  return {
915
998
  raw: { [op]: operand },
@@ -1367,86 +1450,6 @@ const $replaceWith = (expr) => ({
1367
1450
  lin: $replaceWith_(expr),
1368
1451
  });
1369
1452
 
1370
- const $mergeX = (out, keys, f, map, whenNotMatched = 'fail', ext) => {
1371
- const patch = mapExact(keys, v => sub(v, f));
1372
- const filter = mapExactToObject(ext, v => ({ $eq: v }));
1373
- const setDeleted = whenNotMatched === 'discard';
1374
- const replacer = map(field(omitPick().backward(spread(patch, {
1375
- _id: ['_id', f.of('_id').expr()],
1376
- touchedAt: ['touchedAt', current],
1377
- deletedAt: [setDeleted ? 'deletedAt' : 'touchedAt', current],
1378
- }))));
1379
- return {
1380
- raw: (first) => link()
1381
- .with($replaceWith_(replacer))
1382
- .with($merge_({
1383
- into: out,
1384
- on: root().of('_id'),
1385
- whenNotMatched: whenNotMatched === 'fail' ? 'fail' : 'insert',
1386
- stages: true,
1387
- whenMatched: asStages([
1388
- {
1389
- $replaceWith: {
1390
- old: '$$ROOT',
1391
- merged: {
1392
- $mergeObjects: [
1393
- '$$ROOT',
1394
- '$$new',
1395
- ...(setDeleted ? [{ deletedAt: { $ifNull: ['$deletedAt', null] } }] : []),
1396
- ],
1397
- },
1398
- },
1399
- },
1400
- {
1401
- $replaceWith: {
1402
- $cond: {
1403
- if: {
1404
- $eq: [
1405
- {
1406
- $mergeObjects: [
1407
- '$old',
1408
- { deletedAt: { $ifNull: ['$old.deletedAt', null] } },
1409
- ],
1410
- },
1411
- { $mergeObjects: ['$merged', { touchedAt: '$old.touchedAt' }] },
1412
- ],
1413
- },
1414
- then: '$old',
1415
- else: '$merged',
1416
- },
1417
- },
1418
- },
1419
- ]),
1420
- })).stages,
1421
- teardown: c => c({
1422
- collection: out,
1423
- method: 'updateMany',
1424
- params: [
1425
- filter,
1426
- [
1427
- {
1428
- $unset: Object.keys(mapExactToObject(keys, () => 1)),
1429
- },
1430
- ],
1431
- ],
1432
- }),
1433
- };
1434
- };
1435
- const $mergeId = () => (out, keys, id, ext, whenNotMatched) => {
1436
- const omRORec = omitRORec();
1437
- return $mergeX(out, keys, root().of('after'), or => {
1438
- return ite(eqTyped(root().of('after').expr(), nil), field(omRORec.backward(spread(mapExact(keys, () => nil), {
1439
- _id: ['_id', id],
1440
- touchedAt: ['touchedAt', current],
1441
- }))), or);
1442
- }, whenNotMatched, ext);
1443
- };
1444
- const $simpleMergePart = () => (out, keys, whenNotMatched, ext) => $mergeX(out, keys, root(), id$1, whenNotMatched, ext);
1445
- const $simpleMerge = () => (out, keys, whenNotMatched = 'fail') => $mergeX(out, keys, root(), id$1, whenNotMatched, {});
1446
- const $mergePart = () => (out, keys, ext, whenNotMatched = 'fail') => $mergeId()(out, keys, assertNotNull$1(root().of('before').of('_id').expr()), ext, whenNotMatched);
1447
- const $merge = () => (out, keys, whenNotMatched = 'fail') => $mergePart()(out, keys, {}, whenNotMatched);
1448
- const assertNotNull$1 = (expr) => expr;
1449
-
1450
1453
  const $insertX = (out, expr, map, ext, extExpr) => {
1451
1454
  const filter = mapExactToObject(ext, v => ({ $eq: v }));
1452
1455
  return {
@@ -1469,23 +1472,7 @@ const $insertX = (out, expr, map, ext, extExpr) => {
1469
1472
  into: out,
1470
1473
  on: root().of('_id'),
1471
1474
  stages: true,
1472
- whenMatched: asStages([
1473
- { $replaceWith: { old: '$$ROOT', merged: { $mergeObjects: ['$$ROOT', '$$new'] } } },
1474
- {
1475
- $replaceWith: {
1476
- $cond: {
1477
- if: {
1478
- $eq: [
1479
- '$old',
1480
- { $mergeObjects: ['$merged', { touchedAt: '$old.touchedAt' }] },
1481
- ],
1482
- },
1483
- then: '$old',
1484
- else: '$merged',
1485
- },
1486
- },
1487
- },
1488
- ]),
1475
+ whenMatched: getWhenMatched('insert'),
1489
1476
  whenNotMatched: 'insert',
1490
1477
  })).stages;
1491
1478
  },
@@ -2096,6 +2083,7 @@ exports.firstSure = firstSure;
2096
2083
  exports.floor = floor;
2097
2084
  exports.from = from;
2098
2085
  exports.func = func;
2086
+ exports.getWhenMatched = getWhenMatched;
2099
2087
  exports.gt = gt;
2100
2088
  exports.gte = gte;
2101
2089
  exports.inArray = inArray;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "module": "index.esm.js",
4
4
  "typings": "index.d.ts",
5
5
  "name": "@omegup/msync",
6
- "version": "0.1.8",
6
+ "version": "0.1.10",
7
7
  "dependencies": {
8
8
  "dayjs": "^1.11.9",
9
9
  "dotenv": "^16.3.1",
@@ -1,10 +0,0 @@
1
- import type { AppMap, Arr, N, O, Rec } from '../../types';
2
- import type { ExprHKT } from '../expression/concat';
3
- import { type NoRaw } from '../expression/val';
4
- import type { DeltaAccumulator, Expr, Part } from '../types';
5
- export declare const $sum: <D extends O, C = unknown>(expr: Expr<number | N, D, C>) => DeltaAccumulator<D, number, C>;
6
- export declare const $accumulator: <D, T, Ctx, A extends readonly unknown[]>(init: () => NoRaw<T>, accumulateArgs: AppMap<ExprHKT<Part<D>, Ctx>, A>, accumulate: (a: NoRaw<T>, ...args: NoRaw<A>) => NoRaw<T>, merge: (...args: NoRaw<[T | N, T]>) => NoRaw<T>) => DeltaAccumulator<D, T, Ctx>;
7
- export declare const $countDict: <D extends O, C = unknown>(expr: Expr<string, D, C>) => DeltaAccumulator<D, Rec<string, number>, C>;
8
- export declare const $pushDict: <D extends O, V, C = unknown>(key: Expr<string, D, C>, value: Expr<V, D, C>) => DeltaAccumulator<D, Rec<string, Rec<"0" | "1", Arr<V>>>, C>;
9
- export declare const $keys: <D extends O, C = unknown>(expr: Expr<Rec<string, number>, D, C>) => Expr<Arr<string>, D, C>;
10
- export declare const $entries: <D extends O, V, C = unknown>(expr: Expr<Rec<string, Rec<"1" | "0", Arr<V>>>, D, C>) => Expr<Arr<Rec<"k", string> & Rec<"v", V>>, D, C>;
@@ -1,19 +0,0 @@
1
- import type { IdHKT, OPick, RWCollection } from '../../types';
2
- import type { ID, N, O, RORec, Rec, Replace, StrKey, doc, rawItem } from '../../types/json';
3
- import { type ExprsExact } from '../expression/concat';
4
- import type { Delta, Expr, StreamRunnerParam, TS } from '../types';
5
- import { type Exact } from '../utils/map-object';
6
- type OutInputE<T, E, A = T | null> = ID & Rec<'after', A> & E;
7
- type Allowed<K extends string> = Exclude<K, keyof (TS & ID)>;
8
- type Patch<V, KK extends StrKey<V> = StrKey<V>> = ((OPick<V, Allowed<KK>> & ID) | (Rec<Allowed<KK>, N> & ID)) & TS;
9
- type TakeDoc<V, E = ID, KK extends StrKey<V> = StrKey<V>> = OPick<V, Allowed<KK>> & E;
10
- type ND = {
11
- readonly deletedAt?: null;
12
- };
13
- type SafeE<E> = Omit<E, `$${string}` | keyof ID>;
14
- export declare const $mergeId: <V extends O>() => <SourcePart extends doc, Out extends doc, E = unknown, EEE extends RORec<string, rawItem> = {}>(out: RWCollection<Out | Replace<Out, Patch<V>>, Out>, keys: ExprsExact<TakeDoc<V, unknown>, SourcePart>, id: Expr<string, OutInputE<TakeDoc<V>, E, null>>, ext: Exact<Omit<SafeE<EEE>, keyof (ND & TS)>, IdHKT>, whenNotMatched: "fail" | "discard") => StreamRunnerParam<OutInputE<SourcePart, E>, "out">;
15
- export declare const $simpleMergePart: <V extends O>() => <Source extends doc, Out extends doc, EEE extends RORec<string, rawItem>>(out: RWCollection<Out | Replace<Out, Patch<V>>, Out>, keys: ExprsExact<TakeDoc<V, unknown>, Source>, whenNotMatched: "fail" | "discard", ext: Exact<Omit<SafeE<EEE>, keyof (ND & TS)>, IdHKT>) => StreamRunnerParam<Source, "out">;
16
- export declare const $simpleMerge: <V extends O>() => <Source extends doc, Out extends doc>(out: RWCollection<Out | Replace<Out, Patch<V>>, Out>, keys: ExprsExact<TakeDoc<V, unknown>, Source>, whenNotMatched?: "fail" | "discard") => StreamRunnerParam<Source, "out">;
17
- export declare const $mergePart: <V extends O>() => <Out extends doc, SourcePart extends doc, EEE extends RORec<string, rawItem>>(out: RWCollection<Out | Replace<Out, Patch<V>>, Out>, keys: ExprsExact<TakeDoc<V, unknown>, SourcePart>, ext: Exact<Omit<SafeE<EEE>, keyof (ND & TS)>, IdHKT>, whenNotMatched?: "fail" | "discard") => StreamRunnerParam<Delta<SourcePart>, "out">;
18
- export declare const $merge: <V extends O>() => <Out extends doc, SourcePart extends doc>(out: RWCollection<Out | Replace<Out, Patch<V>>, Out>, keys: ExprsExact<TakeDoc<V, unknown>, SourcePart>, whenNotMatched?: "fail" | "discard") => StreamRunnerParam<Delta<SourcePart>, "out">;
19
- export {};
@@ -1,15 +0,0 @@
1
- import type { IdHKT, RWCollection } from '../../types';
2
- import type { ID, O, RORec, doc, rawItem } from '../../types/json';
3
- import { type ExprsExact } from '../expression/concat';
4
- import type { Del, Delta, Expr, StreamRunnerParam, TS } from '../types';
5
- import { type Exact } from '../utils/map-object';
6
- type ND = {
7
- readonly deletedAt?: null;
8
- };
9
- type SafeE<E> = Omit<E, `$${string}` | keyof ID>;
10
- export type Merge<T extends doc, E> = Omit<SafeE<E>, keyof (ND & TS)> & ((T & ND & TS) | Del);
11
- export declare const $insertX: <T extends doc, D extends O, EEE extends RORec<string, rawItem>>(out: RWCollection<Merge<T, EEE>>, expr: Expr<T, D>, map: (x: Expr<T & ND & TS & Omit<SafeE<EEE>, keyof (ND & TS)>, D>) => Expr<Merge<T, EEE>, D>, ext: Exact<Omit<SafeE<EEE>, keyof (ND & TS)>, IdHKT>, extExpr: ExprsExact<Omit<SafeE<EEE>, keyof (ND & TS)>, unknown>) => StreamRunnerParam<D, "out">;
12
- export declare const $simpleInsert: <T extends doc>(out: RWCollection<Merge<T, {}>>) => StreamRunnerParam<T, "out">;
13
- export declare const $insertPart: <T extends doc, EEE extends RORec<string, rawItem>>(out: RWCollection<Merge<T, EEE>>, ext: Exact<Omit<SafeE<EEE>, keyof (ND & TS)>, IdHKT>) => StreamRunnerParam<Delta<T>, "out">;
14
- export declare const $insert: <T extends doc>(out: RWCollection<Merge<T, {}>>) => StreamRunnerParam<Delta<T>, "out">;
15
- export {};
@@ -1,12 +0,0 @@
1
- import type { RWCollection, WriteonlyCollection } from '../../../types';
2
- import type { AsLiteral, ID, O, Rec, Replace, doc, notArr } from '../../../types/json';
3
- import { type ExprsExact } from '../../expression/concat';
4
- import type { Delta, DeltaAccumulators, Expr, StreamRunnerParam, TS } from '../../types';
5
- import type { MergeInto } from '../out';
6
- import { type Extra, type IdAndTsKeys, type Loose, type Strict, type V_Grp } from './utils/sub-merge';
7
- type Denied<GID = never> = keyof (TS & ID) | GID;
8
- type GI<GG> = Exclude<GG, keyof TS>;
9
- export 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">;
10
- export 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">;
11
- export 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">;
12
- export {};
@@ -1,2 +0,0 @@
1
- export { $groupMerge, $groupId, $group } from './$group-merge';
2
- export type { Loose, Strict } from './utils/sub-merge';
@@ -1,6 +0,0 @@
1
- import type { O, RORec, Rec } from '../../../../types';
2
- import { type ExprsExact } from '../../../expression/concat';
3
- import type { Delta, Expr, RawStages } from '../../../types';
4
- import type { DeltaAccumulators } from '../../../types/accumulator';
5
- export type WithItem<V, Grp> = Rec<'_id', Grp> & Rec<'item', O<V>>;
6
- export declare const subGroup: <T extends O, Grp, V extends O, GID extends string>(id: Expr<Grp, T>, args: DeltaAccumulators<T, V>, addGrp: <D extends Rec<"_id", Grp>>(src: ExprsExact<V, D>) => ExprsExact<RORec<GID, Grp> & V, D>) => RawStages<unknown, Delta<T>, Rec<GID, Grp> & V>;
@@ -1,18 +0,0 @@
1
- import type { AsLiteral, ID, O, Rec, Replace, WriteonlyCollection, notArr } from '../../../../types';
2
- import { type ExprsExact } from '../../../expression/concat';
3
- import type { RawStages, TS } from '../../../types';
4
- import type { DeltaAccumulators } from '../../../types/accumulator';
5
- import { type MergeInto } from '../../out';
6
- type GI<GG> = Exclude<GG, keyof TS>;
7
- export type IdAndTsKeys = keyof (TS & ID);
8
- type V<VV, GG extends string> = Omit<VV, IdAndTsKeys | GI<GG>>;
9
- export type Prepare<Grp, GG extends string> = TS & ID & Rec<GI<GG>, Grp>;
10
- type Par<T> = {
11
- [P in keyof T]?: T[P] | null;
12
- };
13
- export type Loose<Grp, VV, GG extends string> = Prepare<Grp, GG> & Par<V<VV, GG>>;
14
- export type Strict<Grp, VV, GG extends string, EE> = Prepare<Grp, GG> & V<VV, GG> & Omit<EE, IdAndTsKeys | GI<GG> | keyof V<VV, GG>>;
15
- export type V_Grp<VV, GG extends string, Grp> = Rec<GI<GG>, Grp> & V<VV, GG>;
16
- export type Extra<EE, VV, GG extends string> = Omit<EE, IdAndTsKeys | GI<GG> | keyof V<VV, GG>>;
17
- export declare const subMerge: <T extends O, Grp extends notArr, VV extends O, GG extends string, EE = {}, Out extends Loose<Grp, VV, GG> = Loose<Grp, VV, GG>>(args: DeltaAccumulators<T, V<VV, GG>>, out: MergeInto<Strict<Grp, VV, GG, EE>, Out, WriteonlyCollection<Replace<Out, Strict<Grp, VV, GG, EE>>>>, gid: AsLiteral<GI<GG>>, extra: ExprsExact<Extra<EE, VV, GG>, V_Grp<VV, GG, Grp>>, idPrefix: string, first: boolean) => RawStages<unknown, V_Grp<VV, GG, Grp>, "out">;
18
- export {};
@@ -1,9 +0,0 @@
1
- export * from './group';
2
- export * from './lookup';
3
- export * from './match';
4
- export * from './set';
5
- export * from './unwind';
6
- export * from './$merge';
7
- export * from './$upsert';
8
- export * from './out';
9
- export { $group_ } from './mongo-stages';
@@ -1,10 +0,0 @@
1
- import type { AsLiteral, ID, N, O, Rec, doc, rawItem } from '../../../types';
2
- import { Field } from '../../field';
3
- import type { BA, Before, Delta, RawStages, TStages, UBefore } from '../../types';
4
- type s = string;
5
- type Both<K1 extends s, LE, KK2 extends s, RE, N1 extends null = never, N2 extends null = never> = Delta<Rec<K1, LE | N1> & Rec<Exclude<KK2, BA | K1>, RE | N2> & ID>;
6
- export declare const $lookupDelta: <LQ extends O, LE extends LQ & doc, RQ extends O, RE extends RQ & doc, BRB extends UBefore<RQ>, RS extends UBefore<RQ>, S extends rawItem, K1 extends s, KK2 extends s, N1 extends null = never, N2 extends null = never>({ field1, field2 }: {
7
- field1: Field<LQ, S | N>;
8
- field2: Field<RQ, S | N>;
9
- }, { coll, exec, input }: TStages<RS, UBefore<RQ>, BRB, Before<RE>>, k1: AsLiteral<K1>, k2: AsLiteral<Exclude<KK2, BA | K1>>, k: ([N1] extends [never] ? K1 : never) | ([N2] extends [never] ? Exclude<KK2, BA | K1> : never) | false, includeNull1?: N1, includeNull2?: N2) => RawStages<unknown, Delta<LE>, Both<K1, LE, KK2, RE, N1, N2>>;
10
- export {};
@@ -1,9 +0,0 @@
1
- import type { App, AsLiteral, HKT, ID, O, Rec } from '../../../types';
2
- import { type Field } from '../../field';
3
- import type { Before, RawStages, TStages } from '../../types';
4
- type s = string;
5
- export declare const $lookupRaw: <LQ extends O, LE extends LQ & ID, RQ extends O, RE extends RQ & ID, BRB extends Before<RQ>, RS, S, As extends s, Null extends null = never>({ field1, field2 }: {
6
- field1: Field<LQ, S>;
7
- field2: Field<RQ, S>;
8
- }, { coll, exec, input }: TStages<RS, Before<RQ>, BRB, Before<RE>>, k2: AsLiteral<As>, k: "left", includeNull?: Null) => <F extends HKT<O, O>>(f: <T extends O>() => Field<App<F, T>, T>) => RawStages<App<F, LQ>, App<F, LE>, App<F, LE & Rec<As, RE> & ID>>;
9
- export {};
@@ -1,12 +0,0 @@
1
- import type { AsLiteral, O, RORec, doc, notArr } from '../../../types';
2
- import { type Field } from '../../field';
3
- import type { SnapshotStream, SnapshotStreamExecutionResult } from '../../types/stream';
4
- type Params<As extends string, LQ extends O, RQ extends O, RE extends RQ, S extends notArr> = {
5
- localField: Field<LQ, S>;
6
- foreignField: Field<RQ, S>;
7
- from: SnapshotStreamExecutionResult<RQ, RE>;
8
- as: AsLiteral<As>;
9
- };
10
- export declare const $lookup: <As extends string, LQ extends doc, RQ extends O, RE extends RQ & doc, S extends notArr>(p: Params<As, LQ, RQ, RE, S>) => <LE extends LQ>(l: SnapshotStream<LQ, LE>) => SnapshotStream<LQ, LE & RORec<As, RE>>;
11
- export declare const $outerLookup: <As extends string, LQ extends doc, RQ extends O, RE extends RQ & doc, S extends notArr>(p: Params<As, LQ, RQ, RE, S>) => <LE extends LQ>(l: SnapshotStream<LQ, LE>) => SnapshotStream<LQ, LE & RORec<As, RE | null>>;
12
- export {};
@@ -1,3 +0,0 @@
1
- import type { doc } from '../../../types';
2
- import type { Delta, Expr } from '../../types';
3
- export declare const $matchDelta: <T extends doc>(query: Expr<boolean, T>) => import("../../types").RawStages<unknown, Delta<T>, Delta<T>, unknown, number>;
@@ -1,3 +0,0 @@
1
- import type { doc } from '../../../types';
2
- import type { DeltaStages, Expr } from '../../types';
3
- export declare const $match: <T extends doc>(query: Expr<boolean, T>) => DeltaStages<T, T, T>;
@@ -1,2 +0,0 @@
1
- export { $matchDelta } from './$match-delta';
2
- export { $match } from './$match';
@@ -1,13 +0,0 @@
1
- import type { Arr, O, Rec } from '../../types';
2
- import type { Accumulators, Expr, LookupArgs, Query, RawStages } from '../types';
3
- import type { Updater } from '../update/updater';
4
- export * from './raws';
5
- type s = string;
6
- export declare const $match_: <Q, T extends Q & O, C = unknown>(query?: Query<T, C>) => RawStages<O<T>, T, T, C>;
7
- export declare const $set_: <Q, T extends Q & O, V extends Q & O, C = unknown>(updater: Updater<T, T, V, C>) => RawStages<O<Q>, T, V, C, 1>;
8
- export declare const $replaceWith_: <T extends O, V extends O, C = unknown>(expr: Expr<V, T, C>) => RawStages<O, T, V, C, 1>;
9
- export declare const $unwind_: <T extends O, K extends s, U, Null extends null = never>(k: K, includeNull?: Null) => RawStages<O<T>, T & Rec<K, Arr<U>>, T & Rec<K, Null | U>>;
10
- export declare const $group_: <V extends O>() => <ID, T extends O, C = unknown>(id: Expr<ID, T, C>, args: Accumulators<T, V, C>) => RawStages<O, T, Rec<"_id", ID> & V, C, 1>;
11
- export declare const $documents_: <T extends O, C>(docs: Expr<Arr<T>, unknown, C>) => RawStages<unknown, null, T, C, 1>;
12
- export declare const $project_: <T extends O, K extends import("../..").StrKey<T>>(projection: import("../utils/map-object").ExactKeys<K>) => RawStages<T, T, T, unknown, 1>;
13
- export declare const $simpleLookup_: <T extends O, U extends O, R, K extends s, Ctx, C = unknown, S = string>(args: LookupArgs<T, U, R, K, Ctx, C, S>) => RawStages<O<T>, T, T & Rec<K, Arr<U>>, C, 1>;