aggregate.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 (this.explain && this.writeConcern) {
  36. throw new error_1.MongoInvalidArgumentError('Option "explain" cannot be used on an aggregate call with writeConcern');
  37. }
  38. if (options?.cursor != null && typeof options.cursor !== 'object') {
  39. throw new error_1.MongoInvalidArgumentError('Cursor options must be an object');
  40. }
  41. this.SERVER_COMMAND_RESPONSE_TYPE = this.explain ? responses_1.ExplainedCursorResponse : responses_1.CursorResponse;
  42. }
  43. get commandName() {
  44. return 'aggregate';
  45. }
  46. get canRetryRead() {
  47. return !this.hasWriteStage;
  48. }
  49. addToPipeline(stage) {
  50. this.pipeline.push(stage);
  51. }
  52. buildCommandDocument() {
  53. const options = this.options;
  54. const command = { aggregate: this.target, pipeline: this.pipeline };
  55. if (this.hasWriteStage && this.writeConcern) {
  56. write_concern_1.WriteConcern.apply(command, this.writeConcern);
  57. }
  58. if (options.bypassDocumentValidation === true) {
  59. command.bypassDocumentValidation = options.bypassDocumentValidation;
  60. }
  61. if (typeof options.allowDiskUse === 'boolean') {
  62. command.allowDiskUse = options.allowDiskUse;
  63. }
  64. if (options.hint) {
  65. command.hint = options.hint;
  66. }
  67. if (options.let) {
  68. command.let = options.let;
  69. }
  70. // we check for undefined specifically here to allow falsy values
  71. // eslint-disable-next-line no-restricted-syntax
  72. if (options.comment !== undefined) {
  73. command.comment = options.comment;
  74. }
  75. command.cursor = options.cursor || {};
  76. if (options.batchSize && !this.hasWriteStage) {
  77. command.cursor.batchSize = options.batchSize;
  78. }
  79. return command;
  80. }
  81. handleOk(response) {
  82. return response;
  83. }
  84. }
  85. exports.AggregateOperation = AggregateOperation;
  86. (0, operation_1.defineAspects)(AggregateOperation, [
  87. operation_1.Aspect.READ_OPERATION,
  88. operation_1.Aspect.RETRYABLE,
  89. operation_1.Aspect.EXPLAINABLE,
  90. operation_1.Aspect.CURSOR_CREATING,
  91. operation_1.Aspect.SUPPORTS_RAW_DATA
  92. ]);
  93. //# sourceMappingURL=aggregate.js.map