explain.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Explain = exports.ExplainVerbosity = void 0;
  4. exports.validateExplainTimeoutOptions = validateExplainTimeoutOptions;
  5. exports.decorateWithExplain = decorateWithExplain;
  6. const error_1 = require("./error");
  7. /** @public */
  8. exports.ExplainVerbosity = Object.freeze({
  9. queryPlanner: 'queryPlanner',
  10. queryPlannerExtended: 'queryPlannerExtended',
  11. executionStats: 'executionStats',
  12. allPlansExecution: 'allPlansExecution'
  13. });
  14. /** @internal */
  15. class Explain {
  16. constructor(verbosity, maxTimeMS) {
  17. if (typeof verbosity === 'boolean') {
  18. this.verbosity = verbosity
  19. ? exports.ExplainVerbosity.allPlansExecution
  20. : exports.ExplainVerbosity.queryPlanner;
  21. }
  22. else {
  23. this.verbosity = verbosity;
  24. }
  25. this.maxTimeMS = maxTimeMS;
  26. }
  27. static fromOptions({ explain } = {}) {
  28. if (explain == null)
  29. return;
  30. if (typeof explain === 'boolean' || typeof explain === 'string') {
  31. return new Explain(explain);
  32. }
  33. const { verbosity, maxTimeMS } = explain;
  34. return new Explain(verbosity, maxTimeMS);
  35. }
  36. }
  37. exports.Explain = Explain;
  38. function validateExplainTimeoutOptions(options, explain) {
  39. const { maxTimeMS, timeoutMS } = options;
  40. if (timeoutMS != null && (maxTimeMS != null || explain?.maxTimeMS != null)) {
  41. throw new error_1.MongoAPIError('Cannot use maxTimeMS with timeoutMS for explain commands.');
  42. }
  43. }
  44. /**
  45. * Applies an explain to a given command.
  46. * @internal
  47. *
  48. * @param command - the command on which to apply the explain
  49. * @param options - the options containing the explain verbosity
  50. */
  51. function decorateWithExplain(command, explain) {
  52. const { verbosity, maxTimeMS } = explain;
  53. const baseCommand = { explain: command, verbosity };
  54. if (typeof maxTimeMS === 'number') {
  55. baseCommand.maxTimeMS = maxTimeMS;
  56. }
  57. return baseCommand;
  58. }
  59. //# sourceMappingURL=explain.js.map