command.js 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.CommandOperation = void 0;
  4. const error_1 = require("../error");
  5. const explain_1 = require("../explain");
  6. const read_concern_1 = require("../read_concern");
  7. const server_selection_1 = require("../sdam/server_selection");
  8. const utils_1 = require("../utils");
  9. const write_concern_1 = require("../write_concern");
  10. const operation_1 = require("./operation");
  11. const SUPPORTS_WRITE_CONCERN_AND_COLLATION = 5;
  12. /** @internal */
  13. class CommandOperation extends operation_1.AbstractOperation {
  14. constructor(parent, options) {
  15. super(options);
  16. this.options = options !== null && options !== void 0 ? options : {};
  17. // NOTE: this was explicitly added for the add/remove user operations, it's likely
  18. // something we'd want to reconsider. Perhaps those commands can use `Admin`
  19. // as a parent?
  20. const dbNameOverride = (options === null || options === void 0 ? void 0 : options.dbName) || (options === null || options === void 0 ? void 0 : options.authdb);
  21. if (dbNameOverride) {
  22. this.ns = new utils_1.MongoDBNamespace(dbNameOverride, '$cmd');
  23. }
  24. else {
  25. this.ns = parent
  26. ? parent.s.namespace.withCollection('$cmd')
  27. : new utils_1.MongoDBNamespace('admin', '$cmd');
  28. }
  29. this.readConcern = read_concern_1.ReadConcern.fromOptions(options);
  30. this.writeConcern = write_concern_1.WriteConcern.fromOptions(options);
  31. // TODO(NODE-2056): make logger another "inheritable" property
  32. if (parent && parent.logger) {
  33. this.logger = parent.logger;
  34. }
  35. if (this.hasAspect(operation_1.Aspect.EXPLAINABLE)) {
  36. this.explain = explain_1.Explain.fromOptions(options);
  37. }
  38. else if ((options === null || options === void 0 ? void 0 : options.explain) != null) {
  39. throw new error_1.MongoInvalidArgumentError(`Option "explain" is not supported on this command`);
  40. }
  41. }
  42. get canRetryWrite() {
  43. if (this.hasAspect(operation_1.Aspect.EXPLAINABLE)) {
  44. return this.explain == null;
  45. }
  46. return true;
  47. }
  48. executeCommand(server, session, cmd, callback) {
  49. // TODO: consider making this a non-enumerable property
  50. this.server = server;
  51. const options = {
  52. ...this.options,
  53. ...this.bsonOptions,
  54. readPreference: this.readPreference,
  55. session
  56. };
  57. const serverWireVersion = (0, utils_1.maxWireVersion)(server);
  58. const inTransaction = this.session && this.session.inTransaction();
  59. if (this.readConcern && (0, utils_1.commandSupportsReadConcern)(cmd) && !inTransaction) {
  60. Object.assign(cmd, { readConcern: this.readConcern });
  61. }
  62. if (this.trySecondaryWrite && serverWireVersion < server_selection_1.MIN_SECONDARY_WRITE_WIRE_VERSION) {
  63. options.omitReadPreference = true;
  64. }
  65. if (options.collation && serverWireVersion < SUPPORTS_WRITE_CONCERN_AND_COLLATION) {
  66. callback(new error_1.MongoCompatibilityError(`Server ${server.name}, which reports wire version ${serverWireVersion}, does not support collation`));
  67. return;
  68. }
  69. if (this.writeConcern && this.hasAspect(operation_1.Aspect.WRITE_OPERATION) && !inTransaction) {
  70. Object.assign(cmd, { writeConcern: this.writeConcern });
  71. }
  72. if (serverWireVersion >= SUPPORTS_WRITE_CONCERN_AND_COLLATION) {
  73. if (options.collation &&
  74. typeof options.collation === 'object' &&
  75. !this.hasAspect(operation_1.Aspect.SKIP_COLLATION)) {
  76. Object.assign(cmd, { collation: options.collation });
  77. }
  78. }
  79. if (typeof options.maxTimeMS === 'number') {
  80. cmd.maxTimeMS = options.maxTimeMS;
  81. }
  82. if (this.hasAspect(operation_1.Aspect.EXPLAINABLE) && this.explain) {
  83. if (serverWireVersion < 6 && cmd.aggregate) {
  84. // Prior to 3.6, with aggregate, verbosity is ignored, and we must pass in "explain: true"
  85. cmd.explain = true;
  86. }
  87. else {
  88. cmd = (0, utils_1.decorateWithExplain)(cmd, this.explain);
  89. }
  90. }
  91. server.command(this.ns, cmd, options, callback);
  92. }
  93. }
  94. exports.CommandOperation = CommandOperation;
  95. //# sourceMappingURL=command.js.map