Skip to content

Commit 2ffef9c

Browse files
committed
allow opting out of schema sampling with config disableSchemaSampling=false
1 parent edffa90 commit 2ffef9c

File tree

5 files changed

+61
-19
lines changed

5 files changed

+61
-19
lines changed

packages/autocomplete/src/index.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,9 @@ function filterShellAPI(
411411

412412
type AutocompleteShellInstanceState = {
413413
getAutocompleteParameters: () => AutocompleteParameters;
414-
getAutocompletionContext: () => AutocompletionContext;
414+
getAutocompletionContext: (options: {
415+
disableSchemaSampling?: boolean;
416+
}) => AutocompletionContext;
415417
};
416418

417419
function transformAutocompleteResults(
@@ -429,15 +431,22 @@ export async function initNewAutocompleter(
429431
instanceState: Pick<
430432
AutocompleteShellInstanceState,
431433
'getAutocompletionContext'
432-
>
434+
>,
435+
{
436+
disableSchemaSampling = false,
437+
}: {
438+
disableSchemaSampling?: boolean;
439+
}
433440
): Promise<(text: string) => Promise<CompletionResults>> {
434441
// only import the autocompleter code the first time we need it to
435442
// hide the time it takes from the initial startup time
436443
const { MongoDBAutocompleter } = await import(
437444
'@mongodb-js/mongodb-ts-autocomplete'
438445
);
439446

440-
const autocompletionContext = instanceState.getAutocompletionContext();
447+
const autocompletionContext = instanceState.getAutocompletionContext({
448+
disableSchemaSampling,
449+
});
441450
const mongoDBCompleter = new MongoDBAutocompleter({
442451
context: autocompletionContext,
443452
});

packages/cli-repl/src/mongosh-repl.spec.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ describe('MongoshNodeRepl', function () {
379379
});
380380
const initialized = await mongoshRepl.initialize(serviceProvider);
381381
await mongoshRepl.startRepl(initialized);
382+
await mongoshRepl.setConfig('disableSchemaSampling', false);
382383
});
383384

384385
it('provides an editor action', async function () {
@@ -481,14 +482,34 @@ describe('MongoshNodeRepl', function () {
481482
await tick();
482483
expect(output, output).to.include('db.coll.updateOne');
483484
});
484-
// this will eventually be supported in the new autocomplete
485-
it.skip('autocompletes collection schema fields', async function () {
486-
input.write('db.coll.find({');
485+
it('autocompletes collection schema fields', async function () {
486+
if (!process.env.USE_NEW_AUTOCOMPLETE) {
487+
// auto-completing collection field names only supported by new autocomplete
488+
this.skip();
489+
}
490+
input.write('db.coll.find({fo');
487491
await tabtab();
488492
await waitCompletion(bus);
489493
await tick();
490494
expect(output, output).to.include('db.coll.find({foo');
491495
});
496+
497+
it('does not autocomplete collection schema fields if disableSchemaSampling=true', async function () {
498+
if (!process.env.USE_NEW_AUTOCOMPLETE) {
499+
// auto-completing collection field names only supported by new autocomplete
500+
this.skip();
501+
}
502+
await mongoshRepl.setConfig('disableSchemaSampling', true);
503+
try {
504+
input.write('db.coll.find({fo');
505+
await tabtab();
506+
await waitCompletion(bus);
507+
await tick();
508+
expect(output, output).to.not.include('db.coll.find({foo');
509+
} finally {
510+
await mongoshRepl.setConfig('disableSchemaSampling', false);
511+
}
512+
});
492513
it('autocompletes shell-api methods (once)', async function () {
493514
input.write('vers');
494515
await tabtab();

packages/cli-repl/src/mongosh-repl.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,11 @@ class MongoshNodeRepl implements EvaluationListener {
457457
(async () => {
458458
if (process.env.USE_NEW_AUTOCOMPLETE) {
459459
if (!newMongoshCompleter) {
460-
newMongoshCompleter = await initNewAutocompleter(instanceState);
460+
newMongoshCompleter = await initNewAutocompleter(instanceState, {
461+
disableSchemaSampling: await this.getConfig(
462+
'disableSchemaSampling'
463+
),
464+
});
461465
}
462466

463467
return newMongoshCompleter(text);

packages/shell-api/src/shell-instance-state.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,11 @@ export class ShellInstanceState {
415415
throw new Error(`mongo with connection id ${connectionId} not found`);
416416
}
417417

418-
public getAutocompletionContext(): AutocompletionContext {
418+
public getAutocompletionContext({
419+
disableSchemaSampling = false,
420+
}: {
421+
disableSchemaSampling?: boolean;
422+
}): AutocompletionContext {
419423
return {
420424
currentDatabaseAndConnection: ():
421425
| {
@@ -483,17 +487,19 @@ export class ShellInstanceState {
483487
): Promise<JSONSchema> => {
484488
const mongo = this.getMongoByConnectionId(connectionId);
485489
let docs: Document[] = [];
486-
try {
487-
docs = await mongo
488-
._getDb(databaseName)
489-
.getCollection(collectionName)
490-
._getSampleDocsForCompletion();
491-
} catch (err: any) {
492-
if (
493-
err?.code !== ShellApiErrors.NotConnected &&
494-
err?.codeName !== 'Unauthorized'
495-
) {
496-
throw err;
490+
if (!disableSchemaSampling) {
491+
try {
492+
docs = await mongo
493+
._getDb(databaseName)
494+
.getCollection(collectionName)
495+
._getSampleDocsForCompletion();
496+
} catch (err: any) {
497+
if (
498+
err?.code !== ShellApiErrors.NotConnected &&
499+
err?.codeName !== 'Unauthorized'
500+
) {
501+
throw err;
502+
}
497503
}
498504
}
499505

packages/types/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,7 @@ export class CliUserConfig extends SnippetShellUserConfig {
520520
logMaxFileCount = 100;
521521
logCompressionEnabled = false;
522522
logRetentionGB: number | undefined = undefined;
523+
disableSchemaSampling = false;
523524
}
524525

525526
export class CliUserConfigValidator extends SnippetShellUserConfigValidator {
@@ -557,6 +558,7 @@ export class CliUserConfigValidator extends SnippetShellUserConfigValidator {
557558
case 'forceDisableTelemetry':
558559
case 'showStackTraces':
559560
case 'logCompressionEnabled':
561+
case 'disableSchemaSampling':
560562
if (typeof value !== 'boolean') {
561563
return `${key} must be a boolean`;
562564
}

0 commit comments

Comments
 (0)