mirror of
https://github.com/dbgate/dbgate
synced 2024-11-07 20:26:23 +00:00
connectors refactor
This commit is contained in:
parent
235ef4764b
commit
948af4984b
@ -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 });
|
||||
}
|
||||
});
|
@ -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;
|
||||
},
|
||||
};
|
||||
|
17
api/src/controllers/serverConnections.js
Normal file
17
api/src/controllers/serverConnections.js
Normal 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);
|
||||
},
|
||||
};
|
@ -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 };
|
||||
};
|
20
api/src/engines/mssql/index.js
Normal file
20
api/src/engines/mssql/index.js
Normal 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;
|
||||
},
|
||||
};
|
@ -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 });
|
||||
});
|
||||
});
|
||||
};
|
25
api/src/engines/mysql/index.js
Normal file
25
api/src/engines/mysql/index.js
Normal 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 }));
|
||||
},
|
||||
};
|
@ -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 };
|
||||
};
|
22
api/src/engines/postgres/index.js
Normal file
22
api/src/engines/postgres/index.js
Normal 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;
|
||||
},
|
||||
};
|
11
api/src/proc/connectProcess.js
Normal file
11
api/src/proc/connectProcess.js
Normal 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 });
|
||||
}
|
||||
});
|
20
api/src/proc/serverConnectionProcess.js
Normal file
20
api/src/proc/serverConnectionProcess.js
Normal 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 });
|
||||
}
|
||||
});
|
@ -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`
|
||||
|
Loading…
Reference in New Issue
Block a user