command.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 resolveReadPreference = require('../utils').resolveReadPreference;
  10. const MongoDBNamespace = require('../utils').MongoDBNamespace;
  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. 'bufferMaxEntries',
  24. 'numberOfRetries',
  25. 'retryMiliSeconds',
  26. 'readPreference',
  27. 'pkFactory',
  28. 'parentDb',
  29. 'promiseLibrary',
  30. 'noListener'
  31. ];
  32. class CommandOperation extends OperationBase {
  33. constructor(db, options, collection, command) {
  34. super(options);
  35. if (!this.hasAspect(Aspect.WRITE_OPERATION)) {
  36. if (collection != null) {
  37. this.options.readPreference = resolveReadPreference(collection, options);
  38. } else {
  39. this.options.readPreference = resolveReadPreference(db, options);
  40. }
  41. } else {
  42. if (collection != null) {
  43. applyWriteConcern(this.options, { db, coll: collection }, this.options);
  44. } else {
  45. applyWriteConcern(this.options, { db }, this.options);
  46. }
  47. this.options.readPreference = ReadPreference.primary;
  48. }
  49. this.db = db;
  50. if (command != null) {
  51. this.command = command;
  52. }
  53. if (collection != null) {
  54. this.collection = collection;
  55. }
  56. }
  57. _buildCommand() {
  58. if (this.command != null) {
  59. return this.command;
  60. }
  61. }
  62. execute(callback) {
  63. const db = this.db;
  64. const options = Object.assign({}, this.options);
  65. // Did the user destroy the topology
  66. if (db.serverConfig && db.serverConfig.isDestroyed()) {
  67. return callback(new MongoError('topology was destroyed'));
  68. }
  69. let command;
  70. try {
  71. command = this._buildCommand();
  72. } catch (e) {
  73. return callback(e);
  74. }
  75. // Get the db name we are executing against
  76. const dbName = options.dbName || options.authdb || db.databaseName;
  77. // Convert the readPreference if its not a write
  78. if (this.hasAspect(Aspect.WRITE_OPERATION)) {
  79. if (options.writeConcern && (!options.session || !options.session.inTransaction())) {
  80. command.writeConcern = options.writeConcern;
  81. }
  82. }
  83. // Debug information
  84. if (db.s.logger.isDebug()) {
  85. db.s.logger.debug(
  86. `executing command ${JSON.stringify(
  87. command
  88. )} against ${dbName}.$cmd with options [${JSON.stringify(
  89. debugOptions(debugFields, options)
  90. )}]`
  91. );
  92. }
  93. const namespace =
  94. this.namespace != null ? this.namespace : new MongoDBNamespace(dbName, '$cmd');
  95. // Execute command
  96. db.s.topology.command(namespace, command, options, (err, result) => {
  97. if (err) return handleCallback(callback, err);
  98. if (options.full) return handleCallback(callback, null, result);
  99. handleCallback(callback, null, result.result);
  100. });
  101. }
  102. }
  103. module.exports = CommandOperation;