Skip to content

Commit 416caae

Browse files
committed
fixed mongoose connection error to two different databases. It is currently working but looks ugly, will refactor code. Now user/login/service database is separate from user provided db URL.
1 parent d3667e3 commit 416caae

File tree

8 files changed

+85
-17
lines changed

8 files changed

+85
-17
lines changed

app/context/ApplicationContext.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export const ApplicationContext = React.createContext<any>(null);
2020
const ApplicationContextProvider: React.FC<AppContextProps> = React.memo(props => {
2121
const children = props.children;
2222
const [servicesData, setServicesData] = useState([]);
23+
// v10 note: there is not function for setApp so app is never updated.
2324
const [app, setApp] = useState<string>('');
2425
const [savedMetrics, setSavedMetrics] = useState({});
2526
const [appIndex, setAppIndex] = useState<number>(0);
@@ -70,10 +71,16 @@ const ApplicationContextProvider: React.FC<AppContextProps> = React.memo(props =
7071
});
7172
}, []);
7273

74+
/**
75+
* @function connectToTB - invoked in Services Modal when Service Modal component is first rendered or when useEffect invoked.
76+
* creates an event emitter that connects to the user provided URI for the service (should be the database URI...)
77+
* v10 notes: seems to only be set up for local instances, not when a cloud based service is clicked, causes an issue since a user provided
78+
* database should not exist...
79+
*/
7380
const connectToDB = useCallback((username: string, index: number, application: string, URI: string) => {
81+
7482
ipcRenderer.removeAllListeners('databaseConnected');
7583
ipcRenderer.send('connect', username, index, URI);
76-
7784
ipcRenderer.on('databaseConnected', (event: Electron.Event, data: any) => {
7885
fetchServicesNames(application);
7986
});

app/context/AwsContext.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ const AwsContextProvider: React.FC<Props> = React.memo(({ children }) => {
6969

7070
};
7171

72+
/**
73+
* @event fetchAWSAppInfo - invoked in AWSGraphsContainer
74+
* @desc
75+
* @params username:
76+
* index:
77+
*/
78+
7279
const fetchAwsAppInfo = (username: string, index: number) => {
7380
console.log('Hi, AwsContext -> fetchAwsAppInfo was invoked.');
7481
ipcRenderer.removeAllListeners('awsAppInfoResponse');
@@ -77,7 +84,7 @@ const AwsContextProvider: React.FC<Props> = React.memo(({ children }) => {
7784
ipcRenderer.send('awsAppInfoRequest', username, index);
7885
ipcRenderer.on('awsAppInfoResponse', (event: Electron.Event, res: any) => {
7986
const appInfo = JSON.parse(res);
80-
console.log('received response: ', res);
87+
console.log('received response: ', appInfo);
8188
setAwsAppInfo(appInfo);
8289
});
8390
};

app/modals/ServicesModal.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ interface IService {
2121
// applications[i][2] refers to the local instance URI
2222
const ServicesModal: React.FC<ServicesModalProps> = React.memo(({ i, app }) => {
2323
console.log('Hi, inside ServicesModal. Memoize function invoked in ServicesModal.');
24-
25-
const { user, applications } = useContext(DashboardContext)
24+
25+
const { user, applications } = useContext(DashboardContext);
2626
console.log('user from Dashboard Context:', user);
2727
console.log('applications from Dashboard Context: ', applications);
2828
console.log('applications[i][2]: ', applications[i][2]);
@@ -38,7 +38,9 @@ const ServicesModal: React.FC<ServicesModalProps> = React.memo(({ i, app }) => {
3838
}
3939
};
4040

41+
// note: connectToDB function definition in Application Context. Used
4142
useEffect(() => {
43+
console.log('Hi, inside ServicesModal - connectToDB');
4244
connectToDB(user, i, app, applications[i][2]);
4345
}, [i]);
4446

electron/databases/mongo.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import mongoose from 'mongoose';
22
// import { MongoError } from 'mongodb';
3-
3+
const testURL = 'mongodb+srv://seconddbtest:[email protected]/?retryWrites=true&w=majority';
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.
55
const connectMongoose = async (i:number, URI: string) => {
66
try {
@@ -12,4 +12,15 @@ const connectMongoose = async (i:number, URI: string) => {
1212
console.log(`${__dirname}/mongo.ts/connectMongoose: ${err}`);
1313
}
1414
};
15+
16+
// const connectMongoose = async (i: number, URI: string) => {
17+
// try {
18+
// const db2 = mongoose.createConnection(testURL);
19+
// console.log('connection to user provided db established..');
20+
// return db2;
21+
// } catch (error) {
22+
// console.log('Error connecting to second db... ', error);
23+
// }
24+
// }
25+
1526
export default connectMongoose;

electron/models/ServicesModel.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ const ServicesSchema = new Schema({
1717
});
1818

1919
export default mongoose.model<IService>('services', ServicesSchema);
20+
// export default ServicesSchema;

electron/models/UserModel.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// const MONGO_URI = 'mongodb+srv://chronoslany:[email protected]/?retryWrites=true&w=majority';
2+
const MONGO_URI = 'mongodb+srv://seconddbtest:[email protected]/?retryWrites=true&w=majority';
13
// import mongoose, { Schema, Document } from 'mongoose';
24

35
// export interface IService extends Document {
@@ -6,6 +8,14 @@
68
// }
79
const mongoose = require('mongoose')
810

11+
const db2 = mongoose.createConnection(MONGO_URI);
12+
console.log('establishing connection to database');
13+
// const connectToUserDB = async (URI: string) => {
14+
// console.log('connecting to user info db...')
15+
// return await mongoose.createConnection(URI);
16+
// }
17+
18+
919
const userSchema = new mongoose.Schema({
1020
// User: {
1121
username: {type: String, required:true, unique: true},
@@ -19,5 +29,6 @@ const userSchema = new mongoose.Schema({
1929
// export default mongoose.model<IService>('services', ServicesSchema);
2030

2131
// export default mongoose.model('User', userSchema);
32+
const UserModel = db2.model('users', userSchema);
2233

23-
module.exports = mongoose.model('users', userSchema)
34+
module.exports = UserModel;

electron/routes/dashboard.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ const User = require('../models/UserModel')
88
const mongoose = require('mongoose');
99
// const db = require('../databases/mongo')
1010

11-
const MONGO_URI = 'mongodb+srv://chronoslany:[email protected]/?retryWrites=true&w=majority';
11+
// const MONGO_URI = 'mongodb+srv://chronoslany:[email protected]/?retryWrites=true&w=majority';
1212

13-
main().catch(err => console.log(err));
14-
async function main() {
15-
await mongoose.connect(MONGO_URI);
16-
console.log('user info db connection established...')
17-
}
13+
// main().catch(err => console.log(err));
14+
// async function main() {
15+
// await mongoose.connect(MONGO_URI);
16+
// console.log('user info db connection established...')
17+
// }
1818
// mongoose.connect(MONGO_URI, {
1919
// useNewUrlParser: true,
2020
// useUnifiedtopology: true,
@@ -354,7 +354,8 @@ ipcMain.on('login', (message: IpcMainEvent, user: { username: string; password:
354354

355355
return User.findOne({ username : username })
356356
.then((data) => {
357-
console.log('data', data)
357+
// console.log('data', data)
358+
console.log(data.username, ' is being logged in...');
358359
if (data !== null && bcrypt.compareSync(password, data.password)) {
359360
console.log('User found');
360361
// console.log('found data', data.mode)
@@ -365,6 +366,7 @@ ipcMain.on('login', (message: IpcMainEvent, user: { username: string; password:
365366
message.returnValue = false;
366367
return message.returnValue;
367368
}
369+
368370
})
369371
.catch((error) => {
370372
console.log(`checkUser failed : ${error}`)

electron/routes/data.ts

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,19 @@ const log = require('electron-log');
2020
const mongoose = require('mongoose');
2121
const User = require('../models/UserModel')
2222

23+
// const testURL = 'mongodb+srv://seconddbtest:[email protected]/?retryWrites=true&w=majority';
24+
// const connectMongoose = async (i: number, URI: string) => {
25+
// try {
26+
// const db2 = mongoose.createConnection(testURL);
27+
// console.log('connection to user provided db established..');
28+
// return db2;
29+
// } catch (error) {
30+
// console.log('Error connecting to second db... ', error);
31+
// }
32+
// }
33+
34+
35+
2336
const mongoFetch = fetchData.mongoFetch;
2437
const postgresFetch = fetchData.postgresFetch;
2538
const AWS = require('aws-sdk');
@@ -57,17 +70,23 @@ ipcMain.on('connect', async (message: Electron.IpcMainEvent, username: string, i
5770
// useNewUrlParser: true,
5871
// useUnifiedtopology: true,
5972
// })
60-
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+
// }
6178
// Check for existing user in DB, if found, connect to load application based on database type
6279
return User.findOne({ username: username })
6380
.then(async (data) => {
64-
console.log('Hi, inside ipcMain.on connect in data.ts!');
81+
console.log('Hi, inside ipcMain.on connect in data.ts! User db is working, testing second db.');
6582
const databaseType = data.services[index][1]
6683
const appURI = data.services[index][2]
6784
console.log('database type', databaseType);
6885
console.log('appURI', appURI);
6986
if (databaseType === 'MongoDB') {
70-
await connectMongo(index, appURI)
87+
const shouldbedb = await connectMongo(index, appURI);
88+
// console.log(shouldbedb);
89+
// await connectMongo()
7190
currentDatabaseType = databaseType;
7291
message.sender.send('databaseConnected', 'connected!');
7392
} else if (databaseType === 'SQL') {
@@ -114,7 +133,7 @@ ipcMain.on('connect', async (message: Electron.IpcMainEvent, username: string, i
114133
ipcMain.on('servicesRequest', async (message: Electron.IpcMainEvent) => {
115134
try {
116135
let result: any;
117-
136+
console.log('Hi, inside data.ts - servicesRequest.');
118137
// Mongo Database
119138
if (currentDatabaseType === 'MongoDB' ) {
120139
// Get all documents from the services collection
@@ -129,6 +148,7 @@ ipcMain.on('servicesRequest', async (message: Electron.IpcMainEvent) => {
129148
result = result.rows;
130149
}
131150

151+
console.log('Sending the following result to frontend:', result);
132152
// Async event emitter - send response
133153
message.sender.send('servicesResponse', JSON.stringify(result));
134154
// eslint-disable-next-line no-shadow
@@ -698,6 +718,13 @@ ipcMain.on('eksMetricsRequest', async (message:Electron.IpcMainEvent, username:
698718
// }
699719
// );
700720

721+
/**
722+
* @event awsAppInfoRequest - invoked in fetchAwsAppInfo in ipcRenderer
723+
* @desc Connects to user or guest database and returns a reponse object with the typeOfService,
724+
* region, and awsURL of the services at provided appIndex.
725+
* @params username:
726+
* index:
727+
*/
701728
ipcMain.on(
702729
'awsAppInfoRequest',
703730
async (message: Electron.IpcMainEvent, username: string, appIndex: number) => {

0 commit comments

Comments
 (0)