command.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. /** @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. }
  33. else if (options?.explain != null) {
  34. throw new error_1.MongoInvalidArgumentError(`Option "explain" is not supported on this command`);
  35. }
  36. }
  37. get canRetryWrite() {
  38. if (this.hasAspect(operation_1.Aspect.EXPLAINABLE)) {
  39. return this.explain == null;
  40. }
  41. return true;
  42. }
  43. async executeCommand(server, session, cmd) {
  44. // TODO: consider making this a non-enumerable property
  45. this.server = server;
  46. const options = {
  47. ...this.options,
  48. ...this.bsonOptions,
  49. readPreference: this.readPreference,
  50. session
  51. };
  52. const serverWireVersion = (0, utils_1.maxWireVersion)(server);
  53. const inTransaction = this.session && this.session.inTransaction();
  54. if (this.readConcern && (0, utils_1.commandSupportsReadConcern)(cmd) && !inTransaction) {
  55. Object.assign(cmd, { readConcern: this.readConcern });
  56. }
  57. if (this.trySecondaryWrite && serverWireVersion < server_selection_1.MIN_SECONDARY_WRITE_WIRE_VERSION) {
  58. options.omitReadPreference = true;
  59. }
  60. if (this.writeConcern && this.hasAspect(operation_1.Aspect.WRITE_OPERATION) && !inTransaction) {
  61. write_concern_1.WriteConcern.apply(cmd, this.writeConcern);
  62. }
  63. if (options.collation &&
  64. typeof options.collation === 'object' &&
  65. !this.hasAspect(operation_1.Aspect.SKIP_COLLATION)) {
  66. Object.assign(cmd, { collation: options.collation });
  67. }
  68. if (typeof options.maxTimeMS === 'number') {
  69. cmd.maxTimeMS = options.maxTimeMS;
  70. }
  71. if (this.hasAspect(operation_1.Aspect.EXPLAINABLE) && this.explain) {
  72. cmd = (0, utils_1.decorateWithExplain)(cmd, this.explain);
  73. }
  74. return server.commandAsync(this.ns, cmd, options);
  75. }
  76. }
  77. exports.CommandOperation = CommandOperation;
  78. //# sourceMappingURL=command.js.map