@@ -25,34 +25,42 @@ type MongoDBAutocompleterOptions = {
25
25
} ;
26
26
27
27
class DatabaseSchema {
28
- private collectionNames : Set < string > ;
29
- private collectionSchemas : Record < string , JSONSchema > ;
28
+ private collectionSchemas : Record < string , JSONSchema | undefined > ;
30
29
31
30
constructor ( ) {
32
31
// TODO: this is kinda the only real reason for this class: So we can keep
33
32
// track of the known collectionNames for a database. It could be all the
34
33
// names from a listCollections() or it could just be all the ones we've
35
34
// auto-completed for. The schemas can come from the autocompletion context.
36
35
// This can probably be added to the autocompletion context.
37
- this . collectionNames = new Set ( ) ;
38
36
this . collectionSchemas = Object . create ( null ) ;
39
37
}
40
38
41
39
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
+ }
43
54
}
44
55
45
56
setCollectionSchema ( collectionName : string , schema : JSONSchema ) : void {
46
- this . collectionNames . add ( collectionName ) ;
47
57
this . collectionSchemas [ collectionName ] = schema ;
48
58
}
49
59
50
60
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 ) : `{}` ;
56
64
return ` '${ collectionName } ': ShellAPI.Collection<${ def } >;` ;
57
65
}
58
66
) ;
@@ -64,7 +72,6 @@ class DatabaseSchema {
64
72
}
65
73
66
74
class ConnectionSchema {
67
- private readonly databaseNames : Set < string > ;
68
75
private readonly databaseSchemas : Record < string , DatabaseSchema > ;
69
76
70
77
constructor ( ) {
@@ -73,12 +80,10 @@ class ConnectionSchema {
73
80
// names from a listDatabases() or it could just be all the ones we've
74
81
// auto-completed for. The schemas can come from the autocompletion context.
75
82
// This can probably be added to the autocompletion context.
76
- this . databaseNames = new Set ( ) ;
77
83
this . databaseSchemas = Object . create ( null ) ;
78
84
}
79
85
80
86
addDatabase ( databaseName : string ) {
81
- this . databaseNames . add ( databaseName ) ;
82
87
if ( ! this . databaseSchemas [ databaseName ] ) {
83
88
this . databaseSchemas [ databaseName ] = new DatabaseSchema ( ) ;
84
89
}
@@ -102,11 +107,9 @@ class ConnectionSchema {
102
107
}
103
108
104
109
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 ( ) ;
110
113
return ` '${ databaseName } ': ShellAPI.Database & ${ def } ` ;
111
114
}
112
115
) ;
0 commit comments