command_v2.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. 'use strict';
  2. const Aspect = require('./operation').Aspect;
  3. const OperationBase = require('./operation').OperationBase;
  4. const ReadPreference = require('../core').ReadPreference;
  5. const ReadConcern = require('../read_concern');
  6. const WriteConcern = require('../write_concern');
  7. const maxWireVersion = require('../core/utils').maxWireVersion;
  8. const decorateWithExplain = require('../utils').decorateWithExplain;
  9. const commandSupportsReadConcern = require('../core/sessions').commandSupportsReadConcern;
  10. const MongoError = require('../core/error').MongoError;
  11. const SUPPORTS_WRITE_CONCERN_AND_COLLATION = 5;
  12. class CommandOperationV2 extends OperationBase {
  13. constructor(parent, options, operationOptions) {
  14. super(options);
  15. this.ns = parent.s.namespace.withCollection('$cmd');
  16. const propertyProvider = this.hasAspect(Aspect.NO_INHERIT_OPTIONS) ? undefined : parent;
  17. this.readPreference = this.hasAspect(Aspect.WRITE_OPERATION)
  18. ? ReadPreference.primary
  19. : ReadPreference.resolve(propertyProvider, this.options);
  20. this.readConcern = resolveReadConcern(propertyProvider, this.options);
  21. this.writeConcern = resolveWriteConcern(propertyProvider, this.options);
  22. if (operationOptions && typeof operationOptions.fullResponse === 'boolean') {
  23. this.fullResponse = true;
  24. }
  25. // TODO: A lot of our code depends on having the read preference in the options. This should
  26. // go away, but also requires massive test rewrites.
  27. this.options.readPreference = this.readPreference;
  28. // TODO(NODE-2056): make logger another "inheritable" property
  29. if (parent.s.logger) {
  30. this.logger = parent.s.logger;
  31. } else if (parent.s.db && parent.s.db.logger) {
  32. this.logger = parent.s.db.logger;
  33. }
  34. }
  35. executeCommand(server, cmd, callback) {
  36. // TODO: consider making this a non-enumerable property
  37. this.server = server;
  38. const options = this.options;
  39. const serverWireVersion = maxWireVersion(server);
  40. const inTransaction = this.session && this.session.inTransaction();
  41. if (this.readConcern && commandSupportsReadConcern(cmd) && !inTransaction) {
  42. Object.assign(cmd, { readConcern: this.readConcern });
  43. }
  44. if (options.collation && serverWireVersion < SUPPORTS_WRITE_CONCERN_AND_COLLATION) {
  45. callback(
  46. new MongoError(
  47. `Server ${server.name}, which reports wire version ${serverWireVersion}, does not support collation`
  48. )
  49. );
  50. return;
  51. }
  52. if (serverWireVersion >= SUPPORTS_WRITE_CONCERN_AND_COLLATION) {
  53. if (this.writeConcern && this.hasAspect(Aspect.WRITE_OPERATION)) {
  54. Object.assign(cmd, { writeConcern: this.writeConcern });
  55. }
  56. if (options.collation && typeof options.collation === 'object') {
  57. Object.assign(cmd, { collation: options.collation });
  58. }
  59. }
  60. if (typeof options.maxTimeMS === 'number') {
  61. cmd.maxTimeMS = options.maxTimeMS;
  62. }
  63. if (typeof options.comment === 'string') {
  64. cmd.comment = options.comment;
  65. }
  66. if (this.hasAspect(Aspect.EXPLAINABLE) && this.explain) {
  67. if (serverWireVersion < 6 && cmd.aggregate) {
  68. // Prior to 3.6, with aggregate, verbosity is ignored, and we must pass in "explain: true"
  69. cmd.explain = true;
  70. } else {
  71. cmd = decorateWithExplain(cmd, this.explain);
  72. }
  73. }
  74. if (this.logger && this.logger.isDebug()) {
  75. this.logger.debug(`executing command ${JSON.stringify(cmd)} against ${this.ns}`);
  76. }
  77. server.command(this.ns.toString(), cmd, this.options, (err, result) => {
  78. if (err) {
  79. callback(err, null);
  80. return;
  81. }
  82. if (this.fullResponse) {
  83. callback(null, result);
  84. return;
  85. }
  86. callback(null, result.result);
  87. });
  88. }
  89. }
  90. function resolveWriteConcern(parent, options) {
  91. return WriteConcern.fromOptions(options) || (parent && parent.writeConcern);
  92. }
  93. function resolveReadConcern(parent, options) {
  94. return ReadConcern.fromOptions(options) || (parent && parent.readConcern);
  95. }
  96. module.exports = CommandOperationV2;