@@ -59,26 +59,26 @@ const settingsLocation = path.resolve(__dirname, '../../settings.json');
5959 */
6060ipcMain . on ( 'connect' , async ( message : Electron . IpcMainEvent , username : string , index : number , URI : string ) => {
6161 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+ // }
6268 // Extract databaseType and URI from settings.json at particular index
6369 // get index from application context
6470
6571 // Connect to User database instantiated in 'dashboard.ts'
6672 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:seconddbtest@cluster0.yhztme0.mongodb.net/?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 } )
8080 . 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 ' ) ;
8282 const databaseType = data . services [ index ] [ 1 ]
8383 const appURI = data . services [ index ] [ 2 ]
8484 console . log ( 'database type' , databaseType ) ;
@@ -94,13 +94,52 @@ ipcMain.on('connect', async (message: Electron.IpcMainEvent, username: string, i
9494 currentDatabaseType = databaseType ;
9595 message . sender . send ( 'databaseConnected' , 'connected!' ) ;
9696 }
97+ console . log ( 'Established connection to user provided URL...' ) ;
9798 console . log ( 'leaving ipcMain.on connect.' )
9899 } )
99100 . catch ( ( error ) => {
100101 console . log ( ` Error in connect, failed to load application : ${ error } ` )
101102 // return false;
102103 } )
103104 }
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:seconddbtest@cluster0.yhztme0.mongodb.net/?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+ }
104143
105144 //LOCAL INSTANCE: SETTINGS.JSON
106145 else {
@@ -133,7 +172,7 @@ ipcMain.on('connect', async (message: Electron.IpcMainEvent, username: string, i
133172ipcMain . on ( 'servicesRequest' , async ( message : Electron . IpcMainEvent ) => {
134173 try {
135174 let result : any ;
136- console . log ( 'Hi, inside data.ts - servicesRequest.' ) ;
175+ console . log ( 'Hi, inside data.ts - servicesRequest function. Fetching services.. .' ) ;
137176 // Mongo Database
138177 if ( currentDatabaseType === 'MongoDB' ) {
139178 // Get all documents from the services collection
@@ -148,7 +187,7 @@ ipcMain.on('servicesRequest', async (message: Electron.IpcMainEvent) => {
148187 result = result . rows ;
149188 }
150189
151- console . log ( 'Sending the following result to frontend :' , result ) ;
190+ console . log ( 'Sending servicesResponse to frontend with the following result:' , result ) ;
152191 // Async event emitter - send response
153192 message . sender . send ( 'servicesResponse' , JSON . stringify ( result ) ) ;
154193 // eslint-disable-next-line no-shadow
0 commit comments