@omegup/msync 0.0.37 → 0.0.39

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 (4) hide show
  1. package/index.d.ts +1 -1
  2. package/index.esm.js +372 -374
  3. package/index.js +372 -374
  4. package/package.json +1 -1
package/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
- var synchronousPromise = require('synchronous-promise');
4
3
  var mongodb = require('mongodb');
4
+ var synchronousPromise = require('synchronous-promise');
5
5
  var crypto$1 = require('crypto');
6
6
  var promises = require('fs/promises');
7
7
 
@@ -648,131 +648,390 @@ const $setDelta = (updater) => {
648
648
  .with($setEach(k => to(nil), dict)).stages;
649
649
  };
650
650
 
651
- const $unwindDelta = (k1, k2, k, includeNull) => {
652
- const outer = includeNull === null;
653
- const newItems = {
654
- $filter: {
655
- input: { $ifNull: [`$after.${k2}`, []] },
656
- as: 'a',
657
- cond: {
658
- $not: {
659
- $in: [
660
- '$$a._id',
651
+ const dayAndMonthPart = (date) => asExpr({
652
+ raw: f => asExprRaw({ $dateToString: { date: date.raw(f).get(), format: '%m-%d' } }),
653
+ });
654
+ const now = () => asExpr({
655
+ raw: f => asExprRaw('$$NOW'),
656
+ });
657
+ const monthPart = (date) => asExpr({
658
+ raw: f => asExprRaw({ $dateToString: { date: date.raw(f).get(), format: '%Y-%m' } }),
659
+ });
660
+ const weekPart = (date) => asExpr({
661
+ raw: f => asExprRaw({ $dateToString: { date: date.raw(f).get(), format: '%Y-%U' } }),
662
+ });
663
+ const year = (date) => asExpr({
664
+ raw: f => asExprRaw({ $year: date.raw(f).get() }),
665
+ });
666
+ const dateAdd = (date, amount, unit) => asExpr({
667
+ raw: f => asExprRaw({
668
+ $dateAdd: {
669
+ startDate: date.raw(f).get(),
670
+ unit: unit.raw(f).get(),
671
+ amount: amount.raw(f).get(),
672
+ },
673
+ }),
674
+ });
675
+ const maxDate = (expr) => asExpr({
676
+ raw: f => asExprRaw({ $max: expr.raw(f).get() }),
677
+ });
678
+ const minDate = (expr) => asExpr({
679
+ raw: f => asExprRaw({ $min: expr.raw(f).get() }),
680
+ });
681
+ const datePart = (date) => asExpr({
682
+ raw: f => asExprRaw({ $dateToString: { date: date.raw(f).get(), format: '%Y-%m-%d' } }),
683
+ });
684
+ const startOf = (startDate, freq, offset) => asExpr({
685
+ raw: f => asExprRaw({
686
+ $let: {
687
+ vars: { d: startDate.raw(f).get(), f: freq.raw(f).get(), i: offset.raw(f).get() },
688
+ in: {
689
+ $switch: {
690
+ branches: [
661
691
  {
662
- $map: {
663
- input: { $ifNull: [`$before.${k2}`, []] },
664
- as: 'b',
665
- in: '$$b._id',
692
+ case: { $eq: ['$$f', 'week'] },
693
+ then: {
694
+ $dateFromParts: {
695
+ isoWeekYear: { $isoWeekYear: '$$d' },
696
+ isoWeek: {
697
+ $add: [
698
+ { $isoWeek: '$$d' },
699
+ {
700
+ $cond: {
701
+ if: {
702
+ $lt: ['$$i', { $isoDayOfWeek: '$$d' }],
703
+ },
704
+ then: 1,
705
+ else: 0,
706
+ },
707
+ },
708
+ ],
709
+ },
710
+ isoDayOfWeek: '$$i',
711
+ },
666
712
  },
667
713
  },
668
- ],
669
- },
670
- },
671
- },
672
- };
673
- const oldItems = {
674
- $filter: {
675
- input: {
676
- $map: {
677
- input: { $ifNull: [`$before.${k2}`, []] },
678
- as: 'b',
679
- in: {
680
- before: '$$b',
681
- after: {
682
- $ifNull: [
683
- {
684
- $first: {
685
- $filter: {
686
- input: `$after.${k2}`,
687
- as: 'a',
688
- cond: { $eq: ['$$a._id', '$$b._id'] },
689
- },
714
+ {
715
+ case: { $eq: ['$$f', 'month'] },
716
+ then: {
717
+ $dateFromParts: {
718
+ year: { $year: '$$d' },
719
+ month: {
720
+ $add: [
721
+ { $month: '$$d' },
722
+ {
723
+ $cond: {
724
+ if: {
725
+ $lt: ['$$i', { $dayOfMonth: '$$d' }],
726
+ },
727
+ then: 1,
728
+ else: 0,
729
+ },
730
+ },
731
+ ],
690
732
  },
733
+ day: '$$i',
691
734
  },
692
- null,
693
- ],
735
+ },
736
+ },
737
+ {
738
+ case: { $eq: ['$$f', 'year'] },
739
+ then: {
740
+ $dateFromParts: {
741
+ year: {
742
+ $add: [
743
+ { $year: '$$d' },
744
+ {
745
+ $cond: {
746
+ if: {
747
+ $or: [
748
+ {
749
+ $lt: [{ $floor: { $divide: ['$$i', 100] } }, { $month: '$$d' }],
750
+ },
751
+ {
752
+ $and: [
753
+ {
754
+ $eq: [
755
+ { $floor: { $divide: ['$$i', 100] } },
756
+ { $month: '$$d' },
757
+ ],
758
+ },
759
+ {
760
+ $lt: [{ $mod: ['$$i', 100] }, { $dayOfMonth: '$$d' }],
761
+ },
762
+ ],
763
+ },
764
+ ],
765
+ },
766
+ then: 1,
767
+ else: 0,
768
+ },
769
+ },
770
+ ],
771
+ },
772
+ month: { $floor: { $divide: ['$$i', 100] } },
773
+ day: { $mod: ['$$i', 100] },
774
+ },
775
+ },
776
+ },
777
+ ],
778
+ default: {
779
+ $dateFromParts: {
780
+ year: { $year: '$$d' },
781
+ month: { $month: '$$d' },
782
+ day: { $add: [{ $dayOfMonth: '$$d' }, '$$i'] },
694
783
  },
695
784
  },
696
785
  },
697
786
  },
698
- as: 'a',
699
- cond: { $ne: ['$$a.before', '$$a.after'] },
700
787
  },
701
- };
702
- const ifNull = (k, part, str = `$${k}.${part}._id`) => outer && k == k2 ? { $ifNull: [str, 'null'] } : str;
703
- const interDot = ([a, b]) => [a, '.', b];
704
- const partReplace = (part) => ({
705
- $cond: {
706
- if: { $or: [{ $eq: [`$${k1}.${part}`, null] }, { $eq: [`$${k2}.${part}`, null] }] },
707
- then: null,
708
- else: {
709
- _id: k
710
- ? ifNull(k, part)
711
- : {
712
- $concat: interDot([k1, k2].sort().map(k => ifNull(k, part))),
713
- },
714
- [k1]: `$${k1}.${part}`,
715
- [k2]: outer
716
- ? {
717
- $cond: {
718
- if: `$${k2}.${part}._id`,
719
- then: `$${k2}.${part}`,
720
- else: null,
721
- },
722
- }
723
- : `$${k2}.${part}`,
788
+ }),
789
+ });
790
+ const dateDiff = ({ end, unit, start, }) => asExpr({
791
+ raw: f => asExprRaw({
792
+ $let: {
793
+ vars: {
794
+ f: unit.raw(f).get(),
795
+ end: end.raw(f).get(),
796
+ start: start.raw(f).get(),
724
797
  },
725
- },
726
- });
727
- const stages = link()
728
- .with(asStages([
729
- {
730
- $set: {
731
- [k1]: {
732
- before: { $ifNull: [`$before.${k1}`, null] },
733
- after: { $ifNull: [`$after.${k1}`, null] },
734
- },
735
- [k2]: {
736
- $concatArrays: [
737
- outer
738
- ? {
739
- $cond: {
740
- if: { $eq: [`$before.${k2}`, []] },
798
+ in: {
799
+ $let: {
800
+ vars: {
801
+ diff: {
802
+ $dateDiff: {
803
+ startDate: '$$start',
804
+ endDate: '$$end',
805
+ unit: '$$f',
806
+ },
807
+ },
808
+ },
809
+ in: {
810
+ $switch: {
811
+ branches: [
812
+ {
813
+ case: { $eq: ['$$f', 'month'] },
741
814
  then: {
742
815
  $cond: {
743
- if: { $eq: [`$after.${k2}`, []] },
744
- then: [],
745
- else: [{ before: {}, after: null }],
816
+ if: {
817
+ $lte: [{ $dayOfMonth: '$$start' }, { $dayOfMonth: '$$end' }],
818
+ },
819
+ then: '$$diff',
820
+ else: { $subtract: ['$$diff', 1] },
746
821
  },
747
822
  },
748
- else: oldItems,
749
823
  },
750
- }
751
- : oldItems,
752
- {
753
- $map: {
754
- input: outer
755
- ? {
824
+ {
825
+ case: { $eq: ['$$f', 'week'] },
826
+ then: {
756
827
  $cond: {
757
828
  if: {
758
- $and: [{ $ne: [`$before.${k2}`, []] }, { $eq: [`$after.${k2}`, []] }],
829
+ $lte: [{ $isoDayOfWeek: '$$start' }, { $isoDayOfWeek: '$$end' }],
759
830
  },
760
- then: [{}],
761
- else: newItems,
831
+ then: '$$diff',
832
+ else: { $subtract: ['$$diff', 1] },
762
833
  },
763
- }
764
- : newItems,
765
- as: 'a',
766
- in: { before: null, after: '$$a' },
767
- },
768
- },
769
- ],
770
- },
771
- },
772
- },
773
- ]))
774
- .with($unwind_(k2))
775
- .with(asStages([
834
+ },
835
+ },
836
+ {
837
+ case: { $eq: ['$$f', 'year'] },
838
+ then: {
839
+ $cond: {
840
+ if: {
841
+ $or: [
842
+ {
843
+ $lt: [{ $month: '$$start' }, { $month: '$$end' }],
844
+ },
845
+ {
846
+ $and: [
847
+ {
848
+ $eq: [{ $month: '$$start' }, { $month: '$$end' }],
849
+ },
850
+ {
851
+ $lte: [{ $dayOfMonth: '$$start' }, { $dayOfMonth: '$$end' }],
852
+ },
853
+ ],
854
+ },
855
+ ],
856
+ },
857
+ then: '$$diff',
858
+ else: { $subtract: ['$$diff', 1] },
859
+ },
860
+ },
861
+ },
862
+ ],
863
+ default: '$$diff',
864
+ },
865
+ },
866
+ },
867
+ },
868
+ },
869
+ }),
870
+ });
871
+
872
+ const range = (start, end, step) => asExpr({
873
+ raw: f => asExprRaw({ $range: [start.raw(f).get(), end.raw(f).get(), step?.raw(f).get() ?? 1] }),
874
+ });
875
+ const $map1 = (ex, map) => asExpr({
876
+ raw: f => asExprRaw({
877
+ $map: {
878
+ input: ex.raw(f).get(),
879
+ as: 'item',
880
+ in: map(ctx()('item')).raw(f).get(),
881
+ },
882
+ }),
883
+ });
884
+ const $map = (ex, map) => $map1(ex, i => map(i.expr()));
885
+
886
+ const operator = () => (op) => (operand) => {
887
+ return {
888
+ raw: { [op]: operand },
889
+ expr: (field) => asExpr({
890
+ raw: f => asExprRaw({ [op]: [$ifNull(field.expr(), nil).raw(f).get(), operand] }),
891
+ }),
892
+ };
893
+ };
894
+
895
+ const dualEq = operator();
896
+ const $eq = dualEq('$eq');
897
+ const $ne = dualEq('$ne');
898
+ const comp = operator();
899
+ const $gt = comp('$gt');
900
+ const $gtTs = comp('$gt');
901
+ const $gteTs = comp('$gte');
902
+ const $lt = comp('$lt');
903
+ const dateLt = comp('$lt');
904
+ const $gte = comp('$gte');
905
+ const $lte = comp('$lte');
906
+
907
+ const $expr = (expr) => ({
908
+ raw: f => ({ $expr: expr.raw(f).get() }),
909
+ });
910
+
911
+ const dualIn = operator();
912
+ const $in = dualIn('$in');
913
+ const $nin = dualIn('$nin');
914
+
915
+ const $unwindDelta = (k1, k2, k, includeNull) => {
916
+ const outer = includeNull === null;
917
+ const newItems = {
918
+ $filter: {
919
+ input: { $ifNull: [`$after.${k2}`, []] },
920
+ as: 'a',
921
+ cond: {
922
+ $not: {
923
+ $in: [
924
+ '$$a._id',
925
+ {
926
+ $map: {
927
+ input: { $ifNull: [`$before.${k2}`, []] },
928
+ as: 'b',
929
+ in: '$$b._id',
930
+ },
931
+ },
932
+ ],
933
+ },
934
+ },
935
+ },
936
+ };
937
+ const oldItems = {
938
+ $map: {
939
+ input: { $ifNull: [`$before.${k2}`, []] },
940
+ as: 'b',
941
+ in: {
942
+ before: '$$b',
943
+ after: {
944
+ $ifNull: [
945
+ {
946
+ $first: {
947
+ $filter: {
948
+ input: `$after.${k2}`,
949
+ as: 'a',
950
+ cond: { $eq: ['$$a._id', '$$b._id'] },
951
+ },
952
+ },
953
+ },
954
+ null,
955
+ ],
956
+ },
957
+ },
958
+ },
959
+ };
960
+ const ifNull = (k, part, str = `$${k}.${part}._id`) => outer && k == k2 ? { $ifNull: [str, 'null'] } : str;
961
+ const interDot = ([a, b]) => [a, '.', b];
962
+ const partReplace = (part) => ({
963
+ $cond: {
964
+ if: { $or: [{ $eq: [`$${k1}.${part}`, null] }, { $eq: [`$${k2}.${part}`, null] }] },
965
+ then: null,
966
+ else: {
967
+ _id: k
968
+ ? ifNull(k, part)
969
+ : {
970
+ $concat: interDot([k1, k2].sort().map(k => ifNull(k, part))),
971
+ },
972
+ [k1]: `$${k1}.${part}`,
973
+ [k2]: outer
974
+ ? {
975
+ $cond: {
976
+ if: `$${k2}.${part}._id`,
977
+ then: `$${k2}.${part}`,
978
+ else: null,
979
+ },
980
+ }
981
+ : `$${k2}.${part}`,
982
+ },
983
+ },
984
+ });
985
+ const part = (k) => root().of(k).expr();
986
+ const stages = link()
987
+ .with(asStages([
988
+ {
989
+ $set: {
990
+ [k1]: {
991
+ before: { $ifNull: [`$before.${k1}`, null] },
992
+ after: { $ifNull: [`$after.${k1}`, null] },
993
+ },
994
+ [k2]: {
995
+ $concatArrays: [
996
+ outer
997
+ ? {
998
+ $cond: {
999
+ if: { $eq: [`$before.${k2}`, []] },
1000
+ then: {
1001
+ $cond: {
1002
+ if: { $eq: [`$after.${k2}`, []] },
1003
+ then: [],
1004
+ else: [{ before: {}, after: null }],
1005
+ },
1006
+ },
1007
+ else: oldItems,
1008
+ },
1009
+ }
1010
+ : oldItems,
1011
+ {
1012
+ $map: {
1013
+ input: outer
1014
+ ? {
1015
+ $cond: {
1016
+ if: {
1017
+ $and: [{ $ne: [`$before.${k2}`, []] }, { $eq: [`$after.${k2}`, []] }],
1018
+ },
1019
+ then: [{}],
1020
+ else: newItems,
1021
+ },
1022
+ }
1023
+ : newItems,
1024
+ as: 'a',
1025
+ in: { before: null, after: '$$a' },
1026
+ },
1027
+ },
1028
+ ],
1029
+ },
1030
+ },
1031
+ },
1032
+ ]))
1033
+ .with($unwind_(k2))
1034
+ .with(asStages([
776
1035
  {
777
1036
  $replaceWith: {
778
1037
  _id: '$_id',
@@ -780,7 +1039,8 @@ const $unwindDelta = (k1, k2, k, includeNull) => {
780
1039
  after: partReplace('after'),
781
1040
  },
782
1041
  },
783
- ])).stages;
1042
+ ]))
1043
+ .with($match_($expr(ne(part('before'))(part('after'))))).stages;
784
1044
  return stages;
785
1045
  };
786
1046
 
@@ -829,10 +1089,6 @@ const $lookupDelta = ({ field1, field2 }, { coll, exec, input }, k1, k2, k, incl
829
1089
  })).with($unwindDelta(k1, k2, k, includeNull)).stages;
830
1090
  };
831
1091
 
832
- const $expr = (expr) => ({
833
- raw: f => ({ $expr: expr.raw(f).get() }),
834
- });
835
-
836
1092
  const $lookupRaw = ({ field1, field2 }, { coll, exec, input }, k2, k) => (f) => {
837
1093
  root().of('_id').expr();
838
1094
  root().of(k2).of('_id').expr();
@@ -960,8 +1216,7 @@ const runCont = async (it, cb) => {
960
1216
  return runCont(stop, cb);
961
1217
  }
962
1218
  const { cont, info } = res.next;
963
- const stopped = cb?.(info);
964
- if (stopped) {
1219
+ if (cb?.(info)) {
965
1220
  await clear();
966
1221
  throw new Error('Machine stopped');
967
1222
  }
@@ -1014,31 +1269,6 @@ const $outerLookup = (p, outer) => (l) => $lookup1({
1014
1269
  left: l(emptyDelta()),
1015
1270
  }, outer);
1016
1271
 
1017
- const operator = () => (op) => (operand) => {
1018
- return {
1019
- raw: { [op]: operand },
1020
- expr: (field) => asExpr({
1021
- raw: f => asExprRaw({ [op]: [$ifNull(field.expr(), nil).raw(f).get(), operand] }),
1022
- }),
1023
- };
1024
- };
1025
-
1026
- const dualEq = operator();
1027
- const $eq = dualEq('$eq');
1028
- const $ne = dualEq('$ne');
1029
- const comp = operator();
1030
- const $gt = comp('$gt');
1031
- const $gtTs = comp('$gt');
1032
- const $gteTs = comp('$gte');
1033
- const $lt = comp('$lt');
1034
- const dateLt = comp('$lt');
1035
- const $gte = comp('$gte');
1036
- const $lte = comp('$lte');
1037
-
1038
- const dualIn = operator();
1039
- const $in = dualIn('$in');
1040
- const $nin = dualIn('$nin');
1041
-
1042
1272
  const make = (alter) => {
1043
1273
  return (op, args) => alter(op, args.filter(defined));
1044
1274
  };
@@ -1396,7 +1626,10 @@ const executes$1 = (view, input, streamName) => {
1396
1626
  return step8(l);
1397
1627
  };
1398
1628
  const step8 = (l) => {
1399
- return nextData(l.aggResult.cursor.firstBatch)(() => l.stream.tryNext().then(doc => (doc ? next(step2, 'restart') : step8(l))), 'wait for change');
1629
+ return nextData(l.aggResult.cursor.firstBatch)(() => l.stream
1630
+ .tryNext()
1631
+ .catch(_ => 1)
1632
+ .then(doc => (doc ? next(step2, 'restart') : step8(l))), 'wait for change');
1400
1633
  };
1401
1634
  return stop;
1402
1635
  };
@@ -1517,241 +1750,6 @@ const executes = (view, input, streamName) => {
1517
1750
  const emptyLin = () => ({ lin: link().stages });
1518
1751
  const from = (view, streamName) => pipe(input => executes(view, input.lin, streamName), { lin: link().stages }, ({ lin: a }, { lin: b }) => ({ lin: concatStages(a, b) }), emptyLin);
1519
1752
 
1520
- const dayAndMonthPart = (date) => asExpr({
1521
- raw: f => asExprRaw({ $dateToString: { date: date.raw(f).get(), format: '%m-%d' } }),
1522
- });
1523
- const now = () => asExpr({
1524
- raw: f => asExprRaw('$$NOW'),
1525
- });
1526
- const monthPart = (date) => asExpr({
1527
- raw: f => asExprRaw({ $dateToString: { date: date.raw(f).get(), format: '%Y-%m' } }),
1528
- });
1529
- const weekPart = (date) => asExpr({
1530
- raw: f => asExprRaw({ $dateToString: { date: date.raw(f).get(), format: '%Y-%U' } }),
1531
- });
1532
- const year = (date) => asExpr({
1533
- raw: f => asExprRaw({ $year: date.raw(f).get() }),
1534
- });
1535
- const dateAdd = (date, amount, unit) => asExpr({
1536
- raw: f => asExprRaw({
1537
- $dateAdd: {
1538
- startDate: date.raw(f).get(),
1539
- unit: unit.raw(f).get(),
1540
- amount: amount.raw(f).get(),
1541
- },
1542
- }),
1543
- });
1544
- const maxDate = (expr) => asExpr({
1545
- raw: f => asExprRaw({ $max: expr.raw(f).get() }),
1546
- });
1547
- const minDate = (expr) => asExpr({
1548
- raw: f => asExprRaw({ $min: expr.raw(f).get() }),
1549
- });
1550
- const datePart = (date) => asExpr({
1551
- raw: f => asExprRaw({ $dateToString: { date: date.raw(f).get(), format: '%Y-%m-%d' } }),
1552
- });
1553
- const startOf = (startDate, freq, offset) => asExpr({
1554
- raw: f => asExprRaw({
1555
- $let: {
1556
- vars: { d: startDate.raw(f).get(), f: freq.raw(f).get(), i: offset.raw(f).get() },
1557
- in: {
1558
- $switch: {
1559
- branches: [
1560
- {
1561
- case: { $eq: ['$$f', 'week'] },
1562
- then: {
1563
- $dateFromParts: {
1564
- isoWeekYear: { $isoWeekYear: '$$d' },
1565
- isoWeek: {
1566
- $add: [
1567
- { $isoWeek: '$$d' },
1568
- {
1569
- $cond: {
1570
- if: {
1571
- $lt: ['$$i', { $isoDayOfWeek: '$$d' }],
1572
- },
1573
- then: 1,
1574
- else: 0,
1575
- },
1576
- },
1577
- ],
1578
- },
1579
- isoDayOfWeek: '$$i',
1580
- },
1581
- },
1582
- },
1583
- {
1584
- case: { $eq: ['$$f', 'month'] },
1585
- then: {
1586
- $dateFromParts: {
1587
- year: { $year: '$$d' },
1588
- month: {
1589
- $add: [
1590
- { $month: '$$d' },
1591
- {
1592
- $cond: {
1593
- if: {
1594
- $lt: ['$$i', { $dayOfMonth: '$$d' }],
1595
- },
1596
- then: 1,
1597
- else: 0,
1598
- },
1599
- },
1600
- ],
1601
- },
1602
- day: '$$i',
1603
- },
1604
- },
1605
- },
1606
- {
1607
- case: { $eq: ['$$f', 'year'] },
1608
- then: {
1609
- $dateFromParts: {
1610
- year: {
1611
- $add: [
1612
- { $year: '$$d' },
1613
- {
1614
- $cond: {
1615
- if: {
1616
- $or: [
1617
- {
1618
- $lt: [{ $floor: { $divide: ['$$i', 100] } }, { $month: '$$d' }],
1619
- },
1620
- {
1621
- $and: [
1622
- {
1623
- $eq: [
1624
- { $floor: { $divide: ['$$i', 100] } },
1625
- { $month: '$$d' },
1626
- ],
1627
- },
1628
- {
1629
- $lt: [{ $mod: ['$$i', 100] }, { $dayOfMonth: '$$d' }],
1630
- },
1631
- ],
1632
- },
1633
- ],
1634
- },
1635
- then: 1,
1636
- else: 0,
1637
- },
1638
- },
1639
- ],
1640
- },
1641
- month: { $floor: { $divide: ['$$i', 100] } },
1642
- day: { $mod: ['$$i', 100] },
1643
- },
1644
- },
1645
- },
1646
- ],
1647
- default: {
1648
- $dateFromParts: {
1649
- year: { $year: '$$d' },
1650
- month: { $month: '$$d' },
1651
- day: { $add: [{ $dayOfMonth: '$$d' }, '$$i'] },
1652
- },
1653
- },
1654
- },
1655
- },
1656
- },
1657
- }),
1658
- });
1659
- const dateDiff = ({ end, unit, start, }) => asExpr({
1660
- raw: f => asExprRaw({
1661
- $let: {
1662
- vars: {
1663
- f: unit.raw(f).get(),
1664
- end: end.raw(f).get(),
1665
- start: start.raw(f).get(),
1666
- },
1667
- in: {
1668
- $let: {
1669
- vars: {
1670
- diff: {
1671
- $dateDiff: {
1672
- startDate: '$$start',
1673
- endDate: '$$end',
1674
- unit: '$$f',
1675
- },
1676
- },
1677
- },
1678
- in: {
1679
- $switch: {
1680
- branches: [
1681
- {
1682
- case: { $eq: ['$$f', 'month'] },
1683
- then: {
1684
- $cond: {
1685
- if: {
1686
- $lte: [{ $dayOfMonth: '$$start' }, { $dayOfMonth: '$$end' }],
1687
- },
1688
- then: '$$diff',
1689
- else: { $subtract: ['$$diff', 1] },
1690
- },
1691
- },
1692
- },
1693
- {
1694
- case: { $eq: ['$$f', 'week'] },
1695
- then: {
1696
- $cond: {
1697
- if: {
1698
- $lte: [{ $isoDayOfWeek: '$$start' }, { $isoDayOfWeek: '$$end' }],
1699
- },
1700
- then: '$$diff',
1701
- else: { $subtract: ['$$diff', 1] },
1702
- },
1703
- },
1704
- },
1705
- {
1706
- case: { $eq: ['$$f', 'year'] },
1707
- then: {
1708
- $cond: {
1709
- if: {
1710
- $or: [
1711
- {
1712
- $lt: [{ $month: '$$start' }, { $month: '$$end' }],
1713
- },
1714
- {
1715
- $and: [
1716
- {
1717
- $eq: [{ $month: '$$start' }, { $month: '$$end' }],
1718
- },
1719
- {
1720
- $lte: [{ $dayOfMonth: '$$start' }, { $dayOfMonth: '$$end' }],
1721
- },
1722
- ],
1723
- },
1724
- ],
1725
- },
1726
- then: '$$diff',
1727
- else: { $subtract: ['$$diff', 1] },
1728
- },
1729
- },
1730
- },
1731
- ],
1732
- default: '$$diff',
1733
- },
1734
- },
1735
- },
1736
- },
1737
- },
1738
- }),
1739
- });
1740
-
1741
- const range = (start, end, step) => asExpr({
1742
- raw: f => asExprRaw({ $range: [start.raw(f).get(), end.raw(f).get(), step?.raw(f).get() ?? 1] }),
1743
- });
1744
- const $map1 = (ex, map) => asExpr({
1745
- raw: f => asExprRaw({
1746
- $map: {
1747
- input: ex.raw(f).get(),
1748
- as: 'item',
1749
- in: map(ctx()('item')).raw(f).get(),
1750
- },
1751
- }),
1752
- });
1753
- const $map = (ex, map) => $map1(ex, i => map(i.expr()));
1754
-
1755
1753
  require('dotenv').config();
1756
1754
  const uri = process.env['MONGO_URL'];
1757
1755