@@ -8,7 +8,7 @@ const { name, version } = require('../package.json');
8
8
import { ConnectionModelType } from './connectionModelType' ;
9
9
import { DataServiceType } from './dataServiceType' ;
10
10
import { createLogger } from './logging' ;
11
- import { StatusView , ConnectFormView } from './views' ;
11
+ import { StatusView } from './views' ;
12
12
import { EventEmitter } from 'events' ;
13
13
import { StorageController , StorageVariables } from './storage' ;
14
14
import { StorageScope , SavedConnection } from './storage/storageController' ;
@@ -54,10 +54,16 @@ export default class ConnectionController {
54
54
) : void {
55
55
let loadedSavedConnection : SavedConnection ;
56
56
try {
57
+ if ( ! savedConnection . connectionModel ) {
58
+ // Ignore empty connections.
59
+ return ;
60
+ }
61
+
57
62
loadedSavedConnection = {
58
63
id : connectionId ,
59
- name : savedConnection . name ,
60
64
driverUrl : savedConnection . driverUrl ,
65
+ name : savedConnection . name ,
66
+ connectionModel : savedConnection . connectionModel ,
61
67
storageLocation : savedConnection . storageLocation
62
68
} ;
63
69
} catch ( error ) {
@@ -101,18 +107,6 @@ export default class ConnectionController {
101
107
}
102
108
}
103
109
104
- public addMongoDBConnection (
105
- context : vscode . ExtensionContext
106
- ) : Promise < boolean > {
107
- log . info ( 'mdb.connect command called.' ) ;
108
-
109
- const connectWebView = new ConnectFormView ( ) ;
110
- return connectWebView . showConnectForm (
111
- context ,
112
- this . addNewConnectionAndConnect
113
- ) ;
114
- }
115
-
116
110
public async connectWithURI ( ) : Promise < boolean > {
117
111
log . info ( 'connectWithURI command called' ) ;
118
112
@@ -140,16 +134,19 @@ export default class ConnectionController {
140
134
}
141
135
142
136
return new Promise ( ( resolve ) => {
143
- this . addNewConnectionAndConnect ( connectionString ) . then ( resolve , ( err ) => {
144
- vscode . window . showErrorMessage ( err . message ) ;
145
- resolve ( false ) ;
146
- } ) ;
137
+ this . addNewConnectionStringAndConnect ( connectionString ) . then (
138
+ resolve ,
139
+ ( err ) => {
140
+ vscode . window . showErrorMessage ( err . message ) ;
141
+ resolve ( false ) ;
142
+ }
143
+ ) ;
147
144
} ) ;
148
145
}
149
146
150
147
// Resolves true when the connection is successfully added.
151
148
// The connection can fail to connect but be successfully added.
152
- public addNewConnectionAndConnect = (
149
+ public addNewConnectionStringAndConnect = (
153
150
connectionString : string
154
151
) : Promise < boolean > => {
155
152
log . info ( 'Trying to connect to a new connection configuration' ) ;
@@ -158,49 +155,71 @@ export default class ConnectionController {
158
155
Connection . from (
159
156
connectionString ,
160
157
( error : Error | undefined , newConnectionModel : ConnectionModelType ) => {
161
- if ( error && ! newConnectionModel ) {
162
- return reject ( new Error ( `Unable to load connection: ${ error } ` ) ) ;
158
+ if ( error ) {
159
+ return reject ( new Error ( `Unable to create connection: ${ error } ` ) ) ;
163
160
}
164
161
165
- const { driverUrl, instanceId } = newConnectionModel . getAttributes ( {
166
- derived : true
167
- } ) ;
162
+ return this . saveNewConnectionAndConnect ( newConnectionModel ) . then (
163
+ resolve ,
164
+ reject
165
+ ) ;
166
+ }
167
+ ) ;
168
+ } ) ;
169
+ } ;
170
+
171
+ public parseNewConnectionAndConnect = (
172
+ newConnectionModel
173
+ ) : Promise < boolean > => {
174
+ // Here we re-parse the connection, as it can be loaded from storage or
175
+ // passed by the connection model without the class methods.
176
+ let connectionModel ;
177
+
178
+ try {
179
+ connectionModel = new Connection ( newConnectionModel ) ;
180
+ } catch ( error ) {
181
+ vscode . window . showErrorMessage ( `Unable to load connection: ${ error } ` ) ;
182
+ return Promise . reject ( new Error ( `Unable to load connection: ${ error } ` ) ) ;
183
+ }
168
184
169
- const newConnection : SavedConnection = {
170
- id : uuidv4 ( ) ,
171
- name : instanceId ,
172
- driverUrl,
173
- // To begin we just store it on the session, the storage controller
174
- // handles changing this based on user preference.
175
- storageLocation : StorageScope . NONE
176
- } ;
177
- this . _savedConnections [ newConnection . id ] = newConnection ;
185
+ return this . saveNewConnectionAndConnect ( connectionModel ) ;
186
+ } ;
178
187
179
- this . _storageController . storeNewConnection ( newConnection ) ;
188
+ public saveNewConnectionAndConnect = (
189
+ connectionModel : ConnectionModelType
190
+ ) : Promise < boolean > => {
191
+ const { driverUrl, instanceId } = connectionModel . getAttributes ( {
192
+ derived : true
193
+ } ) ;
180
194
181
- if ( error ) {
182
- return reject ( new Error ( `Unable to connect: ${ error } ` ) ) ;
183
- }
195
+ const newConnection : SavedConnection = {
196
+ id : uuidv4 ( ) ,
197
+ name : instanceId ,
198
+ connectionModel,
199
+ driverUrl,
200
+ // To begin we just store it on the session, the storage controller
201
+ // handles changing this based on user preference.
202
+ storageLocation : StorageScope . NONE
203
+ } ;
204
+ this . _savedConnections [ newConnection . id ] = newConnection ;
184
205
185
- this . connect ( newConnection . id , newConnectionModel ) . then (
186
- ( connectSuccess ) => {
187
- if ( ! connectSuccess ) {
188
- return resolve ( false ) ;
189
- }
206
+ this . _storageController . storeNewConnection ( newConnection ) ;
190
207
191
- resolve ( true ) ;
192
- } ,
193
- reject
194
- ) ;
208
+ return new Promise ( ( resolve , reject ) => {
209
+ this . connect ( newConnection . id , connectionModel ) . then ( ( connectSuccess ) => {
210
+ if ( ! connectSuccess ) {
211
+ return resolve ( false ) ;
195
212
}
196
- ) ;
213
+
214
+ resolve ( true ) ;
215
+ } , reject ) ;
197
216
} ) ;
198
217
} ;
199
218
200
- public async connect (
219
+ public connect = async (
201
220
connectionId : string ,
202
221
connectionModel : ConnectionModelType
203
- ) : Promise < boolean > {
222
+ ) : Promise < boolean > => {
204
223
log . info (
205
224
'Connect called to connect to instance:' ,
206
225
connectionModel . getAttributes ( {
@@ -258,35 +277,39 @@ export default class ConnectionController {
258
277
return resolve ( true ) ;
259
278
} ) ;
260
279
} ) ;
261
- }
280
+ } ;
262
281
263
- public async connectWithConnectionId ( connectionId : string ) : Promise < boolean > {
282
+ public connectWithConnectionId = ( connectionId : string ) : Promise < boolean > = > {
264
283
if ( this . _savedConnections [ connectionId ] ) {
284
+ let connectionModel ;
285
+
286
+ try {
287
+ const savedConnectionModel = this . _savedConnections [ connectionId ]
288
+ . connectionModel ;
289
+ // Here we rebuild the connection model to ensure it's up to date and
290
+ // contains the connection model class methods (not just attributes).
291
+ connectionModel = new Connection (
292
+ savedConnectionModel . getAttributes
293
+ ? savedConnectionModel . getAttributes ( { props : true } )
294
+ : savedConnectionModel
295
+ ) ;
296
+ } catch ( error ) {
297
+ vscode . window . showErrorMessage ( `Unable to load connection: ${ error } ` ) ;
298
+ return Promise . resolve ( false ) ;
299
+ }
265
300
return new Promise ( ( resolve ) => {
266
- Connection . from (
267
- this . _savedConnections [ connectionId ] . driverUrl ,
268
- ( error : Error | undefined , connectionModel : ConnectionModelType ) => {
269
- if ( error && ! connectionModel ) {
270
- vscode . window . showErrorMessage (
271
- `Unable to load connection: ${ error } `
272
- ) ;
273
- return resolve ( false ) ;
274
- }
275
-
276
- return this . connect ( connectionId , connectionModel ) . then (
277
- resolve ,
278
- ( err : Error ) => {
279
- vscode . window . showErrorMessage ( err . message ) ;
280
- return resolve ( false ) ;
281
- }
282
- ) ;
301
+ this . connect ( connectionId , connectionModel ) . then (
302
+ resolve ,
303
+ ( err : Error ) => {
304
+ vscode . window . showErrorMessage ( err . message ) ;
305
+ return resolve ( false ) ;
283
306
}
284
307
) ;
285
308
} ) ;
286
309
}
287
310
288
311
return Promise . reject ( new Error ( 'Connection not found.' ) ) ;
289
- }
312
+ } ;
290
313
291
314
public disconnect ( ) : Promise < boolean > {
292
315
log . info (
0 commit comments