aggregate.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.AggregateOperation = exports.DB_AGGREGATE_COLLECTION = void 0;
  4. const responses_1 = require("../cmap/wire_protocol/responses");
  5. const error_1 = require("../error");
  6. const write_concern_1 = require("../write_concern");
  7. const command_1 = require("./command");
  8. const operation_1 = require("./operation");
  9. /** @internal */
  10. exports.DB_AGGREGATE_COLLECTION = 1;
  11. /** @internal */
  12. class AggregateOperation extends command_1.CommandOperation {
  13. constructor(ns, pipeline, options) {
  14. super(undefined, { ...options, dbName: ns.db });
  15. this.SERVER_COMMAND_RESPONSE_TYPE = responses_1.CursorResponse;
  16. this.options = { ...options };
  17. // Covers when ns.collection is null, undefined or the empty string, use DB_AGGREGATE_COLLECTION
  18. this.target = ns.collection || exports.DB_AGGREGATE_COLLECTION;
  19. this.pipeline = pipeline;
  20. // determine if we have a write stage, override read preference if so
  21. this.hasWriteStage = false;
  22. if (typeof options?.out === 'string') {
  23. this.pipeline = this.pipeline.concat({ $out: options.out });
  24. this.hasWriteStage = true;
  25. }
  26. else if (pipeline.length > 0) {
  27. const finalStage = pipeline[pipeline.length - 1];
  28. if (finalStage.$out || finalStage.$merge) {
  29. this.hasWriteStage = true;
  30. }
  31. }
  32. if (!this.hasWriteStage) {
  33. delete this.options.writeConcern;
  34. }
  35. if (options?.cursor != null && typeof options.cursor !== 'object') {
  36. throw new error_1.MongoInvalidArgumentError('Cursor options must be an object');
  37. }
  38. this.SERVER_COMMAND_RESPONSE_TYPE = this.explain ? responses_1.ExplainedCursorResponse : responses_1.CursorResponse;
  39. }
  40. get commandName() {
  41. return 'aggregate';
  42. }
  43. get canRetryRead() {
  44. return !this.hasWriteStage;
  45. }
  46. addToPipeline(stage) {
  47. this.pipeline.push(stage);
  48. }
  49. buildCommandDocument() {
  50. const options = this.options;
  51. const command = { aggregate: this.target, pipeline: this.pipeline };
  52. if (this.hasWriteStage && this.writeConcern) {
  53. write_concern_1.WriteConcern.apply(command, this.writeConcern);
  54. }
  55. if (options.bypassDocumentValidation === true) {
  56. command.bypassDocumentValidation = options.bypassDocumentValidation;
  57. }
  58. if (typeof options.allowDiskUse === 'boolean') {
  59. command.allowDiskUse = options.allowDiskUse;
  60. }
  61. if (options.hint) {
  62. command.hint = options.hint;
  63. }
  64. if (options.let) {
  65. command.let = options.let;
  66. }
  67. // we check for undefined specifically here to allow falsy values
  68. // eslint-disable-next-line no-restricted-syntax
  69. if (options.comment !== undefined) {
  70. command.comment = options.comment;
  71. }
  72. command.cursor = options.cursor || {};
  73. if (options.batchSize && !this.hasWriteStage) {
  74. command.cursor.batchSize = options.batchSize;
  75. }
  76. return command;
  77. }
  78. handleOk(response) {
  79. return response;
  80. }
  81. }
  82. exports.AggregateOperation = AggregateOperation;
  83. (0, operation_1.defineAspects)(AggregateOperation, [
  84. operation_1.Aspect.READ_OPERATION,
  85. operation_1.Aspect.RETRYABLE,
  86. operation_1.Aspect.EXPLAINABLE,
  87. operation_1.Aspect.CURSOR_CREATING,
  88. operation_1.Aspect.SUPPORTS_RAW_DATA
  89. ]);
  90. //# sourceMappingURL=aggregate.js.map