2020-05-14 15:21:57 +02:00
|
|
|
|
|
|
|
import { ipcMain } from 'electron';
|
|
|
|
import knex from 'knex';
|
|
|
|
import InformationSchema from '../models/InformationSchema';
|
|
|
|
|
|
|
|
const connections = {};
|
|
|
|
|
|
|
|
export default () => {
|
|
|
|
ipcMain.handle('testConnection', async (event, conn) => {
|
|
|
|
try {
|
|
|
|
await knex({
|
|
|
|
client: conn.client,
|
|
|
|
connection: {
|
|
|
|
host: conn.host,
|
|
|
|
port: +conn.port,
|
|
|
|
user: conn.user,
|
|
|
|
password: conn.password
|
|
|
|
}
|
|
|
|
}).raw('SELECT 1+1 AS result');
|
|
|
|
|
|
|
|
return { status: 'success' };
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
return { status: 'error', response: err };
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
ipcMain.handle('checkConnection', async (event, uid) => {
|
|
|
|
return uid in connections;
|
|
|
|
});
|
|
|
|
|
|
|
|
ipcMain.handle('connect', async (event, conn) => {
|
2020-05-17 19:34:56 +02:00
|
|
|
let structure;
|
|
|
|
const connection = knex({
|
2020-05-14 15:21:57 +02:00
|
|
|
client: conn.client,
|
|
|
|
connection: {
|
|
|
|
host: conn.host,
|
|
|
|
port: +conn.port,
|
|
|
|
user: conn.user,
|
|
|
|
password: conn.password
|
|
|
|
},
|
|
|
|
pool: {
|
|
|
|
min: 1,
|
|
|
|
max: 3
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-05-17 19:34:56 +02:00
|
|
|
try {
|
|
|
|
structure = await InformationSchema.getStructure(connection);
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
return { status: 'error', response: err.toString() };
|
|
|
|
}
|
|
|
|
|
|
|
|
connections[conn.uid] = connection;
|
|
|
|
return { status: 'success', response: structure };
|
2020-05-14 15:21:57 +02:00
|
|
|
});
|
2020-05-18 18:06:32 +02:00
|
|
|
|
|
|
|
ipcMain.handle('disconnect', (event, uid) => {
|
|
|
|
connections[uid].destroy();
|
|
|
|
delete connections[uid];
|
|
|
|
});
|
2020-05-19 18:06:05 +02:00
|
|
|
|
|
|
|
ipcMain.handle('refresh', async (event, uid) => {
|
|
|
|
try {
|
|
|
|
structure = await InformationSchema.getStructure(connection);
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
return { status: 'error', response: err.toString() };
|
|
|
|
}
|
|
|
|
|
|
|
|
return { status: 'success', response: structure };
|
|
|
|
});
|
2020-05-14 15:21:57 +02:00
|
|
|
};
|