@@ -58,166 +58,42 @@ const settingsLocation = path.resolve(__dirname, '../../settings.json');
58
58
* is accessed in info.commsData and info.healthData
59
59
*/
60
60
61
- ipcMain . on ( 'connect' , async ( message : Electron . IpcMainEvent , username : string , index : number , URI : string ) => {
61
+ ipcMain . on ( 'connect' , async ( message : Electron . IpcMainEvent , username : string , index : number , URI : string , databaseType : string ) => {
62
62
63
63
try {
64
- const isConnected = mongoose . connection . readyState === 1 ;
65
- if ( isConnected ) {
66
- // console.log('Connection to MongoDb has already been established.');
67
- console . log ( 'A connection to a mongoDB has already been establisehd.' ) ;
68
- mongoose . connection . close ( ( error ) => {
69
- if ( error ) {
70
- console . log ( 'Error closing mongoDB connection: ' , error ) ;
71
- }
72
- else {
73
- console . log ( 'MongoDB connection closed. Reconfiguring connection to new database.' ) ;
74
- mongoose . connect ( URI , { useNewUrlParser : true , useUnifiedTopology : true } )
75
- . then ( ( ) => {
76
- console . log ( 'Connected to user provided mongo database!' ) ;
77
- // testing database
78
- // ServicesModel.find()
79
- // .then(data => console.log(data))
80
- // .catch(err => console.log('error fetching services'));
81
-
82
- message . sender . send ( 'databaseConnected' , 'connected!' ) ;
83
- } )
84
- . catch ( error => {
85
- console . log ( 'Error connecting to MongoDB inside data.ts connection:' , error ) ;
86
- } ) ;
87
- }
88
- } ) ;
89
- } else {
64
+ // set database type from parameter
65
+ currentDatabaseType = databaseType ;
66
+ console . log ( 'Database type: ' , databaseType ) ;
67
+ if ( currentDatabaseType === 'MongoDB' ) {
68
+ // First check if there is already an established mongoose connection with another databse...
69
+ const isConnected = mongoose . connection . readyState === 1 ;
70
+ if ( isConnected ) {
71
+ console . log ( 'A connection to a mongoDB has already been established. Closing connection.' ) ;
72
+ mongoose . connection . close ( ( error ) => {
73
+ if ( error ) {
74
+ console . log ( 'Error closing mongoDB connection: ' , error ) ;
75
+ }
76
+ } ) ;
77
+ }
90
78
console . log ( 'Database connection not found. Establishing connection...' ) ;
91
- // Connect to the proper database
92
- mongoose . connect ( URI , { useNewUrlParser : true , useUnifiedTopology : true } )
93
- . then ( ( ) => {
79
+ // Connect to the proper database
80
+ mongoose . connect ( URI , { useNewUrlParser : true , useUnifiedTopology : true } )
81
+ . then ( ( ) => {
94
82
console . log ( 'Connected to user provided mongo database!' ) ;
95
- // testing database
96
- // ServicesModel.find()
97
- // .then(data => console.log(data))
98
- // .catch(err => console.log('error fetching services'));
99
-
100
83
message . sender . send ( 'databaseConnected' , 'connected!' ) ;
101
- } )
102
- . catch ( error => {
103
- console . log ( 'Error connecting to MongoDB inside data.ts connection:' , error ) ;
104
- } ) ;
105
-
106
- }
107
- } catch ( err ) {
84
+ } )
85
+ . catch ( error => {
86
+ console . log ( 'Error connecting to MongoDB inside data.ts connection:' , error ) ;
87
+ } ) ;
88
+ } else if ( currentDatabaseType === 'SQL' ) {
89
+ // has not been reconfigured to handle different requests to SQL databses.
90
+ pool = await connectPostgres ( index , URI ) ;
91
+ message . sender . send ( 'databaseConnected' , 'connected!' ) ;
92
+ }
93
+ } catch ( err ) {
108
94
console . log ( 'Error connecting to databse: ' , err ) ;
109
95
}
110
96
} ) ;
111
- // console.log(` Error in connect, failed to load application : ${error}`)
112
-
113
-
114
- // ipcMain.on('connect', async (message: Electron.IpcMainEvent, username: string, index: number, URI: string) => {
115
- // try {
116
- // // const isConnected = mongoose.connection.readyState === 1;
117
- // // if (isConnected){
118
- // // console.log('Connection to MongoDb has already been established.');
119
- // // } else {
120
-
121
- // // }
122
- // // Extract databaseType and URI from settings.json at particular index
123
- // // get index from application context
124
-
125
- // // Connect to User database instantiated in 'dashboard.ts'
126
- // if (username !== 'guest') {
127
-
128
- // const isConnected = mongoose.connection.readyState === 1;
129
- // if (isConnected){
130
- // console.log('Connection to MongoDb has already been established.');
131
- // message.sender.send('databaseConnected', 'connection already established.');
132
- // } else {
133
- // return User.findOne({ username: username })
134
- // .then(async (data) => {
135
- // console.log('Hi, inside ipcMain.on connect in data.ts! Establishing connection to user provided database URI');
136
- // const databaseType = data.services[index][1]
137
- // const appURI = data.services[index][2]
138
- // console.log('database type', databaseType);
139
- // console.log('appURI', appURI);
140
- // if (databaseType === 'MongoDB') {
141
- // const shouldbedb = await connectMongo(index, appURI);
142
- // // console.log(shouldbedb);
143
- // // await connectMongo()
144
- // currentDatabaseType = databaseType;
145
- // message.sender.send('databaseConnected', 'connected!');
146
- // } else if (databaseType === 'SQL') {
147
- // pool = await connectPostgres(index, appURI);
148
- // currentDatabaseType = databaseType;
149
- // message.sender.send('databaseConnected', 'connected!');
150
- // }
151
- // console.log('Established connection to user provided URL...');
152
- // console.log('leaving ipcMain.on connect.')
153
- // })
154
- // .catch((error) => {
155
- // console.log(` Error in connect, failed to load application : ${error}`)
156
- // // return false;
157
- // })
158
- // }
159
-
160
- // // const MONGO_URI = URI
161
- // // mongoose.connect(MONGO_URI, {
162
- // // useNewUrlParser: true,
163
- // // useUnifiedtopology: true,
164
- // // })
165
- // // test().catch((error) => console.log('error in second db', error));
166
- // // async function test() {
167
- // // const db2 = await mongoose.createConnection('mongodb+srv://seconddbtest:[email protected] /?retryWrites=true&w=majority');
168
- // // console.log('connection to user provided db established..');
169
- // // }
170
- // // Check for existing user in DB, if found, connect to load application based on database type
171
- // // return User.findOne({ username: username })
172
- // // .then(async (data) => {
173
- // // console.log('Hi, inside ipcMain.on connect in data.ts! Establishing connection to user provided database URI');
174
- // // const databaseType = data.services[index][1]
175
- // // const appURI = data.services[index][2]
176
- // // console.log('database type', databaseType);
177
- // // console.log('appURI', appURI);
178
- // // if (databaseType === 'MongoDB') {
179
- // // const shouldbedb = await connectMongo(index, appURI);
180
- // // // console.log(shouldbedb);
181
- // // // await connectMongo()
182
- // // currentDatabaseType = databaseType;
183
- // // message.sender.send('databaseConnected', 'connected!');
184
- // // } else if (databaseType === 'SQL') {
185
- // // pool = await connectPostgres(index, appURI);
186
- // // currentDatabaseType = databaseType;
187
- // // message.sender.send('databaseConnected', 'connected!');
188
- // // }
189
- // // console.log('Established connection to user provided URL...');
190
- // // console.log('leaving ipcMain.on connect.')
191
- // // })
192
- // // .catch((error) => {
193
- // // console.log(` Error in connect, failed to load application : ${error}`)
194
- // // // return false;
195
- // // })
196
- // }
197
-
198
- // //LOCAL INSTANCE: SETTINGS.JSON
199
- // else {
200
-
201
- // const fileContents = JSON.parse(fs.readFileSync(settingsLocation, 'utf8'));
202
- // const userDatabase = fileContents[username].services[index];
203
- // // We get index from sidebar container: which is the mapplication (DEMO)
204
- // const [databaseType, URI] = [userDatabase[1], userDatabase[2]];
205
-
206
- // console.log('if guest, inputted URI here...', URI)
207
- // // Connect to the proper database
208
- // if (databaseType === 'MongoDB') await connectMongo(index,URI);
209
- // if (databaseType === 'SQL') pool = await connectPostgres(index, URI);
210
-
211
- // // Currently set to a global variable
212
- // currentDatabaseType = databaseType;
213
-
214
- // message.sender.send('databaseConnected', 'connected!');
215
- // // eslint-disable-next-line no-shadow
216
- // }
217
- // } catch ({ message }) {
218
- // console.log('Error in "connect" event', message);
219
- // }
220
- // });
221
97
222
98
/**
223
99
* @event serviceRequest/serviceResponse
@@ -228,17 +104,8 @@ ipcMain.on('servicesRequest', async (message: Electron.IpcMainEvent) => {
228
104
let result : any ;
229
105
console . log ( 'Hi, inside data.ts - servicesRequest function. Fetching services...' ) ;
230
106
231
- console . log ( 'Testing ServicesModel!' ) ;
232
- ServicesModel . find ( )
233
- . then ( data => {
234
- console . log ( 'Testing ServicesModel. Here is the data being sent to front end: ' , data ) ;
235
- result = data ;
236
- message . sender . send ( 'servicesResponse' , JSON . stringify ( result ) ) ;
237
- } )
238
- . catch ( err => console . log ( 'error fetching services' ) ) ;
239
-
240
107
// Mongo Database
241
- // console.log('CurrentDataBase TYPE:', currentDatabaseType);
108
+ console . log ( 'CurrentDataBase TYPE:' , currentDatabaseType ) ;
242
109
if ( currentDatabaseType === 'MongoDB' ) {
243
110
// Get all documents from the services collection
244
111
result = await ServicesModel . find ( ) ;
@@ -254,7 +121,7 @@ ipcMain.on('servicesRequest', async (message: Electron.IpcMainEvent) => {
254
121
255
122
// console.log('Sending servicesResponse to frontend with the following result:', result);
256
123
// Async event emitter - send response
257
- // message.sender.send('servicesResponse', JSON.stringify(result));
124
+ message . sender . send ( 'servicesResponse' , JSON . stringify ( result ) ) ;
258
125
// eslint-disable-next-line no-shadow
259
126
} catch ( { message } ) {
260
127
console . log ( 'Error in "servicesRequest" event' , message ) ;
0 commit comments