mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-20 00:46:01 +00:00
TableDataTab
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
const _ = require('lodash');
|
||||
const uuidv1 = require('uuid/v1');
|
||||
const connections = require('./connections');
|
||||
const socket = require('../utility/socket');
|
||||
const { fork } = require('child_process');
|
||||
@@ -7,6 +8,7 @@ const DatabaseAnalyser = require('../engines/default/DatabaseAnalyser');
|
||||
module.exports = {
|
||||
/** @type {import('../types').OpenedDatabaseConnection[]} */
|
||||
opened: [],
|
||||
requests: {},
|
||||
|
||||
handle_structure(id, database, { structure }) {
|
||||
const existing = this.opened.find(x => x.id == id && x.database == database);
|
||||
@@ -17,6 +19,11 @@ module.exports = {
|
||||
handle_error(id, { error }) {
|
||||
console.log(error);
|
||||
},
|
||||
handle_response(id, database, { msgid, ...response }) {
|
||||
const [resolve, reject] = this.requests[msgid];
|
||||
resolve(response);
|
||||
delete this.requests[msgid];
|
||||
},
|
||||
|
||||
async ensureOpened(id, database) {
|
||||
const existing = this.opened.find(x => x.id == id && x.database == database);
|
||||
@@ -39,6 +46,16 @@ module.exports = {
|
||||
return newOpened;
|
||||
},
|
||||
|
||||
/** @param {import('../types').OpenedDatabaseConnection} conn */
|
||||
async sendRequest(conn, message) {
|
||||
const msgid = uuidv1();
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
this.requests[msgid] = [resolve, reject];
|
||||
conn.subprocess.send({ msgid, ...message });
|
||||
});
|
||||
return promise;
|
||||
},
|
||||
|
||||
listObjects_meta: 'get',
|
||||
async listObjects({ id, database }) {
|
||||
const opened = await this.ensureOpened(id, database);
|
||||
|
||||
11
api/src/controllers/tables.js
Normal file
11
api/src/controllers/tables.js
Normal file
@@ -0,0 +1,11 @@
|
||||
const _ = require('lodash');
|
||||
const databaseConnections = require('./databaseConnections');
|
||||
|
||||
module.exports = {
|
||||
tableData_meta: 'get',
|
||||
async tableData({ id, database, schemaName, pureName }) {
|
||||
const opened = await databaseConnections.ensureOpened(id, database);
|
||||
const res = opened.sendRequest({ msgtype: 'tableData', schemaName, pureName });
|
||||
return res;
|
||||
},
|
||||
};
|
||||
@@ -8,6 +8,7 @@ const useController = require('./utility/useController');
|
||||
const connections = require('./controllers/connections');
|
||||
const serverConnections = require('./controllers/serverConnections');
|
||||
const databaseConnections = require('./controllers/databaseConnections');
|
||||
const tables = require('./controllers/tables');
|
||||
const socket = require('./utility/socket');
|
||||
|
||||
const app = express();
|
||||
@@ -25,5 +26,6 @@ app.get('/', (req, res) => {
|
||||
useController(app, '/connections', connections);
|
||||
useController(app, '/server-connections', serverConnections);
|
||||
useController(app, '/database-connections', databaseConnections);
|
||||
useController(app, '/tables', tables);
|
||||
|
||||
server.listen(3000);
|
||||
|
||||
@@ -18,8 +18,15 @@ async function handleConnect(connection) {
|
||||
setInterval(handleFullRefresh, 30 * 1000);
|
||||
}
|
||||
|
||||
async function handleTableData({ msgid, schemaName, pureName }) {
|
||||
const driver = engines(storedConnection);
|
||||
const res = await driver.query(systemConnection, `SELECT TOP(100) FROM ${pureName}`);
|
||||
process.send({ msgtype: 'response', msgid, rows: res });
|
||||
}
|
||||
|
||||
const messageHandlers = {
|
||||
connect: handleConnect,
|
||||
tableData: handleTableData,
|
||||
};
|
||||
|
||||
async function handleMessage({ msgtype, ...other }) {
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { ChildProcess } from 'child_process';
|
||||
|
||||
export interface EngineDriver {
|
||||
connect({ server, port, user, password });
|
||||
query(pool, sql: string): Promise<any[]>;
|
||||
@@ -23,4 +25,5 @@ export interface OpenedDatabaseConnection {
|
||||
id: string;
|
||||
database: string;
|
||||
structure: DatabaseInfo;
|
||||
}
|
||||
subprocess: ChildProcess;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user