@@ -59,26 +59,26 @@ const settingsLocation = path.resolve(__dirname, '../../settings.json');
59
59
*/
60
60
ipcMain . on ( 'connect' , async ( message : Electron . IpcMainEvent , username : string , index : number , URI : string ) => {
61
61
try {
62
+ // const isConnected = mongoose.connection.readyState === 1;
63
+ // if (isConnected){
64
+ // console.log('Connection to MongoDb has already been established.');
65
+ // } else {
66
+
67
+ // }
62
68
// Extract databaseType and URI from settings.json at particular index
63
69
// get index from application context
64
70
65
71
// Connect to User database instantiated in 'dashboard.ts'
66
72
if ( username !== 'guest' ) {
67
-
68
- // const MONGO_URI = URI
69
- // mongoose.connect(MONGO_URI, {
70
- // useNewUrlParser: true,
71
- // useUnifiedtopology: true,
72
- // })
73
- // test().catch((error) => console.log('error in second db', error));
74
- // async function test() {
75
- // const db2 = await mongoose.createConnection('mongodb+srv://seconddbtest:[email protected] /?retryWrites=true&w=majority');
76
- // console.log('connection to user provided db established..');
77
- // }
78
- // Check for existing user in DB, if found, connect to load application based on database type
79
- return User . findOne ( { username : username } )
73
+
74
+ const isConnected = mongoose . connection . readyState === 1 ;
75
+ if ( isConnected ) {
76
+ console . log ( 'Connection to MongoDb has already been established.' ) ;
77
+ message . sender . send ( 'databaseConnected' , 'connection already established.' ) ;
78
+ } else {
79
+ return User . findOne ( { username : username } )
80
80
. then ( async ( data ) => {
81
- console . log ( 'Hi, inside ipcMain.on connect in data.ts! User db is working, testing second db. ' ) ;
81
+ console . log ( 'Hi, inside ipcMain.on connect in data.ts! Establishing connection to user provided database URI ' ) ;
82
82
const databaseType = data . services [ index ] [ 1 ]
83
83
const appURI = data . services [ index ] [ 2 ]
84
84
console . log ( 'database type' , databaseType ) ;
@@ -94,13 +94,52 @@ ipcMain.on('connect', async (message: Electron.IpcMainEvent, username: string, i
94
94
currentDatabaseType = databaseType ;
95
95
message . sender . send ( 'databaseConnected' , 'connected!' ) ;
96
96
}
97
+ console . log ( 'Established connection to user provided URL...' ) ;
97
98
console . log ( 'leaving ipcMain.on connect.' )
98
99
} )
99
100
. catch ( ( error ) => {
100
101
console . log ( ` Error in connect, failed to load application : ${ error } ` )
101
102
// return false;
102
103
} )
103
104
}
105
+
106
+ // const MONGO_URI = URI
107
+ // mongoose.connect(MONGO_URI, {
108
+ // useNewUrlParser: true,
109
+ // useUnifiedtopology: true,
110
+ // })
111
+ // test().catch((error) => console.log('error in second db', error));
112
+ // async function test() {
113
+ // const db2 = await mongoose.createConnection('mongodb+srv://seconddbtest:[email protected] /?retryWrites=true&w=majority');
114
+ // console.log('connection to user provided db established..');
115
+ // }
116
+ // Check for existing user in DB, if found, connect to load application based on database type
117
+ // return User.findOne({ username: username })
118
+ // .then(async (data) => {
119
+ // console.log('Hi, inside ipcMain.on connect in data.ts! Establishing connection to user provided database URI');
120
+ // const databaseType = data.services[index][1]
121
+ // const appURI = data.services[index][2]
122
+ // console.log('database type', databaseType);
123
+ // console.log('appURI', appURI);
124
+ // if (databaseType === 'MongoDB') {
125
+ // const shouldbedb = await connectMongo(index, appURI);
126
+ // // console.log(shouldbedb);
127
+ // // await connectMongo()
128
+ // currentDatabaseType = databaseType;
129
+ // message.sender.send('databaseConnected', 'connected!');
130
+ // } else if (databaseType === 'SQL') {
131
+ // pool = await connectPostgres(index, appURI);
132
+ // currentDatabaseType = databaseType;
133
+ // message.sender.send('databaseConnected', 'connected!');
134
+ // }
135
+ // console.log('Established connection to user provided URL...');
136
+ // console.log('leaving ipcMain.on connect.')
137
+ // })
138
+ // .catch((error) => {
139
+ // console.log(` Error in connect, failed to load application : ${error}`)
140
+ // // return false;
141
+ // })
142
+ }
104
143
105
144
//LOCAL INSTANCE: SETTINGS.JSON
106
145
else {
@@ -133,7 +172,7 @@ ipcMain.on('connect', async (message: Electron.IpcMainEvent, username: string, i
133
172
ipcMain . on ( 'servicesRequest' , async ( message : Electron . IpcMainEvent ) => {
134
173
try {
135
174
let result : any ;
136
- console . log ( 'Hi, inside data.ts - servicesRequest.' ) ;
175
+ console . log ( 'Hi, inside data.ts - servicesRequest function. Fetching services.. .' ) ;
137
176
// Mongo Database
138
177
if ( currentDatabaseType === 'MongoDB' ) {
139
178
// Get all documents from the services collection
@@ -148,7 +187,7 @@ ipcMain.on('servicesRequest', async (message: Electron.IpcMainEvent) => {
148
187
result = result . rows ;
149
188
}
150
189
151
- console . log ( 'Sending the following result to frontend :' , result ) ;
190
+ console . log ( 'Sending servicesResponse to frontend with the following result:' , result ) ;
152
191
// Async event emitter - send response
153
192
message . sender . send ( 'servicesResponse' , JSON . stringify ( result ) ) ;
154
193
// eslint-disable-next-line no-shadow
0 commit comments