explain.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. 'use strict';
  2. const MongoError = require('./core/error').MongoError;
  3. /**
  4. * @class
  5. * @property {string} verbosity The verbosity mode for the explain output, e.g.: 'queryPlanner', 'queryPlannerExtended', 'executionStats', 'allPlansExecution'.
  6. */
  7. class Explain {
  8. /**
  9. * Constructs an Explain from the explain verbosity.
  10. *
  11. * For backwards compatibility, true is interpreted as "allPlansExecution"
  12. * and false as "queryPlanner". Prior to server version 3.6, aggregate()
  13. * ignores the verbosity parameter and executes in "queryPlanner".
  14. *
  15. * @param {string|boolean} [verbosity] The verbosity mode for the explain output.
  16. */
  17. constructor(verbosity) {
  18. if (typeof verbosity === 'boolean') {
  19. this.verbosity = verbosity ? 'allPlansExecution' : 'queryPlanner';
  20. } else {
  21. this.verbosity = verbosity;
  22. }
  23. }
  24. /**
  25. * Construct an Explain given an options object.
  26. *
  27. * @param {object} [options] The options object from which to extract the explain.
  28. * @param {string|boolean} [options.explain] The verbosity mode for the explain output.
  29. * @return {Explain}
  30. */
  31. static fromOptions(options) {
  32. if (options == null || options.explain === undefined) {
  33. return;
  34. }
  35. const explain = options.explain;
  36. if (typeof explain === 'boolean' || typeof explain === 'string') {
  37. return new Explain(options.explain);
  38. }
  39. throw new MongoError(`explain must be a string or a boolean`);
  40. }
  41. }
  42. module.exports = { Explain };