explain.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { MongoInvalidArgumentError } from './error';
  2. /** @public */
  3. export const ExplainVerbosity = Object.freeze({
  4. queryPlanner: 'queryPlanner',
  5. queryPlannerExtended: 'queryPlannerExtended',
  6. executionStats: 'executionStats',
  7. allPlansExecution: 'allPlansExecution'
  8. } as const);
  9. /** @public */
  10. export type ExplainVerbosity = string;
  11. /**
  12. * For backwards compatibility, true is interpreted as "allPlansExecution"
  13. * and false as "queryPlanner". Prior to server version 3.6, aggregate()
  14. * ignores the verbosity parameter and executes in "queryPlanner".
  15. * @public
  16. */
  17. export type ExplainVerbosityLike = ExplainVerbosity | boolean;
  18. /** @public */
  19. export interface ExplainOptions {
  20. /** Specifies the verbosity mode for the explain output. */
  21. explain?: ExplainVerbosityLike;
  22. }
  23. /** @internal */
  24. export class Explain {
  25. verbosity: ExplainVerbosity;
  26. constructor(verbosity: ExplainVerbosityLike) {
  27. if (typeof verbosity === 'boolean') {
  28. this.verbosity = verbosity
  29. ? ExplainVerbosity.allPlansExecution
  30. : ExplainVerbosity.queryPlanner;
  31. } else {
  32. this.verbosity = verbosity;
  33. }
  34. }
  35. static fromOptions(options?: ExplainOptions): Explain | undefined {
  36. if (options?.explain == null) return;
  37. const explain = options.explain;
  38. if (typeof explain === 'boolean' || typeof explain === 'string') {
  39. return new Explain(explain);
  40. }
  41. throw new MongoInvalidArgumentError('Field "explain" must be a string or a boolean');
  42. }
  43. }