aggregate.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. const validate_1 = require("./validate");
  9. const point_1 = require("./geo/point");
  10. class Aggregation {
  11. constructor(db, collectionName, rawPipeline) {
  12. this._stages = [];
  13. if (db && collectionName) {
  14. this._db = db;
  15. this._request = new index_1.Db.reqClass(this._db.config);
  16. this._collectionName = collectionName;
  17. if (rawPipeline && rawPipeline.length > 0) {
  18. rawPipeline.forEach((stage) => {
  19. validate_1.Validate.isValidAggregation(stage);
  20. const stageName = Object.keys(stage)[0];
  21. this._pipe(stageName, stage[stageName], true);
  22. });
  23. }
  24. }
  25. }
  26. async end() {
  27. if (!this._collectionName || !this._db) {
  28. throw new Error('Aggregation pipeline cannot send request');
  29. }
  30. const result = await this._request.send('database.aggregateDocuments', {
  31. collectionName: this._collectionName,
  32. stages: this._stages
  33. });
  34. if (result && result.data && result.data.list) {
  35. return {
  36. requestId: result.requestId,
  37. data: result.data.list.map(bson_1.EJSON.parse)
  38. };
  39. }
  40. return result;
  41. }
  42. unwrap() {
  43. return this._stages;
  44. }
  45. done() {
  46. return this._stages.map(({ stageKey, stageValue }) => {
  47. return {
  48. [stageKey]: JSON.parse(stageValue)
  49. };
  50. });
  51. }
  52. _pipe(stage, param, raw = false) {
  53. let transformParam = '';
  54. if (type_1.getType(param) === 'object') {
  55. transformParam = utils_1.stringifyByEJSON(param);
  56. }
  57. else {
  58. transformParam = JSON.stringify(param);
  59. }
  60. this._stages.push({
  61. stageKey: raw ? stage : `$${stage}`,
  62. stageValue: transformParam
  63. });
  64. return this;
  65. }
  66. addFields(param) {
  67. return this._pipe('addFields', param);
  68. }
  69. bucket(param) {
  70. return this._pipe('bucket', param);
  71. }
  72. bucketAuto(param) {
  73. return this._pipe('bucketAuto', param);
  74. }
  75. count(param) {
  76. return this._pipe('count', param);
  77. }
  78. geoNear(param) {
  79. if (param.query) {
  80. param.query = query_1.QuerySerializer.encode(param.query);
  81. }
  82. if (param.distanceMultiplier && typeof (param.distanceMultiplier) === 'number') {
  83. param.distanceMultiplier = param.distanceMultiplier;
  84. }
  85. if (param.near) {
  86. param.near = new point_1.Point(param.near.longitude, param.near.latitude).toJSON();
  87. }
  88. return this._pipe('geoNear', param);
  89. }
  90. group(param) {
  91. return this._pipe('group', param);
  92. }
  93. limit(param) {
  94. return this._pipe('limit', param);
  95. }
  96. match(param) {
  97. return this._pipe('match', query_1.QuerySerializer.encode(param));
  98. }
  99. project(param) {
  100. return this._pipe('project', param);
  101. }
  102. lookup(param) {
  103. return this._pipe('lookup', param);
  104. }
  105. replaceRoot(param) {
  106. return this._pipe('replaceRoot', param);
  107. }
  108. sample(param) {
  109. return this._pipe('sample', param);
  110. }
  111. skip(param) {
  112. return this._pipe('skip', param);
  113. }
  114. sort(param) {
  115. return this._pipe('sort', param);
  116. }
  117. sortByCount(param) {
  118. return this._pipe('sortByCount', param);
  119. }
  120. unwind(param) {
  121. return this._pipe('unwind', param);
  122. }
  123. }
  124. exports.default = Aggregation;