Skip to content

Commit fadbd9a

Browse files
authored
Merge pull request #6 from xkiso/master
Fixed Async Database Issue
2 parents e3fed00 + 5192410 commit fadbd9a

File tree

5 files changed

+48
-42
lines changed

5 files changed

+48
-42
lines changed

app/context/ApplicationContext.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,20 @@ const ApplicationContextProvider: React.FC = ({ children }) => {
1919
/**
2020
* Connect to database provided by user at 'index'
2121
*/
22-
const connectToDB = async (index: number) => {
22+
const connectToDB = async (index: number, application: string) => {
23+
ipcRenderer.removeAllListeners('databaseConnected');
2324
await ipcRenderer.send('connect', index);
25+
console.log(`${__dirname}/ApplicationContext.tsx/connectToDB: ** between connect & servicesRequest`);
26+
27+
// Response
28+
ipcRenderer.on('databaseConnected', (event: Electron.Event, data: any) => {
29+
// Parse JSON response
30+
const result = data;
31+
if (result.length)
32+
console.log(`${__dirname}/ApplicationContext.tsx/connectToDB: ${result}`);
33+
34+
fetchServicesNames(application);
35+
});
2436
};
2537

2638
/**
@@ -39,6 +51,7 @@ const ApplicationContextProvider: React.FC = ({ children }) => {
3951

4052
// Set local state
4153
setServicesData(result);
54+
ipcRenderer.removeAllListeners('servicesResponse');
4255
});
4356
};
4457

app/modals/ServicesModal.tsx

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,16 @@ const ServicesModal: React.SFC<ServicesModalProps> = ({ i, app }) => {
1919
);
2020

2121
useEffect(() => {
22-
connectToDB(i);
23-
fetchServicesNames(app);
22+
connectToDB(i, app);
23+
// fetchServicesNames(app);
2424

2525
return () => {
2626
setServicesData([]);
2727
};
2828
}, [i]);
2929

30-
/**
31-
* TEMPORARY fix to allow us to fetch service names
32-
* AFTER we connect to the Mongo Database. This error does
33-
* not occur with PostgreSQL databases.
34-
*
35-
* Just click on the whitespace of the modal to run another
36-
* fetch request for service names
37-
*/
38-
const fetchStuff = () => {
39-
fetchServicesNames(app);
40-
};
41-
4230
return (
43-
<div className="servicesContainer" onClick={() => fetchStuff()}>
31+
<div className="services-container">
4432
{!servicesData.length ? (
4533
<div className="loadingMessageModal">
4634
<h2 id="loadingMessage">Loading...</h2>

electron/databases/mongo.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,21 @@ import mongoose from 'mongoose';
22
import { MongoError } from 'mongodb';
33

44
// Mongoose connection wrapped in function that takes the index of the selected database as the parameter. This index is used to target the correct database for querying.
5-
const connectMongoose = (i: number, URI: string) => {
6-
return mongoose.connect(
7-
URI,
8-
{ useNewUrlParser: true, useUnifiedTopology: true },
9-
(err: MongoError) => {
10-
if (err) console.log(err);
11-
console.log('Connected to Mongo database!');
12-
}
13-
);
5+
const connectMongoose = async (i: number, URI: string) => {
6+
try {
7+
const db = await mongoose.connect(
8+
URI,
9+
{ useNewUrlParser: true, useUnifiedTopology: true }
10+
// ,
11+
// (err: MongoError) => {
12+
// if (err) console.log(err);
13+
// console.log('Connected to Mongo database!');
14+
// }
15+
);
16+
console.log(`${__dirname}/mongo.ts/connectMongoose: connected!`);
17+
return db;
18+
} catch(err) {
19+
console.log(`${__dirname}/mongo.ts/connectMongoose: ${err}`);
20+
}
1421
};
1522
export default connectMongoose;

electron/routes/data.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ let currentDatabaseType: string;
2121
*/
2222
ipcMain.on('connect', async (message: Electron.IpcMainEvent, index: number) => {
2323
try {
24-
console.log('Attempting to connect to DB');
24+
console.log('electron/routes/data.ts, ipcMain.on(connect): 1 Attempting to connect to DB');
2525
// Extract databaseType and URI from settings.json at particular index
2626
// get index from application context
2727
const fileContents = fs.readFileSync(path.resolve(__dirname, '../user/settings.json'), 'utf8');
@@ -30,14 +30,18 @@ ipcMain.on('connect', async (message: Electron.IpcMainEvent, index: number) => {
3030
// We get index from sidebar container: which is the mapplication (DEMO)
3131
const [databaseType, URI] = [userDatabase[1], userDatabase[2]];
3232

33+
console.log('electron/routes/data.ts, ipcMain.on(connect): 2 pre-connect');
34+
3335
// Connect to the proper database
3436
if (databaseType === 'MongoDB') await connectMongo(index, URI);
3537
if (databaseType === 'SQL') pool = await connectPostgres(index, URI);
3638

37-
console.log('connected?');
39+
console.log('electron/routes/data.ts, ipcMain.on(connect): 3 connected');
3840

3941
// Currently set to a global variable
4042
currentDatabaseType = databaseType;
43+
44+
message.sender.send('databaseConnected', 'connected!');
4145
} catch ({ message }) {
4246
console.log('Error in "connect" event', message);
4347
}
@@ -49,7 +53,7 @@ ipcMain.on('connect', async (message: Electron.IpcMainEvent, index: number) => {
4953
*/
5054
ipcMain.on('servicesRequest', async (message: Electron.IpcMainEvent) => {
5155
try {
52-
console.log('Requesting application microservices');
56+
console.log('electron/routes/data.ts, ipcMain.on(servicesRequest): 4 Requesting application microservices');
5357
let result: any;
5458

5559
// Mongo Database

electron/user/settings.json

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,21 @@
11
{
22
"setupRequired": false,
33
"services": [
4-
[
5-
"myPostgres",
6-
"SQL",
7-
"postgres://zwezmnqm:[email protected]:5432/zwezmnqm",
8-
"Online bookstore with that keeps track of orders and customers",
9-
"Jun 28, 2020 4:58 PM"
10-
],
11-
[
12-
"myMongo",
13-
"MongoDB",
14-
"mongodb+srv://chronos:[email protected]/chronos?retryWrites=true&w=majority",
15-
"E-commerce website for buying and selling clothes",
16-
"Jul 01, 2020 10:33 PM"
17-
],
184
[
195
"ToddDB",
206
"MongoDB",
217
"mongodb+srv://tdwolf6:[email protected]/Chronos?retryWrites=true&w=majority",
228
"Web app deployed on AWS",
239
"Jul 3, 2020 7:12AM"
2410
],
11+
[
12+
13+
"myPostgres",
14+
"SQL",
15+
"postgres://zwezmnqm:[email protected]:5432/zwezmnqm",
16+
"Online bookstore with that keeps track of orders and customers",
17+
"Jun 28, 2020 4:58 PM"
18+
],
2519
[
2620
"pattyDB",
2721
"MongoDB",

0 commit comments

Comments
 (0)