Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | 247x 247x 247x 247x 5383x 1531x 3852x 5383x 1028923x 5383x 3647x 1736x 1736x 247x 6925x 6925x 144x 247x 4295x 4295x 4295x 576x 4295x 247x 1279x 379x 379x 900x 752x 752x 148x 148x 1279x | import { type Document } from './bson'; import { AbstractCursor } from './cursor/abstract_cursor'; import { MongoAPIError } from './error'; /** @public */ export const ExplainVerbosity = Object.freeze({ queryPlanner: 'queryPlanner', queryPlannerExtended: 'queryPlannerExtended', executionStats: 'executionStats', allPlansExecution: 'allPlansExecution' } as const); /** @public */ export type ExplainVerbosity = string; /** * For backwards compatibility, true is interpreted as "allPlansExecution" * and false as "queryPlanner". * @public */ export type ExplainVerbosityLike = ExplainVerbosity | boolean; /** @public */ export interface ExplainCommandOptions { /** The explain verbosity for the command. */ verbosity: ExplainVerbosity; /** The maxTimeMS setting for the command. */ maxTimeMS?: number; } /** * @public * * When set, this configures an explain command. Valid values are boolean (for legacy compatibility, * see {@link ExplainVerbosityLike}), a string containing the explain verbosity, or an object containing the verbosity and * an optional maxTimeMS. * * Examples of valid usage: * * ```typescript * collection.find({ name: 'john doe' }, { explain: true }); * collection.find({ name: 'john doe' }, { explain: false }); * collection.find({ name: 'john doe' }, { explain: 'queryPlanner' }); * collection.find({ name: 'john doe' }, { explain: { verbosity: 'queryPlanner' } }); * ``` * * maxTimeMS can be configured to limit the amount of time the server * spends executing an explain by providing an object: * * ```typescript * // limits the `explain` command to no more than 2 seconds * collection.find({ name: 'john doe' }, { * explain: { * verbosity: 'queryPlanner', * maxTimeMS: 2000 * } * }); * ``` */ export interface ExplainOptions { /** Specifies the verbosity mode for the explain output. */ explain?: ExplainVerbosityLike | ExplainCommandOptions; } /** @internal */ export class Explain { readonly verbosity: ExplainVerbosity; readonly maxTimeMS?: number; private constructor(verbosity: ExplainVerbosityLike, maxTimeMS?: number) { if (typeof verbosity === 'boolean') { this.verbosity = verbosity ? ExplainVerbosity.allPlansExecution : ExplainVerbosity.queryPlanner; } else { this.verbosity = verbosity; } this.maxTimeMS = maxTimeMS; } static fromOptions({ explain }: ExplainOptions = {}): Explain | undefined { if (explain == null) return; if (typeof explain === 'boolean' || typeof explain === 'string') { return new Explain(explain); } const { verbosity, maxTimeMS } = explain; return new Explain(verbosity, maxTimeMS); } } export function validateExplainTimeoutOptions(options: Document, explain?: Explain) { const { maxTimeMS, timeoutMS } = options; if (timeoutMS != null && (maxTimeMS != null || explain?.maxTimeMS != null)) { throw new MongoAPIError('Cannot use maxTimeMS with timeoutMS for explain commands.'); } } /** * Applies an explain to a given command. * @internal * * @param command - the command on which to apply the explain * @param options - the options containing the explain verbosity */ export function decorateWithExplain( command: Document, explain: Explain ): { explain: Document; verbosity: ExplainVerbosity; maxTimeMS?: number; } { type ExplainCommand = ReturnType<typeof decorateWithExplain>; const { verbosity, maxTimeMS } = explain; const baseCommand: ExplainCommand = { explain: command, verbosity }; if (typeof maxTimeMS === 'number') { baseCommand.maxTimeMS = maxTimeMS; } return baseCommand; } /** * @public * * A base class for any cursors that have `explain()` methods. */ export abstract class ExplainableCursor<TSchema> extends AbstractCursor<TSchema> { /** Execute the explain for the cursor */ abstract explain(): Promise<Document>; abstract explain(verbosity: ExplainVerbosityLike | ExplainCommandOptions): Promise<Document>; abstract explain(options: { timeoutMS?: number }): Promise<Document>; abstract explain( verbosity: ExplainVerbosityLike | ExplainCommandOptions, options: { timeoutMS?: number } ): Promise<Document>; abstract explain( verbosity?: ExplainVerbosityLike | ExplainCommandOptions | { timeoutMS?: number }, options?: { timeoutMS?: number } ): Promise<Document>; protected resolveExplainTimeoutOptions( verbosity?: ExplainVerbosityLike | ExplainCommandOptions | { timeoutMS?: number }, options?: { timeoutMS?: number } ): { timeout?: { timeoutMS?: number }; explain?: ExplainVerbosityLike | ExplainCommandOptions } { let explain: ExplainVerbosityLike | ExplainCommandOptions | undefined; let timeout: { timeoutMS?: number } | undefined; if (verbosity == null && options == null) { explain = undefined; timeout = undefined; } else if (verbosity != null && options == null) { explain = typeof verbosity !== 'object' ? verbosity : 'verbosity' in verbosity ? verbosity : undefined; timeout = typeof verbosity === 'object' && 'timeoutMS' in verbosity ? verbosity : undefined; } else { // @ts-expect-error TS isn't smart enough to determine that if both options are provided, the first is explain options explain = verbosity; timeout = options; } return { timeout, explain }; } } |