Skip to content

Commit 31fee15

Browse files
authored
feat(shell-api): add .finish() method to ExplainableCursor MONGOSH-1493 (#2475)
1 parent 07bd8c4 commit 31fee15

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

packages/shell-api/src/cursor.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export default class Cursor extends AggregateOrFindCursor<ServiceProviderFindCur
6262

6363
@returnType('Cursor')
6464
@serverVersions([ServerVersions.earliest, '3.2.0'])
65-
addOption(optionFlagNumber: number): Cursor {
65+
addOption(optionFlagNumber: number): this {
6666
if (optionFlagNumber === 4) {
6767
throw new MongoshUnimplementedError(
6868
'the slaveOk option is not supported.',
@@ -86,27 +86,27 @@ export default class Cursor extends AggregateOrFindCursor<ServiceProviderFindCur
8686

8787
@returnType('Cursor')
8888
@serverVersions(['4.4.0', ServerVersions.latest])
89-
allowDiskUse(allow?: boolean): Cursor {
89+
allowDiskUse(allow?: boolean): this {
9090
this._cursor.allowDiskUse(allow);
9191
return this;
9292
}
9393

9494
@returnType('Cursor')
95-
allowPartialResults(): Cursor {
95+
allowPartialResults(): this {
9696
this._addFlag('partial');
9797
return this;
9898
}
9999

100100
@returnType('Cursor')
101101
@serverVersions(['3.4.0', ServerVersions.latest])
102-
collation(spec: CollationOptions): Cursor {
102+
collation(spec: CollationOptions): this {
103103
this._cursor.collation(spec);
104104
return this;
105105
}
106106

107107
@returnType('Cursor')
108108
@serverVersions(['3.2.0', ServerVersions.latest])
109-
comment(cmt: string): Cursor {
109+
comment(cmt: string): this {
110110
this._cursor.comment(cmt);
111111
return this;
112112
}
@@ -130,32 +130,32 @@ export default class Cursor extends AggregateOrFindCursor<ServiceProviderFindCur
130130
}
131131

132132
@returnType('Cursor')
133-
hint(index: string): Cursor {
133+
hint(index: string): this {
134134
this._cursor.hint(index);
135135
return this;
136136
}
137137

138138
@returnType('Cursor')
139-
limit(value: number): Cursor {
139+
limit(value: number): this {
140140
this._cursor.limit(value);
141141
return this;
142142
}
143143

144144
@returnType('Cursor')
145-
max(indexBounds: Document): Cursor {
145+
max(indexBounds: Document): this {
146146
this._cursor.max(indexBounds);
147147
return this;
148148
}
149149

150150
@returnType('Cursor')
151151
@serverVersions(['3.2.0', ServerVersions.latest])
152-
maxAwaitTimeMS(value: number): Cursor {
152+
maxAwaitTimeMS(value: number): this {
153153
this._cursor.maxAwaitTimeMS(value);
154154
return this;
155155
}
156156

157157
@returnType('Cursor')
158-
min(indexBounds: Document): Cursor {
158+
min(indexBounds: Document): this {
159159
this._cursor.min(indexBounds);
160160
return this;
161161
}
@@ -172,13 +172,13 @@ export default class Cursor extends AggregateOrFindCursor<ServiceProviderFindCur
172172
}
173173

174174
@returnType('Cursor')
175-
noCursorTimeout(): Cursor {
175+
noCursorTimeout(): this {
176176
this._addFlag('noCursorTimeout');
177177
return this;
178178
}
179179

180180
@returnType('Cursor')
181-
oplogReplay(): Cursor {
181+
oplogReplay(): this {
182182
this._addFlag('oplogReplay');
183183
return this;
184184
}
@@ -188,7 +188,7 @@ export default class Cursor extends AggregateOrFindCursor<ServiceProviderFindCur
188188
mode: ReadPreferenceLike,
189189
tagSet?: TagSet[],
190190
hedgeOptions?: HedgeOptions
191-
): Cursor {
191+
): this {
192192
let pref: ReadPreferenceLike;
193193

194194
// Only conditionally use readPreferenceFromOptions, for java-shell compatibility.
@@ -207,7 +207,7 @@ export default class Cursor extends AggregateOrFindCursor<ServiceProviderFindCur
207207

208208
@returnType('Cursor')
209209
@serverVersions(['3.2.0', ServerVersions.latest])
210-
returnKey(enabled: boolean): Cursor {
210+
returnKey(enabled: boolean): this {
211211
this._cursor.returnKey(enabled);
212212
return this;
213213
}
@@ -220,7 +220,7 @@ export default class Cursor extends AggregateOrFindCursor<ServiceProviderFindCur
220220
@returnType('Cursor')
221221
@serverVersions(['3.2.0', ServerVersions.latest])
222222
@apiVersions([])
223-
tailable(opts = { awaitData: false }): Cursor {
223+
tailable(opts = { awaitData: false }): this {
224224
this._tailable = true;
225225
this._addFlag('tailable');
226226
if (opts.awaitData) {
@@ -239,13 +239,13 @@ export default class Cursor extends AggregateOrFindCursor<ServiceProviderFindCur
239239

240240
@returnType('Cursor')
241241
@serverVersions(['3.2.0', ServerVersions.latest])
242-
showRecordId(): Cursor {
242+
showRecordId(): this {
243243
this._cursor.showRecordId(true);
244244
return this;
245245
}
246246

247247
@returnType('Cursor')
248-
readConcern(level: ReadConcernLevel): Cursor {
248+
readConcern(level: ReadConcernLevel): this {
249249
this._cursor = this._cursor.withReadConcern({ level });
250250
return this;
251251
}

packages/shell-api/src/explainable-cursor.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { shellApiClassDefault } from './decorators';
1+
import { returnsPromise, shellApiClassDefault } from './decorators';
22
import Cursor from './cursor';
33
import type Mongo from './mongo';
44
import { asPrintable } from './enums';
@@ -21,6 +21,11 @@ export default class ExplainableCursor extends Cursor {
2121
* Internal method to determine what is printed for this class.
2222
*/
2323
async [asPrintable](): Promise<any> {
24+
return await this.finish();
25+
}
26+
27+
@returnsPromise
28+
async finish(): Promise<any> {
2429
// Cache the result so that we don't explain over and over again for the
2530
// same object.
2631
this._explained ??= await this._baseCursor.explain(this._verbosity);

packages/shell-api/src/integration.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1597,6 +1597,16 @@ describe('Shell API (integration)', function () {
15971597
]);
15981598
});
15991599

1600+
it('returns a cursor whose .finish() method returns the explain result', async function () {
1601+
const cursor = (await explainable.find()).skip(1).limit(1);
1602+
const result = await cursor.finish();
1603+
expect(result).to.include.all.keys([
1604+
'ok',
1605+
'queryPlanner',
1606+
'serverInfo',
1607+
]);
1608+
});
1609+
16001610
describe('after server 4.4', function () {
16011611
skipIfServerVersion(testServer, '<= 4.4');
16021612
it('the explainable cursor reflects collation', async function () {

0 commit comments

Comments
 (0)