admin_ops.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. 'use strict';
  2. const executeCommand = require('./db_ops').executeCommand;
  3. const executeDbAdminCommand = require('./db_ops').executeDbAdminCommand;
  4. /**
  5. * Get ReplicaSet status
  6. *
  7. * @param {Admin} a collection instance.
  8. * @param {Object} [options] Optional settings. See Admin.prototype.replSetGetStatus for a list of options.
  9. * @param {Admin~resultCallback} [callback] The command result callback.
  10. */
  11. function replSetGetStatus(admin, options, callback) {
  12. executeDbAdminCommand(admin.s.db, { replSetGetStatus: 1 }, options, callback);
  13. }
  14. /**
  15. * Retrieve this db's server status.
  16. *
  17. * @param {Admin} a collection instance.
  18. * @param {Object} [options] Optional settings. See Admin.prototype.serverStatus for a list of options.
  19. * @param {Admin~resultCallback} [callback] The command result callback
  20. */
  21. function serverStatus(admin, options, callback) {
  22. executeDbAdminCommand(admin.s.db, { serverStatus: 1 }, options, callback);
  23. }
  24. /**
  25. * Validate an existing collection
  26. *
  27. * @param {Admin} a collection instance.
  28. * @param {string} collectionName The name of the collection to validate.
  29. * @param {Object} [options] Optional settings. See Admin.prototype.validateCollection for a list of options.
  30. * @param {Admin~resultCallback} [callback] The command result callback.
  31. */
  32. function validateCollection(admin, collectionName, options, callback) {
  33. const command = { validate: collectionName };
  34. const keys = Object.keys(options);
  35. // Decorate command with extra options
  36. for (let i = 0; i < keys.length; i++) {
  37. if (options.hasOwnProperty(keys[i]) && keys[i] !== 'session') {
  38. command[keys[i]] = options[keys[i]];
  39. }
  40. }
  41. executeCommand(admin.s.db, command, options, (err, doc) => {
  42. if (err != null) return callback(err, null);
  43. if (doc.ok === 0) return callback(new Error('Error with validate command'), null);
  44. if (doc.result != null && doc.result.constructor !== String)
  45. return callback(new Error('Error with validation data'), null);
  46. if (doc.result != null && doc.result.match(/exception|corrupt/) != null)
  47. return callback(new Error('Error: invalid collection ' + collectionName), null);
  48. if (doc.valid != null && !doc.valid)
  49. return callback(new Error('Error: invalid collection ' + collectionName), null);
  50. return callback(null, doc);
  51. });
  52. }
  53. module.exports = { replSetGetStatus, serverStatus, validateCollection };