mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-26 12:06:00 +00:00
database connections
This commit is contained in:
@@ -7,6 +7,7 @@ const DatabaseAnalyser = require('@dbgate/engines/default/DatabaseAnalyser');
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
/** @type {import('@dbgate/types').OpenedDatabaseConnection[]} */
|
/** @type {import('@dbgate/types').OpenedDatabaseConnection[]} */
|
||||||
opened: [],
|
opened: [],
|
||||||
|
closed: [],
|
||||||
requests: {},
|
requests: {},
|
||||||
|
|
||||||
handle_structure(conid, database, { structure }) {
|
handle_structure(conid, database, { structure }) {
|
||||||
@@ -24,6 +25,14 @@ module.exports = {
|
|||||||
resolve(response);
|
resolve(response);
|
||||||
delete this.requests[msgid];
|
delete this.requests[msgid];
|
||||||
},
|
},
|
||||||
|
handle_status(conid, database, { status }) {
|
||||||
|
const existing = this.opened.find((x) => x.conid == conid && x.database == database);
|
||||||
|
if (!existing) return;
|
||||||
|
existing.status = status;
|
||||||
|
socket.emitChanged(`database-status-changed-${conid}-${database}`);
|
||||||
|
socket.emitChanged(`database-structure-changed-${conid}-${database}`);
|
||||||
|
},
|
||||||
|
|
||||||
handle_ping() {},
|
handle_ping() {},
|
||||||
|
|
||||||
async ensureOpened(conid, database) {
|
async ensureOpened(conid, database) {
|
||||||
@@ -31,19 +40,31 @@ module.exports = {
|
|||||||
if (existing) return existing;
|
if (existing) return existing;
|
||||||
const connection = await connections.get({ conid });
|
const connection = await connections.get({ conid });
|
||||||
const subprocess = fork(process.argv[1], ['databaseConnectionProcess']);
|
const subprocess = fork(process.argv[1], ['databaseConnectionProcess']);
|
||||||
|
const lastClosed = this.closed.find((x) => x.conid == conid && x.database == database);
|
||||||
const newOpened = {
|
const newOpened = {
|
||||||
conid,
|
conid,
|
||||||
database,
|
database,
|
||||||
subprocess,
|
subprocess,
|
||||||
structure: DatabaseAnalyser.createEmptyStructure(),
|
structure: lastClosed ? lastClosed.structure : DatabaseAnalyser.createEmptyStructure(),
|
||||||
connection,
|
connection,
|
||||||
|
status: { name: 'pending' },
|
||||||
};
|
};
|
||||||
this.opened.push(newOpened);
|
this.opened.push(newOpened);
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
subprocess.on('message', ({ msgtype, ...message }) => {
|
subprocess.on('message', ({ msgtype, ...message }) => {
|
||||||
|
if (newOpened.disconnected) return;
|
||||||
this[`handle_${msgtype}`](conid, database, message);
|
this[`handle_${msgtype}`](conid, database, message);
|
||||||
});
|
});
|
||||||
subprocess.send({ msgtype: 'connect', ...connection, database });
|
subprocess.on('exit', () => {
|
||||||
|
if (newOpened.disconnected) return;
|
||||||
|
this.close(conid, database, false);
|
||||||
|
});
|
||||||
|
|
||||||
|
subprocess.send({
|
||||||
|
msgtype: 'connect',
|
||||||
|
connection: { ...connection, database },
|
||||||
|
structure: lastClosed ? lastClosed.structure : null,
|
||||||
|
});
|
||||||
return newOpened;
|
return newOpened;
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -65,6 +86,41 @@ module.exports = {
|
|||||||
return res;
|
return res;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
ping_meta: 'post',
|
||||||
|
async ping({ conid, database }) {
|
||||||
|
const existing = this.opened.find((x) => x.conid == conid && x.database == database);
|
||||||
|
if (existing) {
|
||||||
|
existing.subprocess.send({ msgtype: 'ping' });
|
||||||
|
}
|
||||||
|
return { status: 'ok' };
|
||||||
|
},
|
||||||
|
|
||||||
|
refresh_meta: 'post',
|
||||||
|
async refresh({ conid, database }) {
|
||||||
|
this.close(conid, database);
|
||||||
|
|
||||||
|
await this.ensureOpened(conid, database);
|
||||||
|
return { status: 'ok' };
|
||||||
|
},
|
||||||
|
|
||||||
|
close(conid, database, kill = true) {
|
||||||
|
const existing = this.opened.find((x) => x.conid == conid && x.database == database);
|
||||||
|
if (existing) {
|
||||||
|
existing.disconnected = true;
|
||||||
|
if (kill) existing.subprocess.kill();
|
||||||
|
this.opened = this.opened.filter((x) => x.conid != conid || x.database != database);
|
||||||
|
this.closed[conid] = {
|
||||||
|
status: {
|
||||||
|
...existing.status,
|
||||||
|
name: 'error',
|
||||||
|
},
|
||||||
|
structure: existing.structure,
|
||||||
|
};
|
||||||
|
socket.emitChanged(`database-status-changed-${conid}-${database}`);
|
||||||
|
socket.emitChanged(`database-structure-changed-${conid}-${database}`);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
// runCommand_meta: 'post',
|
// runCommand_meta: 'post',
|
||||||
// async runCommand({ conid, database, sql }) {
|
// async runCommand({ conid, database, sql }) {
|
||||||
// console.log(`Running SQL command , conid=${conid}, database=${database}, sql=${sql}`);
|
// console.log(`Running SQL command , conid=${conid}, database=${database}, sql=${sql}`);
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ module.exports = {
|
|||||||
(res, type) => ({
|
(res, type) => ({
|
||||||
...res,
|
...res,
|
||||||
[type]: pickObjectNames(opened.structure[type]),
|
[type]: pickObjectNames(opened.structure[type]),
|
||||||
|
status: opened.status,
|
||||||
}),
|
}),
|
||||||
{}
|
{}
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -60,10 +60,9 @@ module.exports = {
|
|||||||
if (existing) {
|
if (existing) {
|
||||||
existing.disconnected = true;
|
existing.disconnected = true;
|
||||||
if (kill) existing.subprocess.kill();
|
if (kill) existing.subprocess.kill();
|
||||||
const last = this.opened.find((x) => x.conid == conid);
|
|
||||||
this.opened = this.opened.filter((x) => x.conid != conid);
|
this.opened = this.opened.filter((x) => x.conid != conid);
|
||||||
this.closed[conid] = {
|
this.closed[conid] = {
|
||||||
...(last && last.status),
|
...existing.status,
|
||||||
name: 'error',
|
name: 'error',
|
||||||
};
|
};
|
||||||
socket.emitChanged(`server-status-changed`);
|
socket.emitChanged(`server-status-changed`);
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
const engines = require('@dbgate/engines');
|
const engines = require('@dbgate/engines');
|
||||||
|
const stableStringify = require('json-stable-stringify');
|
||||||
const driverConnect = require('../utility/driverConnect');
|
const driverConnect = require('../utility/driverConnect');
|
||||||
const childProcessChecker = require('../utility/childProcessChecker');
|
const childProcessChecker = require('../utility/childProcessChecker');
|
||||||
|
|
||||||
@@ -6,11 +7,14 @@ let systemConnection;
|
|||||||
let storedConnection;
|
let storedConnection;
|
||||||
let afterConnectCallbacks = [];
|
let afterConnectCallbacks = [];
|
||||||
let analysedStructure = null;
|
let analysedStructure = null;
|
||||||
|
let lastPing = null;
|
||||||
|
let lastStatus = null;
|
||||||
|
|
||||||
async function handleFullRefresh() {
|
async function handleFullRefresh() {
|
||||||
const driver = engines(storedConnection);
|
const driver = engines(storedConnection);
|
||||||
analysedStructure = await driver.analyseFull(systemConnection);
|
analysedStructure = await driver.analyseFull(systemConnection);
|
||||||
process.send({ msgtype: 'structure', structure: analysedStructure });
|
process.send({ msgtype: 'structure', structure: analysedStructure });
|
||||||
|
setStatusName('ok');
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleIncrementalRefresh() {
|
async function handleIncrementalRefresh() {
|
||||||
@@ -22,9 +26,23 @@ async function handleIncrementalRefresh() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleConnect(connection) {
|
function setStatus(status) {
|
||||||
storedConnection = connection;
|
const statusString = stableStringify(status);
|
||||||
|
if (lastStatus != statusString) {
|
||||||
|
process.send({ msgtype: 'status', status });
|
||||||
|
lastStatus = statusString;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function setStatusName(name) {
|
||||||
|
setStatus({ name });
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleConnect({ connection, structure }) {
|
||||||
|
storedConnection = connection;
|
||||||
|
lastPing = new Date().getTime();
|
||||||
|
|
||||||
|
setStatusName('pending');
|
||||||
const driver = engines(storedConnection);
|
const driver = engines(storedConnection);
|
||||||
systemConnection = await driverConnect(driver, storedConnection);
|
systemConnection = await driverConnect(driver, storedConnection);
|
||||||
handleFullRefresh();
|
handleFullRefresh();
|
||||||
@@ -56,9 +74,14 @@ async function handleQueryData({ msgid, sql }) {
|
|||||||
// process.send({ msgtype: 'response', msgid, ...res });
|
// process.send({ msgtype: 'response', msgid, ...res });
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
function handlePing() {
|
||||||
|
lastPing = new Date().getTime();
|
||||||
|
}
|
||||||
|
|
||||||
const messageHandlers = {
|
const messageHandlers = {
|
||||||
connect: handleConnect,
|
connect: handleConnect,
|
||||||
queryData: handleQueryData,
|
queryData: handleQueryData,
|
||||||
|
ping: handlePing,
|
||||||
// runCommand: handleRunCommand,
|
// runCommand: handleRunCommand,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -69,6 +92,14 @@ async function handleMessage({ msgtype, ...other }) {
|
|||||||
|
|
||||||
function start() {
|
function start() {
|
||||||
childProcessChecker();
|
childProcessChecker();
|
||||||
|
|
||||||
|
setInterval(() => {
|
||||||
|
const time = new Date().getTime();
|
||||||
|
if (time - lastPing > 60 * 1000) {
|
||||||
|
process.exit(0);
|
||||||
|
}
|
||||||
|
}, 60 * 1000);
|
||||||
|
|
||||||
process.on('message', async (message) => {
|
process.on('message', async (message) => {
|
||||||
try {
|
try {
|
||||||
await handleMessage(message);
|
await handleMessage(message);
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
const engines = require('@dbgate/engines');
|
const engines = require('@dbgate/engines');
|
||||||
|
const stableStringify = require('json-stable-stringify');
|
||||||
const driverConnect = require('../utility/driverConnect');
|
const driverConnect = require('../utility/driverConnect');
|
||||||
const childProcessChecker = require('../utility/childProcessChecker');
|
const childProcessChecker = require('../utility/childProcessChecker');
|
||||||
const stableStringify = require('json-stable-stringify');
|
|
||||||
|
|
||||||
let systemConnection;
|
let systemConnection;
|
||||||
let storedConnection;
|
let storedConnection;
|
||||||
|
|||||||
21
packages/types/index.d.ts
vendored
21
packages/types/index.d.ts
vendored
@@ -1,10 +1,15 @@
|
|||||||
import { ChildProcess } from "child_process";
|
import { ChildProcess } from 'child_process';
|
||||||
import { DatabaseInfo } from "./dbinfo";
|
import { DatabaseInfo } from './dbinfo';
|
||||||
export interface OpenedDatabaseConnection {
|
export interface OpenedDatabaseConnection {
|
||||||
conid: string;
|
conid: string;
|
||||||
database: string;
|
database: string;
|
||||||
structure: DatabaseInfo;
|
structure: DatabaseInfo;
|
||||||
subprocess: ChildProcess;
|
subprocess: ChildProcess;
|
||||||
|
disconnected?: boolean;
|
||||||
|
status?: {
|
||||||
|
name: string;
|
||||||
|
message?: string;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface OpenedSession {
|
export interface OpenedSession {
|
||||||
@@ -23,9 +28,9 @@ export interface StoredConnection {
|
|||||||
displayName: string;
|
displayName: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export * from "./engines";
|
export * from './engines';
|
||||||
export * from "./dbinfo";
|
export * from './dbinfo';
|
||||||
export * from "./query";
|
export * from './query';
|
||||||
export * from "./dialect";
|
export * from './dialect';
|
||||||
export * from "./dumper";
|
export * from './dumper';
|
||||||
export * from "./dbtypes";
|
export * from './dbtypes';
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import {
|
|||||||
OpenedConnectionsProvider,
|
OpenedConnectionsProvider,
|
||||||
} from './utility/globalState';
|
} from './utility/globalState';
|
||||||
import { SocketProvider } from './utility/SocketProvider';
|
import { SocketProvider } from './utility/SocketProvider';
|
||||||
import OpenedConnectionsPinger from './utility/OpnedConnectionsPinger';
|
import ConnectionsPinger from './utility/ConnectionsPinger';
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
return (
|
return (
|
||||||
@@ -19,9 +19,9 @@ function App() {
|
|||||||
<OpenedTabsProvider>
|
<OpenedTabsProvider>
|
||||||
<SavedSqlFilesProvider>
|
<SavedSqlFilesProvider>
|
||||||
<OpenedConnectionsProvider>
|
<OpenedConnectionsProvider>
|
||||||
<OpenedConnectionsPinger>
|
<ConnectionsPinger>
|
||||||
<Screen />
|
<Screen />
|
||||||
</OpenedConnectionsPinger>
|
</ConnectionsPinger>
|
||||||
</OpenedConnectionsProvider>
|
</OpenedConnectionsProvider>
|
||||||
</SavedSqlFilesProvider>
|
</SavedSqlFilesProvider>
|
||||||
</OpenedTabsProvider>
|
</OpenedTabsProvider>
|
||||||
|
|||||||
22
packages/web/src/utility/ConnectionsPinger.js
Normal file
22
packages/web/src/utility/ConnectionsPinger.js
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import _ from 'lodash';
|
||||||
|
import { useOpenedConnections, useCurrentDatabase } from './globalState';
|
||||||
|
import axios from './axios';
|
||||||
|
|
||||||
|
export default function ConnectionsPinger({ children }) {
|
||||||
|
const openedConnections = useOpenedConnections();
|
||||||
|
const currentDatabase = useCurrentDatabase();
|
||||||
|
React.useEffect(() => {
|
||||||
|
const handle = window.setInterval(() => {
|
||||||
|
axios.post('server-connections/ping', { connections: openedConnections });
|
||||||
|
|
||||||
|
const database = _.get(currentDatabase, 'name');
|
||||||
|
const conid = _.get(currentDatabase, 'connection._id');
|
||||||
|
if (conid && database) {
|
||||||
|
axios.post('database-connections/ping', { conid });
|
||||||
|
}
|
||||||
|
}, 30 * 1000);
|
||||||
|
return () => window.clearInterval(handle);
|
||||||
|
}, [openedConnections, currentDatabase]);
|
||||||
|
return children;
|
||||||
|
}
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
import React from 'react';
|
|
||||||
import { useOpenedConnections } from './globalState';
|
|
||||||
import axios from './axios';
|
|
||||||
|
|
||||||
export default function OpenedConnectionsPinger({ children }) {
|
|
||||||
const openedConnections = useOpenedConnections();
|
|
||||||
React.useEffect(() => {
|
|
||||||
const handle = window.setInterval(() => {
|
|
||||||
axios.post('server-connections/ping', { connections: openedConnections });
|
|
||||||
}, 30 * 1000);
|
|
||||||
return () => window.clearInterval(handle);
|
|
||||||
}, [openedConnections]);
|
|
||||||
return children;
|
|
||||||
}
|
|
||||||
@@ -10,6 +10,7 @@ import databaseObjectAppObject from '../appobj/databaseObjectAppObject';
|
|||||||
import { useSqlObjectList, useDatabaseList, useConnectionList, useServerStatus } from '../utility/metadataLoaders';
|
import { useSqlObjectList, useDatabaseList, useConnectionList, useServerStatus } from '../utility/metadataLoaders';
|
||||||
import { SearchBoxWrapper, InnerContainer, Input, MainContainer, OuterContainer, WidgetTitle } from './WidgetStyles';
|
import { SearchBoxWrapper, InnerContainer, Input, MainContainer, OuterContainer, WidgetTitle } from './WidgetStyles';
|
||||||
import axios from '../utility/axios';
|
import axios from '../utility/axios';
|
||||||
|
import LoadingInfo from './LoadingInfo';
|
||||||
|
|
||||||
function SubDatabaseList({ data }) {
|
function SubDatabaseList({ data }) {
|
||||||
const setDb = useSetCurrentDatabase();
|
const setDb = useSetCurrentDatabase();
|
||||||
@@ -68,6 +69,11 @@ function ConnectionList() {
|
|||||||
|
|
||||||
function SqlObjectList({ conid, database }) {
|
function SqlObjectList({ conid, database }) {
|
||||||
const objects = useSqlObjectList({ conid, database });
|
const objects = useSqlObjectList({ conid, database });
|
||||||
|
const { status } = objects || {};
|
||||||
|
|
||||||
|
const handleRefreshDatabase = () => {
|
||||||
|
axios.post('database-connections/refresh', { conid, database });
|
||||||
|
};
|
||||||
|
|
||||||
const [filter, setFilter] = React.useState('');
|
const [filter, setFilter] = React.useState('');
|
||||||
const objectList = _.flatten(
|
const objectList = _.flatten(
|
||||||
@@ -85,15 +91,19 @@ function SqlObjectList({ conid, database }) {
|
|||||||
value={filter}
|
value={filter}
|
||||||
onChange={(e) => setFilter(e.target.value)}
|
onChange={(e) => setFilter(e.target.value)}
|
||||||
/>
|
/>
|
||||||
<InlineButton>Refresh</InlineButton>
|
<InlineButton onClick={handleRefreshDatabase}>Refresh</InlineButton>
|
||||||
</SearchBoxWrapper>
|
</SearchBoxWrapper>
|
||||||
<InnerContainer>
|
<InnerContainer>
|
||||||
|
{status && status.name == 'pending' ? (
|
||||||
|
<LoadingInfo message="Loading database structure" />
|
||||||
|
) : (
|
||||||
<AppObjectList
|
<AppObjectList
|
||||||
list={objectList.map((x) => ({ ...x, conid, database }))}
|
list={objectList.map((x) => ({ ...x, conid, database }))}
|
||||||
makeAppObj={databaseObjectAppObject()}
|
makeAppObj={databaseObjectAppObject()}
|
||||||
groupFunc={(appobj) => appobj.groupTitle}
|
groupFunc={(appobj) => appobj.groupTitle}
|
||||||
filter={filter}
|
filter={filter}
|
||||||
/>
|
/>
|
||||||
|
)}
|
||||||
</InnerContainer>
|
</InnerContainer>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|||||||
24
packages/web/src/widgets/LoadingInfo.js
Normal file
24
packages/web/src/widgets/LoadingInfo.js
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
import React from 'react';
|
||||||
|
|
||||||
|
import styled from 'styled-components';
|
||||||
|
|
||||||
|
const Container = styled.div`
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
`;
|
||||||
|
|
||||||
|
const Spinner = styled.div`
|
||||||
|
font-size: 20pt;
|
||||||
|
margin: 10px;
|
||||||
|
`;
|
||||||
|
|
||||||
|
export default function LoadingInfo({ message }) {
|
||||||
|
return (
|
||||||
|
<Container>
|
||||||
|
<Spinner>
|
||||||
|
<i className="fas fa-spinner fa-spin" />
|
||||||
|
</Spinner>
|
||||||
|
{message}
|
||||||
|
</Container>
|
||||||
|
);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user