@@ -19,6 +19,11 @@ export abstract class ExplainableCommand<
19
19
20
20
constructor ( parent ?: OperationParent , options ?: T ) {
21
21
super ( parent , options ) ;
22
+
23
+ if ( ! Explain . explainOptionsValid ( options ) ) {
24
+ throw new MongoError ( `explain must be one of ${ Object . keys ( Verbosity ) } or a boolean` ) ;
25
+ }
26
+
22
27
this . explain = Explain . fromOptions ( options ) ;
23
28
}
24
29
@@ -57,15 +62,15 @@ export type VerbosityLike = Verbosity | boolean;
57
62
58
63
/** @internal */
59
64
export class Explain {
60
- explain : Verbosity ;
65
+ verbosity : Verbosity ;
61
66
62
- constructor ( explain : VerbosityLike ) {
63
- if ( typeof explain === 'boolean' ) {
67
+ constructor ( verbosity : VerbosityLike ) {
68
+ if ( typeof verbosity === 'boolean' ) {
64
69
// For backwards compatibility, true is interpreted as
65
70
// "allPlansExecution" and false as "queryPlanner".
66
- this . explain = explain ? Verbosity . allPlansExecution : Verbosity . queryPlanner ;
71
+ this . verbosity = verbosity ? Verbosity . allPlansExecution : Verbosity . queryPlanner ;
67
72
} else {
68
- this . explain = Verbosity [ explain ] ;
73
+ this . verbosity = Verbosity [ verbosity ] ;
69
74
}
70
75
}
71
76
@@ -76,6 +81,14 @@ export class Explain {
76
81
return new Explain ( options . explain ) ;
77
82
}
78
83
84
+ static explainOptionsValid ( options ?: ExplainOptions ) : boolean {
85
+ if ( options == null || options . explain === undefined ) {
86
+ return true ;
87
+ }
88
+ const explain = options . explain ;
89
+ return typeof explain === 'boolean' || explain in Verbosity ;
90
+ }
91
+
79
92
/**
80
93
* Checks that the server supports explain on the given operation.
81
94
* @internal
@@ -113,15 +126,12 @@ export class Explain {
113
126
* @param command - the command on which to apply the read concern
114
127
* @param options - the options containing the explain verbosity
115
128
*/
116
- export function decorateWithExplain ( command : Document , options : ExplainOptions ) : Document {
117
- const explain = Explain . fromOptions ( options ) ;
118
- if ( explain === undefined ) return command ;
119
-
129
+ export function decorateWithExplain ( command : Document , explain : Explain ) : Document {
120
130
// A command being explained may not have an explain field directly on it
121
131
if ( command . explain !== undefined ) {
122
132
delete command . explain ;
123
133
}
124
134
125
- command = { explain : command , verbosity : explain . explain } ;
135
+ command = { explain : command , verbosity : explain . verbosity } ;
126
136
return command ;
127
137
}
0 commit comments