aggregate.js 3.8 KB

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