antares/src/main/ipc-handlers/connection.js

87 lines
2.3 KiB
JavaScript
Raw Normal View History

2020-05-14 15:21:57 +02:00
import { ipcMain } from 'electron';
import { ClientsFactory } from '../libs/ClientsFactory';
2020-05-14 15:21:57 +02:00
import InformationSchema from '../models/InformationSchema';
2020-06-14 19:02:07 +02:00
import Generic from '../models/Generic';
2020-05-14 15:21:57 +02:00
export default connections => {
ipcMain.handle('test-connection', async (event, conn) => {
const Connection = ClientsFactory.getConnection({
2020-06-05 21:00:15 +02:00
client: conn.client,
2020-06-07 14:38:38 +02:00
params: {
2020-06-05 21:00:15 +02:00
host: conn.host,
port: +conn.port,
user: conn.user,
password: conn.password
}
});
2020-06-07 14:38:38 +02:00
await Connection.connect();
2020-05-14 15:21:57 +02:00
try {
2020-06-07 14:38:38 +02:00
await InformationSchema.testConnection(Connection);
Connection.destroy();
2020-05-14 15:21:57 +02:00
return { status: 'success' };
}
catch (err) {
return { status: 'error', response: err };
}
});
ipcMain.handle('check-connection', async (event, uid) => {
2020-05-14 15:21:57 +02:00
return uid in connections;
});
ipcMain.handle('connect', async (event, conn) => {
const Connection = ClientsFactory.getConnection({
2020-05-14 15:21:57 +02:00
client: conn.client,
2020-06-07 14:38:38 +02:00
params: {
2020-05-14 15:21:57 +02:00
host: conn.host,
port: +conn.port,
user: conn.user,
password: conn.password
},
2020-06-07 14:38:38 +02:00
poolSize: 3
2020-05-14 15:21:57 +02:00
});
try {
2020-06-15 18:23:51 +02:00
await Connection.connect();
2020-06-07 14:38:38 +02:00
const { rows: structure } = await InformationSchema.getStructure(Connection);
connections[conn.uid] = Connection;
2020-05-20 18:00:14 +02:00
return { status: 'success', response: structure };
}
catch (err) {
return { status: 'error', response: err.toString() };
}
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 {
2020-06-07 14:38:38 +02:00
const { rows: structure } = await InformationSchema.getStructure(connections[uid]);
2020-05-20 18:00:14 +02:00
return { status: 'success', response: structure };
2020-05-19 18:06:05 +02:00
}
catch (err) {
return { status: 'error', response: err.toString() };
}
});
2020-06-06 16:27:42 +02:00
ipcMain.handle('raw-query', async (event, { uid, query, schema }) => {
2020-06-07 14:38:38 +02:00
if (!query) return;
2020-06-06 16:27:42 +02:00
try {
2020-06-15 18:23:51 +02:00
const result = await Generic.raw(connections[uid], query, schema);
2020-06-07 14:38:38 +02:00
return { status: 'success', response: result };
2020-06-06 16:27:42 +02:00
}
catch (err) {
return { status: 'error', response: err.toString() };
}
});
2020-05-14 15:21:57 +02:00
};