Skip to content

Commit 289bf53

Browse files
authored
chore: support undefined db/connection (#543)
support undefined db/connection
1 parent 3e7ed9e commit 289bf53

File tree

3 files changed

+14
-21
lines changed

3 files changed

+14
-21
lines changed

packages/mongodb-ts-autocomplete/src/autocompleter.spec.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@ describe('MongoDBAutocompleter', function () {
5151

5252
it('deals with no connection', async function () {
5353
autocompleterContext.currentDatabaseAndConnection = () => {
54-
const error = new Error('No connection');
55-
error.name = 'MongoshInvalidInputError';
56-
throw error;
54+
return undefined;
5755
};
5856

5957
const completions = await autocompleter.autocomplete('db.');

packages/mongodb-ts-autocomplete/src/autocompleter.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -202,22 +202,15 @@ declare global {
202202
}
203203

204204
async autocomplete(code: string): Promise<AutoCompletion[]> {
205-
let connectionId: string;
206-
let databaseName: string;
207-
208-
// If there's no known connection, currentDatabaseAndConnection() will
209-
// error, but we won't be able to generate types for a connection, db
210-
// object, etc, anyway. So just return no results in that case.
211-
try {
212-
({ connectionId, databaseName } =
213-
this.context.currentDatabaseAndConnection());
214-
} catch (err: any) {
215-
if (err.name === 'MongoshInvalidInputError') {
216-
return [];
217-
}
218-
throw err;
205+
// If there's no known connection we won't be able to generate types for a
206+
// connection, db object, etc. So just return no results in that case.
207+
const dbAndConnection = this.context.currentDatabaseAndConnection();
208+
if (!dbAndConnection) {
209+
return [];
219210
}
220211

212+
const { connectionId, databaseName } = dbAndConnection;
213+
221214
const tsAst = compileSourceFile(code);
222215
const collectionName = inferCollectionNameFromFunctionCall(tsAst) || 'test';
223216

packages/mongodb-ts-autocomplete/src/autocompletion-context.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ type CacheOptions = {
99
};
1010

1111
export interface AutocompletionContext {
12-
currentDatabaseAndConnection(): {
13-
connectionId: string;
14-
databaseName: string;
15-
};
12+
currentDatabaseAndConnection():
13+
| {
14+
connectionId: string;
15+
databaseName: string;
16+
}
17+
| undefined;
1618
databasesForConnection(connectionId: string): Promise<string[]>;
1719
collectionsForDatabase(
1820
connectionId: string,

0 commit comments

Comments
 (0)