command.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. 'use strict';
  2. const Aspect = require('./operation').Aspect;
  3. const OperationBase = require('./operation').OperationBase;
  4. const applyWriteConcern = require('../utils').applyWriteConcern;
  5. const debugOptions = require('../utils').debugOptions;
  6. const handleCallback = require('../utils').handleCallback;
  7. const MongoError = require('../core').MongoError;
  8. const ReadPreference = require('../core').ReadPreference;
  9. const MongoDBNamespace = require('../utils').MongoDBNamespace;
  10. const extractCommand = require('../command_utils').extractCommand;
  11. const debugFields = [
  12. 'authSource',
  13. 'w',
  14. 'wtimeout',
  15. 'j',
  16. 'native_parser',
  17. 'forceServerObjectId',
  18. 'serializeFunctions',
  19. 'raw',
  20. 'promoteLongs',
  21. 'promoteValues',
  22. 'promoteBuffers',
  23. 'bsonRegExp',
  24. 'bufferMaxEntries',
  25. 'numberOfRetries',
  26. 'retryMiliSeconds',
  27. 'readPreference',
  28. 'pkFactory',
  29. 'parentDb',
  30. 'promiseLibrary',
  31. 'noListener'
  32. ];
  33. class CommandOperation extends OperationBase {
  34. constructor(db, options, collection, command) {
  35. super(options);
  36. if (!this.hasAspect(Aspect.WRITE_OPERATION)) {
  37. if (collection != null) {
  38. this.options.readPreference = ReadPreference.resolve(collection, options);
  39. } else {
  40. this.options.readPreference = ReadPreference.resolve(db, options);
  41. }
  42. } else {
  43. if (collection != null) {
  44. applyWriteConcern(this.options, { db, coll: collection }, this.options);
  45. } else {
  46. applyWriteConcern(this.options, { db }, this.options);
  47. }
  48. this.options.readPreference = ReadPreference.primary;
  49. }
  50. this.db = db;
  51. if (command != null) {
  52. this.command = command;
  53. }
  54. if (collection != null) {
  55. this.collection = collection;
  56. }
  57. }
  58. _buildCommand() {
  59. if (this.command != null) {
  60. return this.command;
  61. }
  62. }
  63. execute(callback) {
  64. const db = this.db;
  65. const options = Object.assign({}, this.options);
  66. // Did the user destroy the topology
  67. if (db.serverConfig && db.serverConfig.isDestroyed()) {
  68. return callback(new MongoError('topology was destroyed'));
  69. }
  70. let command;
  71. try {
  72. command = this._buildCommand();
  73. } catch (e) {
  74. return callback(e);
  75. }
  76. // Get the db name we are executing against
  77. const dbName = options.dbName || options.authdb || db.databaseName;
  78. // Convert the readPreference if its not a write
  79. if (this.hasAspect(Aspect.WRITE_OPERATION)) {
  80. if (options.writeConcern && (!options.session || !options.session.inTransaction())) {
  81. command.writeConcern = options.writeConcern;
  82. }
  83. }
  84. // Debug information
  85. if (db.s.logger.isDebug()) {
  86. const extractedCommand = extractCommand(command);
  87. db.s.logger.debug(
  88. `executing command ${JSON.stringify(
  89. extractedCommand.shouldRedact ? `${extractedCommand.name} details REDACTED` : command
  90. )} against ${dbName}.$cmd with options [${JSON.stringify(
  91. debugOptions(debugFields, options)
  92. )}]`
  93. );
  94. }
  95. const namespace =
  96. this.namespace != null ? this.namespace : new MongoDBNamespace(dbName, '$cmd');
  97. // Execute command
  98. db.s.topology.command(namespace, command, options, (err, result) => {
  99. if (err) return handleCallback(callback, err);
  100. if (options.full) return handleCallback(callback, null, result);
  101. handleCallback(callback, null, result.result);
  102. });
  103. }
  104. }
  105. module.exports = CommandOperation;