AGGREGATE.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.transformReply = exports.pushAggregatehOptions = exports.transformArguments = exports.IS_READ_ONLY = exports.FIRST_KEY_INDEX = exports.AggregateGroupByReducers = exports.AggregateSteps = void 0;
  4. const generic_transformers_1 = require("@redis/client/dist/lib/commands/generic-transformers");
  5. const _1 = require(".");
  6. var AggregateSteps;
  7. (function (AggregateSteps) {
  8. AggregateSteps["GROUPBY"] = "GROUPBY";
  9. AggregateSteps["SORTBY"] = "SORTBY";
  10. AggregateSteps["APPLY"] = "APPLY";
  11. AggregateSteps["LIMIT"] = "LIMIT";
  12. AggregateSteps["FILTER"] = "FILTER";
  13. })(AggregateSteps || (exports.AggregateSteps = AggregateSteps = {}));
  14. var AggregateGroupByReducers;
  15. (function (AggregateGroupByReducers) {
  16. AggregateGroupByReducers["COUNT"] = "COUNT";
  17. AggregateGroupByReducers["COUNT_DISTINCT"] = "COUNT_DISTINCT";
  18. AggregateGroupByReducers["COUNT_DISTINCTISH"] = "COUNT_DISTINCTISH";
  19. AggregateGroupByReducers["SUM"] = "SUM";
  20. AggregateGroupByReducers["MIN"] = "MIN";
  21. AggregateGroupByReducers["MAX"] = "MAX";
  22. AggregateGroupByReducers["AVG"] = "AVG";
  23. AggregateGroupByReducers["STDDEV"] = "STDDEV";
  24. AggregateGroupByReducers["QUANTILE"] = "QUANTILE";
  25. AggregateGroupByReducers["TOLIST"] = "TOLIST";
  26. AggregateGroupByReducers["TO_LIST"] = "TOLIST";
  27. AggregateGroupByReducers["FIRST_VALUE"] = "FIRST_VALUE";
  28. AggregateGroupByReducers["RANDOM_SAMPLE"] = "RANDOM_SAMPLE";
  29. })(AggregateGroupByReducers || (exports.AggregateGroupByReducers = AggregateGroupByReducers = {}));
  30. exports.FIRST_KEY_INDEX = 1;
  31. exports.IS_READ_ONLY = true;
  32. function transformArguments(index, query, options) {
  33. return pushAggregatehOptions(['FT.AGGREGATE', index, query], options);
  34. }
  35. exports.transformArguments = transformArguments;
  36. function pushAggregatehOptions(args, options) {
  37. if (options?.VERBATIM) {
  38. args.push('VERBATIM');
  39. }
  40. if (options?.ADDSCORES) {
  41. args.push('ADDSCORES');
  42. }
  43. if (options?.LOAD) {
  44. args.push('LOAD');
  45. (0, _1.pushArgumentsWithLength)(args, () => {
  46. if (Array.isArray(options.LOAD)) {
  47. for (const load of options.LOAD) {
  48. pushLoadField(args, load);
  49. }
  50. }
  51. else {
  52. pushLoadField(args, options.LOAD);
  53. }
  54. });
  55. }
  56. if (options?.STEPS) {
  57. for (const step of options.STEPS) {
  58. switch (step.type) {
  59. case AggregateSteps.GROUPBY:
  60. args.push('GROUPBY');
  61. if (!step.properties) {
  62. args.push('0');
  63. }
  64. else {
  65. (0, generic_transformers_1.pushVerdictArgument)(args, step.properties);
  66. }
  67. if (Array.isArray(step.REDUCE)) {
  68. for (const reducer of step.REDUCE) {
  69. pushGroupByReducer(args, reducer);
  70. }
  71. }
  72. else {
  73. pushGroupByReducer(args, step.REDUCE);
  74. }
  75. break;
  76. case AggregateSteps.SORTBY:
  77. (0, _1.pushSortByArguments)(args, 'SORTBY', step.BY);
  78. if (step.MAX) {
  79. args.push('MAX', step.MAX.toString());
  80. }
  81. break;
  82. case AggregateSteps.APPLY:
  83. args.push('APPLY', step.expression, 'AS', step.AS);
  84. break;
  85. case AggregateSteps.LIMIT:
  86. args.push('LIMIT', step.from.toString(), step.size.toString());
  87. break;
  88. case AggregateSteps.FILTER:
  89. args.push('FILTER', step.expression);
  90. break;
  91. }
  92. }
  93. }
  94. (0, _1.pushParamsArgs)(args, options?.PARAMS);
  95. if (options?.DIALECT) {
  96. args.push('DIALECT', options.DIALECT.toString());
  97. }
  98. if (options?.TIMEOUT !== undefined) {
  99. args.push('TIMEOUT', options.TIMEOUT.toString());
  100. }
  101. return args;
  102. }
  103. exports.pushAggregatehOptions = pushAggregatehOptions;
  104. function pushLoadField(args, toLoad) {
  105. if (typeof toLoad === 'string') {
  106. args.push(toLoad);
  107. }
  108. else {
  109. args.push(toLoad.identifier);
  110. if (toLoad.AS) {
  111. args.push('AS', toLoad.AS);
  112. }
  113. }
  114. }
  115. function pushGroupByReducer(args, reducer) {
  116. args.push('REDUCE', reducer.type);
  117. switch (reducer.type) {
  118. case AggregateGroupByReducers.COUNT:
  119. args.push('0');
  120. break;
  121. case AggregateGroupByReducers.COUNT_DISTINCT:
  122. case AggregateGroupByReducers.COUNT_DISTINCTISH:
  123. case AggregateGroupByReducers.SUM:
  124. case AggregateGroupByReducers.MIN:
  125. case AggregateGroupByReducers.MAX:
  126. case AggregateGroupByReducers.AVG:
  127. case AggregateGroupByReducers.STDDEV:
  128. case AggregateGroupByReducers.TOLIST:
  129. args.push('1', reducer.property);
  130. break;
  131. case AggregateGroupByReducers.QUANTILE:
  132. args.push('2', reducer.property, reducer.quantile.toString());
  133. break;
  134. case AggregateGroupByReducers.FIRST_VALUE: {
  135. (0, _1.pushArgumentsWithLength)(args, () => {
  136. args.push(reducer.property);
  137. if (reducer.BY) {
  138. args.push('BY');
  139. if (typeof reducer.BY === 'string') {
  140. args.push(reducer.BY);
  141. }
  142. else {
  143. args.push(reducer.BY.property);
  144. if (reducer.BY.direction) {
  145. args.push(reducer.BY.direction);
  146. }
  147. }
  148. }
  149. });
  150. break;
  151. }
  152. case AggregateGroupByReducers.RANDOM_SAMPLE:
  153. args.push('2', reducer.property, reducer.sampleSize.toString());
  154. break;
  155. }
  156. if (reducer.AS) {
  157. args.push('AS', reducer.AS);
  158. }
  159. }
  160. function transformReply(rawReply) {
  161. const results = [];
  162. for (let i = 1; i < rawReply.length; i++) {
  163. results.push((0, generic_transformers_1.transformTuplesReply)(rawReply[i]));
  164. }
  165. return {
  166. total: rawReply[0],
  167. results
  168. };
  169. }
  170. exports.transformReply = transformReply;