Skip to content

Commit df6eee5

Browse files
committed
remove unnecessary databaseNames and collectionNames
1 parent fd4973b commit df6eee5

File tree

1 file changed

+21
-18
lines changed
  • packages/mongodb-ts-autocomplete/src

1 file changed

+21
-18
lines changed

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

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,34 +25,42 @@ type MongoDBAutocompleterOptions = {
2525
};
2626

2727
class DatabaseSchema {
28-
private collectionNames: Set<string>;
29-
private collectionSchemas: Record<string, JSONSchema>;
28+
private collectionSchemas: Record<string, JSONSchema | undefined>;
3029

3130
constructor() {
3231
// TODO: this is kinda the only real reason for this class: So we can keep
3332
// track of the known collectionNames for a database. It could be all the
3433
// names from a listCollections() or it could just be all the ones we've
3534
// auto-completed for. The schemas can come from the autocompletion context.
3635
// This can probably be added to the autocompletion context.
37-
this.collectionNames = new Set();
3836
this.collectionSchemas = Object.create(null);
3937
}
4038

4139
setCollectionNames(collectionNames: string[]): void {
42-
this.collectionNames = new Set(collectionNames);
40+
// add the missing ones as undefined
41+
for (const collectionName of collectionNames) {
42+
if (!this.collectionSchemas[collectionName]) {
43+
this.collectionSchemas[collectionName] = undefined;
44+
}
45+
}
46+
47+
// remove the ones that don't exist anymore
48+
const knownCollectionNames = new Set(collectionNames);
49+
for (const key of Object.keys(this.collectionSchemas)) {
50+
if (!knownCollectionNames.has(key)) {
51+
delete this.collectionSchemas[key];
52+
}
53+
}
4354
}
4455

4556
setCollectionSchema(collectionName: string, schema: JSONSchema): void {
46-
this.collectionNames.add(collectionName);
4757
this.collectionSchemas[collectionName] = schema;
4858
}
4959

5060
toTypescriptTypeDefinition(): string {
51-
const collectionProperties = [...this.collectionNames.values()].map(
52-
(collectionName) => {
53-
const def = this.collectionSchemas[collectionName]
54-
? toTypescriptTypeDefinition(this.collectionSchemas[collectionName])
55-
: `{}`;
61+
const collectionProperties = Object.entries(this.collectionSchemas).map(
62+
([collectionName, schema]) => {
63+
const def = schema ? toTypescriptTypeDefinition(schema) : `{}`;
5664
return ` '${collectionName}': ShellAPI.Collection<${def}>;`;
5765
}
5866
);
@@ -64,7 +72,6 @@ class DatabaseSchema {
6472
}
6573

6674
class ConnectionSchema {
67-
private readonly databaseNames: Set<string>;
6875
private readonly databaseSchemas: Record<string, DatabaseSchema>;
6976

7077
constructor() {
@@ -73,12 +80,10 @@ class ConnectionSchema {
7380
// names from a listDatabases() or it could just be all the ones we've
7481
// auto-completed for. The schemas can come from the autocompletion context.
7582
// This can probably be added to the autocompletion context.
76-
this.databaseNames = new Set();
7783
this.databaseSchemas = Object.create(null);
7884
}
7985

8086
addDatabase(databaseName: string) {
81-
this.databaseNames.add(databaseName);
8287
if (!this.databaseSchemas[databaseName]) {
8388
this.databaseSchemas[databaseName] = new DatabaseSchema();
8489
}
@@ -102,11 +107,9 @@ class ConnectionSchema {
102107
}
103108

104109
toTypescriptTypeDefinition(): string {
105-
const databaseProperties = [...this.databaseNames.values()].map(
106-
(databaseName) => {
107-
const def = this.databaseSchemas[databaseName]
108-
? this.databaseSchemas[databaseName].toTypescriptTypeDefinition()
109-
: `{}`;
110+
const databaseProperties = Object.entries(this.databaseSchemas).map(
111+
([databaseName, schema]) => {
112+
const def = schema.toTypescriptTypeDefinition();
110113
return ` '${databaseName}': ShellAPI.Database & ${def}`;
111114
}
112115
);

0 commit comments

Comments
 (0)