Skip to content

Commit

Permalink
Merge pull request #3 from oslabs-beta/junaid-getlists
Browse files Browse the repository at this point in the history
chase and junaid refactored models page
  • Loading branch information
junaid-ahmed7 authored Apr 3, 2023
2 parents da7444b + a39e259 commit c4cadac
Show file tree
Hide file tree
Showing 7 changed files with 578 additions and 295 deletions.
16 changes: 11 additions & 5 deletions backend/BE_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface TableDetails {
columns?: ColumnObj[];
}
export interface DBList {
databaseConnected: boolean[];
databaseConnected: [boolean, boolean, boolean, boolean];
databaseList: dbDetails[];
tableList: TableDetails[];
}
Expand All @@ -40,6 +40,8 @@ export type BackendObjType = {
export enum DBType {
Postgres = 'pg',
MySQL = 'mysql',
RDSPostgres = 'rds-pg',
RDSMySQL = 'rds-mysql',
}

export enum LogType {
Expand All @@ -58,8 +60,12 @@ export interface DocConfigFile {
pg_user: string;
pg_pass: string;
pg_port: number | string;
rds_host: string;
rds_user: string;
rds_pass: string;
rds_port: string | number;
rds_mysql_host: string;
rds_mysql_user: string;
rds_mysql_pass: string;
rds_mysql_port: number | string;
rds_pg_host: string;
rds_pg_user: string;
rds_pg_pass: string;
rds_pg_port: number | string;
}
40 changes: 31 additions & 9 deletions backend/_documentsConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,20 @@ const writeConfigDefault = function (): DocConfigFile {
logger('Could not find config file. Creating default', LogType.WARNING);

const defaultFile: DocConfigFile = {
mysql_user: 'mysql',
mysql_pass: 'mysql',
mysql_user: '',
mysql_pass: '',
mysql_port: 3306,
pg_user: 'postgres',
pg_pass: 'postgres',
pg_user: '',
pg_pass: '',
pg_port: 5432,
rds_host: 'AWS RDS',
rds_user: 'RDS',
rds_pass: 'password',
rds_port: 'Port',
rds_mysql_host: '',
rds_mysql_user: '',
rds_mysql_pass: '',
rds_mysql_port: 3306,
rds_pg_host: '',
rds_pg_user: '',
rds_pg_pass: '',
rds_pg_port: 5432,
};

fs.writeFileSync(configPath, JSON.stringify(defaultFile));
Expand All @@ -49,7 +53,8 @@ interface DocConfig {
getConfigFolder: () => string;
getCredentials: (dbType: DBType) => {
user: string;
pass: string;
pass?: string;
password?: string;
port: number | string;
};
getFullConfig: () => Object;
Expand Down Expand Up @@ -95,6 +100,23 @@ const docConfig: DocConfig = {
port: configFile.mysql_port,
};
}
if( dbType === DBType.RDSMySQL) { // added grabbing RDSMySQL credentials
return {
host: configFile.rds_mysql_host,
user: configFile.rds_mysql_user,
password: configFile.rds_mysql_pass,
port: configFile.rds_mysql_port,
};
}
if( dbType === DBType.RDSPostgres) { // added grabbing RDSPG credentials
return {
host: configFile.rds_pg_host,
user: configFile.rds_pg_user,
password: configFile.rds_pg_pass,
port: configFile.rds_pg_port,
};
}

logger('Could not get credentials of DBType: ', LogType.ERROR, dbType);
return { user: 'none', pass: 'none', port: 1 };
},
Expand Down
22 changes: 16 additions & 6 deletions backend/channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,21 @@ ipcMain.handle('get-config', async (event, configObj) => {
});

// Listen for request from front-end and send back the DB List upon request
ipcMain.on('return-db-list', (event, dbType: DBType = DBType.Postgres) => {
//JUNAID AND CHASE
//removed the parameters because it doesnt seem like they do anything here, and it prevents the other databses from rendering on the list if pg is passed in.
// ipcMain.on('return-db-list', (event, dbType: DBType = DBType.Postgres) => {
ipcMain.on('return-db-list', (event) => {
logger(
"Received 'return-db-list' (Note: No Async being sent here)",
LogType.RECEIVE
);

db.setBaseConnections()
.then(() => {
db.getLists('', dbType)
//JUNAID AND CHASE
//removed the parameters because it doesnt seem like they do anything here, and it prevents the other databses from rendering on the list if pg is passed in.
// db.getLists('', dbType)
db.getLists()
.then((data: DBList) => {
event.sender.send('db-lists', data);
logger("Sent 'db-lists' from 'return-db-list'", LogType.SEND);
Expand Down Expand Up @@ -440,7 +446,8 @@ interface dummyDataRequestPayload {
rows: number;
}

ipcMain.handle( // generate dummy data
ipcMain.handle(
// generate dummy data
'generate-dummy-data',
async (event, data: dummyDataRequestPayload, dbType: DBType) => {
logger("Received 'generate-dummy-data'", LogType.RECEIVE);
Expand All @@ -453,11 +460,14 @@ ipcMain.handle( // generate dummy data
};
try {
// console.log('data in generate-dummy-data', data); // gets here fine

// Retrieves the Primary Keys and Foreign Keys for all the tables
const tableInfo: ColumnObj[] = await db.getTableInfo(data.tableName, dbType); // passed in dbType to second argument
const tableInfo: ColumnObj[] = await db.getTableInfo(
data.tableName,
dbType
); // passed in dbType to second argument
// console.log('tableInfo in generate-dummy-data', tableInfo); // working

// generate dummy data
const dummyArray: DummyRecords = await generateDummyData(
tableInfo,
Expand Down
65 changes: 65 additions & 0 deletions backend/databaseConnections.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//ATTEMPT AT EXTRACTING TO ANOTHER FILE, NOT WORKING YET :(

const poolVariables = require('./models');
const { Pool } = require('pg');
const mysql = require('mysql2/promise');
import logger from './Logging/masterlog';
import { LogType } from './BE_types';

module.exports = {
PG_DBConnect: async function (pg_uri: string, db: string) {
const newURI = `${pg_uri}${db}`;
const newPool = new Pool({ connectionString: newURI });
if (poolVariables.pools.pg_pool) await poolVariables.pools.pg_pool.end();
poolVariables.pools.pg_pool = newPool;
logger(`New pool URI set: ${newURI}`, LogType.SUCCESS);
},

MSQL_DBConnect: async function (MYSQL_CREDS: any) {
if (poolVariables.pools.msql_pool)
await poolVariables.pools.msql_pool.end();
poolVariables.pools.msql_pool = mysql.createPool({ ...MYSQL_CREDS });
},

MSQL_DBQuery: function (db: string) {
poolVariables.pools.msql_pool
.query(`USE ${db};`)
.then(() => {
logger(`Connected to MSQL DB: ${db}`, LogType.SUCCESS);
})
.catch((err) => {
logger(`Couldnt connect to MSQL DB: ${db}`, LogType.ERROR);
});
},

RDS_PG_DBConnect: async function (RDS_PG_INFO) {
poolVariables.pools.rds_pg_pool = new Pool({ ...RDS_PG_INFO });
poolVariables.pools.rds_pg_pool.connect((err) => {
if (err) console.log(err, 'ERR PG');
else console.log('CONNECTED TO RDS PG DATABASE!');
});
},

RDS_MSQL_DBConnect: async function (RDS_MSQL_INFO) {
if (poolVariables.pools.rds_msql_pool)
await poolVariables.pools.rds_msql_pool.end();
poolVariables.pools.rds_msql_pool = mysql.createPool({ ...RDS_MSQL_INFO });
const testQuery = await poolVariables.pools.rds_msql_pool.query(
'SHOW DATABASES;'
); // just a test query to make sure were connected (it works, i tested with other queries creating tables too)
console.log(
`CONNECTED TO RDS ${testQuery[0][1].Database.toUpperCase()} DATABASE!`
);
},

RDS_MSQL_DBQuery: function (db: string) {
poolVariables.pools.rds_msql_pool
.query(`USE ${db};`)
.then(() => {
logger(`Connected to MSQL DB: ${db}`, LogType.SUCCESS);
})
.catch((err) => {
logger(`Couldnt connect to MSQL DB: ${db}`, LogType.ERROR);
});
},
};
9 changes: 6 additions & 3 deletions backend/main.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import { BrowserWindow, Menu } from 'electron';

const { app } = require('electron');
const { app, session } = require('electron');

const dev: boolean = process.env.NODE_ENV === 'development';

const os = require('os');
const path = require('path');
const url = require('url');
const fixPath = require('fix-path');
Expand All @@ -16,7 +16,10 @@ require('./channels');
fixPath();
// Keep a global reference of the window objects, if you don't, the window will be closed automatically when the JavaScript object is garbage collected.
let mainWindow: BrowserWindow | null;

const reactDevToolsPath = path.join(os.homedir(), '/Desktop/ReactDevTools');
app.whenReady().then(async () => {
await session.defaultSession.loadExtension(reactDevToolsPath);
});
function createWindow() {
mainWindow = new BrowserWindow({
width: 1800,
Expand Down
Loading

0 comments on commit c4cadac

Please sign in to comment.