aggregate.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const index_1 = require("./index");
  4. const bson_1 = require("bson");
  5. const query_1 = require("./serializer/query");
  6. const utils_1 = require("./utils/utils");
  7. const type_1 = require("./utils/type");
  8. class Aggregation {
  9. constructor(db, collectionName) {
  10. this._stages = [];
  11. if (db && collectionName) {
  12. this._db = db;
  13. this._request = new index_1.Db.reqClass(this._db.config);
  14. this._collectionName = collectionName;
  15. }
  16. }
  17. async end() {
  18. if (!this._collectionName || !this._db) {
  19. throw new Error('Aggregation pipeline cannot send request');
  20. }
  21. const result = await this._request.send('database.aggregateDocuments', {
  22. collectionName: this._collectionName,
  23. stages: this._stages
  24. });
  25. if (result && result.data && result.data.list) {
  26. return {
  27. requestId: result.requestId,
  28. data: result.data.list.map(bson_1.EJSON.parse)
  29. };
  30. }
  31. return result;
  32. }
  33. unwrap() {
  34. return this._stages;
  35. }
  36. done() {
  37. return this._stages.map(({ stageKey, stageValue }) => {
  38. return {
  39. [stageKey]: JSON.parse(stageValue)
  40. };
  41. });
  42. }
  43. _pipe(stage, param) {
  44. let transformParam = '';
  45. if (type_1.getType(param) === 'object') {
  46. transformParam = utils_1.stringifyByEJSON(param);
  47. }
  48. else {
  49. transformParam = JSON.stringify(param);
  50. }
  51. this._stages.push({
  52. stageKey: `$${stage}`,
  53. stageValue: transformParam
  54. });
  55. return this;
  56. }
  57. addFields(param) {
  58. return this._pipe('addFields', param);
  59. }
  60. bucket(param) {
  61. return this._pipe('bucket', param);
  62. }
  63. bucketAuto(param) {
  64. return this._pipe('bucketAuto', param);
  65. }
  66. count(param) {
  67. return this._pipe('count', param);
  68. }
  69. geoNear(param) {
  70. if (param.query) {
  71. param.query = query_1.QuerySerializer.encode(param.query);
  72. }
  73. return this._pipe('geoNear', param);
  74. }
  75. group(param) {
  76. return this._pipe('group', param);
  77. }
  78. limit(param) {
  79. return this._pipe('limit', param);
  80. }
  81. match(param) {
  82. return this._pipe('match', query_1.QuerySerializer.encode(param));
  83. }
  84. project(param) {
  85. return this._pipe('project', param);
  86. }
  87. lookup(param) {
  88. return this._pipe('lookup', param);
  89. }
  90. replaceRoot(param) {
  91. return this._pipe('replaceRoot', param);
  92. }
  93. sample(param) {
  94. return this._pipe('sample', param);
  95. }
  96. skip(param) {
  97. return this._pipe('skip', param);
  98. }
  99. sort(param) {
  100. return this._pipe('sort', param);
  101. }
  102. sortByCount(param) {
  103. return this._pipe('sortByCount', param);
  104. }
  105. unwind(param) {
  106. return this._pipe('unwind', param);
  107. }
  108. }
  109. exports.default = Aggregation;