@@ -8,7 +8,7 @@ const { name, version } = require('../package.json');
88import { ConnectionModelType } from './connectionModelType' ;
99import { DataServiceType } from './dataServiceType' ;
1010import { createLogger } from './logging' ;
11- import { StatusView , ConnectFormView } from './views' ;
11+ import { StatusView } from './views' ;
1212import { EventEmitter } from 'events' ;
1313import { StorageController , StorageVariables } from './storage' ;
1414import { StorageScope , SavedConnection } from './storage/storageController' ;
@@ -54,10 +54,16 @@ export default class ConnectionController {
5454 ) : void {
5555 let loadedSavedConnection : SavedConnection ;
5656 try {
57+ if ( ! savedConnection . connectionModel ) {
58+ // Ignore empty connections.
59+ return ;
60+ }
61+
5762 loadedSavedConnection = {
5863 id : connectionId ,
59- name : savedConnection . name ,
6064 driverUrl : savedConnection . driverUrl ,
65+ name : savedConnection . name ,
66+ connectionModel : savedConnection . connectionModel ,
6167 storageLocation : savedConnection . storageLocation
6268 } ;
6369 } catch ( error ) {
@@ -101,18 +107,6 @@ export default class ConnectionController {
101107 }
102108 }
103109
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-
116110 public async connectWithURI ( ) : Promise < boolean > {
117111 log . info ( 'connectWithURI command called' ) ;
118112
@@ -140,16 +134,19 @@ export default class ConnectionController {
140134 }
141135
142136 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+ ) ;
147144 } ) ;
148145 }
149146
150147 // Resolves true when the connection is successfully added.
151148 // The connection can fail to connect but be successfully added.
152- public addNewConnectionAndConnect = (
149+ public addNewConnectionStringAndConnect = (
153150 connectionString : string
154151 ) : Promise < boolean > => {
155152 log . info ( 'Trying to connect to a new connection configuration' ) ;
@@ -158,49 +155,71 @@ export default class ConnectionController {
158155 Connection . from (
159156 connectionString ,
160157 ( 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 } ` ) ) ;
163160 }
164161
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+ }
168184
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+ } ;
178187
179- this . _storageController . storeNewConnection ( newConnection ) ;
188+ public saveNewConnectionAndConnect = (
189+ connectionModel : ConnectionModelType
190+ ) : Promise < boolean > => {
191+ const { driverUrl, instanceId } = connectionModel . getAttributes ( {
192+ derived : true
193+ } ) ;
180194
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 ;
184205
185- this . connect ( newConnection . id , newConnectionModel ) . then (
186- ( connectSuccess ) => {
187- if ( ! connectSuccess ) {
188- return resolve ( false ) ;
189- }
206+ this . _storageController . storeNewConnection ( newConnection ) ;
190207
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 ) ;
195212 }
196- ) ;
213+
214+ resolve ( true ) ;
215+ } , reject ) ;
197216 } ) ;
198217 } ;
199218
200- public async connect (
219+ public connect = async (
201220 connectionId : string ,
202221 connectionModel : ConnectionModelType
203- ) : Promise < boolean > {
222+ ) : Promise < boolean > => {
204223 log . info (
205224 'Connect called to connect to instance:' ,
206225 connectionModel . getAttributes ( {
@@ -258,35 +277,39 @@ export default class ConnectionController {
258277 return resolve ( true ) ;
259278 } ) ;
260279 } ) ;
261- }
280+ } ;
262281
263- public async connectWithConnectionId ( connectionId : string ) : Promise < boolean > {
282+ public connectWithConnectionId = ( connectionId : string ) : Promise < boolean > = > {
264283 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+ }
265300 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 ) ;
283306 }
284307 ) ;
285308 } ) ;
286309 }
287310
288311 return Promise . reject ( new Error ( 'Connection not found.' ) ) ;
289- }
312+ } ;
290313
291314 public disconnect ( ) : Promise < boolean > {
292315 log . info (
0 commit comments