connectors refactor

This commit is contained in:
Jan Prochazka 2020-01-04 21:59:53 +01:00
parent 235ef4764b
commit 948af4984b
12 changed files with 130 additions and 46 deletions

View File

@ -1,9 +0,0 @@
process.on('message', async connection => {
try {
const connectFunc = require(`./engines/${connection.engine}/connect`);
const res = await connectFunc(connection);
process.send(res);
} catch (e) {
process.send({ error: e.message });
}
});

View File

@ -1,8 +1,4 @@
const os = require('os');
const path = require('path');
const fs = require('fs-extra');
const express = require('express');
const router = express.Router();
const { fork } = require('child_process');
const _ = require('lodash');
const nedb = require('nedb-promises');
@ -12,6 +8,8 @@ const socket = require('../utility/socket');
module.exports = {
datastore: null,
opened: [],
async _init() {
const dir = await datadir();
this.datastore = nedb.create(path.join(dir, 'connections.jsonl'));
@ -27,7 +25,7 @@ module.exports = {
raw: true,
},
test(req, res) {
const subprocess = fork(`${__dirname}/../connectProcess.js`);
const subprocess = fork(`${__dirname}/../proc/connectProcess.js`);
subprocess.send(req.body);
subprocess.on('message', resp => res.json(resp));
},
@ -46,8 +44,14 @@ module.exports = {
delete_meta: 'post',
async delete(connection) {
let res = await this.datastore.remove(_.pick(connection, '_id'));
const res = await this.datastore.remove(_.pick(connection, '_id'));
socket.emit('connection-list-changed');
return res;
},
get_meta: 'get',
async get({ id }) {
const res = await this.datastore.find({ _id: id });
return res;
},
};

View File

@ -0,0 +1,17 @@
const connections = require('./connections');
const socket = require('../utility/socket');
module.exports = {
opened: [],
async ensureOpened(id) {
const existing = this.opened.find(x => x.connection.id == id);
if (existing) return existing;
},
listDatabases_meta: 'get',
async listDatabases({ id }) {
const opened = this.ensureOpened(id);
},
};

View File

@ -1,8 +0,0 @@
const mssql = require('mssql');
module.exports = async function connect({ server, port, user, password }) {
const pool = await mssql.connect({ server, port, user, password });
const resp = await pool.request().query('SELECT @@VERSION AS version');
const { version } = resp.recordset[0];
return { version };
};

View File

@ -0,0 +1,20 @@
const mssql = require('mssql');
module.exports = {
async connect({ server, port, user, password }) {
const pool = await mssql.connect({ server, port, user, password });
return pool;
},
async query(pool, sql) {
const resp = await pool.request().query(sql);
return resp.recordset;
},
async getVersion(pool) {
const { version } = (await this.query(pool, 'SELECT @@VERSION AS version'))[0];
return { version };
},
async listDatabases(pool) {
const res = await this.query(pool, 'SELECT name FROM sys.databases order by name');
return res;
},
};

View File

@ -1,12 +0,0 @@
const mysql = require('mysql');
module.exports = function connect({ server, port, user, password }) {
return new Promise((resolve, reject) => {
const connection = mysql.createConnection({ host: server, port, user, password });
connection.query("show variables like 'version'", function(error, results, fields) {
if (error) reject(error);
const version = results[0].Value;
resolve({ version });
});
});
};

View File

@ -0,0 +1,25 @@
const mysql = require('mysql');
module.exports = {
async connect({ server, port, user, password }) {
const connection = mysql.createConnection({ host: server, port, user, password });
return connection;
},
async query(connection, sql) {
return new Promise((resolve, reject) => {
connection.query(sql, function(error, results, fields) {
if (error) reject(error);
resolve(results);
});
});
},
async getVersion(connection) {
const rows = await this.query(connection, "show variables like 'version'");
const version = rows[0].Value;
return { version };
},
async listDatabases(connection) {
const res = await this.query(connection, 'show databases');
return res.map(x => ({ name: x.Database }));
},
};

View File

@ -1,10 +0,0 @@
const { Client } = require('pg');
module.exports = async function connect({ server, port, user, password }) {
const client = new Client({ host: server, port, user, password, database: 'postgres' });
await client.connect();
const res = await client.query('SELECT version()');
const { version } = res.rows[0];
await client.end();
return { version };
};

View File

@ -0,0 +1,22 @@
const { Client } = require('pg');
module.exports = {
async connect({ server, port, user, password }) {
const client = new Client({ host: server, port, user, password, database: 'postgres' });
await client.connect();
return client;
},
async query(client, sql) {
const res = await client.query(sql);
return res.rows;
},
async getVersion(client) {
const rows = await this.query(client, 'SELECT version()');
const { version } = rows[0];
return { version };
},
async listDatabases(client) {
const res = await this.query(client, 'SELECT datname AS name FROM pg_database WHERE datistemplate = false');
return res;
},
};

View File

@ -0,0 +1,11 @@
process.on('message', async connection => {
try {
const driver = require(`../engines/${connection.engine}/index`);
const conn = await driver.connect(connection);
const res = await driver.getVersion(conn);
process.send(res);
} catch (e) {
console.log(e);
process.send({ error: e.message });
}
});

View File

@ -0,0 +1,20 @@
function handleConnect() {}
const messageHandlers = {
connect: handleConnect,
};
function handleMessage({ type, ...other }) {
const handler = messageHandlers[type];
handler(other);
}
process.on('message', async connection => {
try {
const connectFunc = require(`../engines/${connection.engine}/connect`);
const res = await connectFunc(connection);
process.send(res);
} catch (e) {
process.send({ error: e.message });
}
});

View File

@ -3,7 +3,11 @@ import styled from 'styled-components';
import { showMenu } from '../modals/DropDownMenu';
const AppObjectDiv = styled.div`
margin: 5px;
padding: 5px;
&:hover {
background-color: lightblue;
};
cursor: pointer;
`;
const IconWrap = styled.span`