command.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.CommandOperation = void 0;
  4. const constants_1 = require("../cmap/wire_protocol/constants");
  5. const error_1 = require("../error");
  6. const explain_1 = require("../explain");
  7. const read_concern_1 = require("../read_concern");
  8. const utils_1 = require("../utils");
  9. const write_concern_1 = require("../write_concern");
  10. const operation_1 = require("./operation");
  11. /** @internal */
  12. class CommandOperation extends operation_1.AbstractOperation {
  13. constructor(parent, options) {
  14. super(options);
  15. this.options = options ?? {};
  16. // NOTE: this was explicitly added for the add/remove user operations, it's likely
  17. // something we'd want to reconsider. Perhaps those commands can use `Admin`
  18. // as a parent?
  19. const dbNameOverride = options?.dbName || options?.authdb;
  20. if (dbNameOverride) {
  21. this.ns = new utils_1.MongoDBNamespace(dbNameOverride, '$cmd');
  22. }
  23. else {
  24. this.ns = parent
  25. ? parent.s.namespace.withCollection('$cmd')
  26. : new utils_1.MongoDBNamespace('admin', '$cmd');
  27. }
  28. this.readConcern = read_concern_1.ReadConcern.fromOptions(options);
  29. this.writeConcern = write_concern_1.WriteConcern.fromOptions(options);
  30. if (this.hasAspect(operation_1.Aspect.EXPLAINABLE)) {
  31. this.explain = explain_1.Explain.fromOptions(options);
  32. if (this.explain)
  33. (0, explain_1.validateExplainTimeoutOptions)(this.options, this.explain);
  34. }
  35. else if (options?.explain != null) {
  36. throw new error_1.MongoInvalidArgumentError(`Option "explain" is not supported on this command`);
  37. }
  38. }
  39. get canRetryWrite() {
  40. if (this.hasAspect(operation_1.Aspect.EXPLAINABLE)) {
  41. return this.explain == null;
  42. }
  43. return super.canRetryWrite;
  44. }
  45. buildOptions(timeoutContext) {
  46. return {
  47. ...this.options,
  48. ...this.bsonOptions,
  49. timeoutContext,
  50. readPreference: this.readPreference,
  51. session: this.session
  52. };
  53. }
  54. buildCommand(connection, session) {
  55. const command = this.buildCommandDocument(connection, session);
  56. const inTransaction = this.session && this.session.inTransaction();
  57. if (this.readConcern && (0, utils_1.commandSupportsReadConcern)(command) && !inTransaction) {
  58. Object.assign(command, { readConcern: this.readConcern });
  59. }
  60. if (this.writeConcern && this.hasAspect(operation_1.Aspect.WRITE_OPERATION) && !inTransaction) {
  61. write_concern_1.WriteConcern.apply(command, this.writeConcern);
  62. }
  63. if (this.options.collation &&
  64. typeof this.options.collation === 'object' &&
  65. !this.hasAspect(operation_1.Aspect.SKIP_COLLATION)) {
  66. Object.assign(command, { collation: this.options.collation });
  67. }
  68. if (typeof this.options.maxTimeMS === 'number') {
  69. command.maxTimeMS = this.options.maxTimeMS;
  70. }
  71. if (this.options.rawData != null &&
  72. this.hasAspect(operation_1.Aspect.SUPPORTS_RAW_DATA) &&
  73. (0, utils_1.maxWireVersion)(connection) >= constants_1.MIN_SUPPORTED_RAW_DATA_WIRE_VERSION) {
  74. command.rawData = this.options.rawData;
  75. }
  76. if (this.hasAspect(operation_1.Aspect.EXPLAINABLE) && this.explain) {
  77. return (0, explain_1.decorateWithExplain)(command, this.explain);
  78. }
  79. return command;
  80. }
  81. }
  82. exports.CommandOperation = CommandOperation;
  83. //# sourceMappingURL=command.js.map