From c96cb08cfddf584449c6c0e45451bc523ecfe71e Mon Sep 17 00:00:00 2001 From: Jan Prochazka Date: Tue, 24 Nov 2020 20:42:02 +0100 Subject: [PATCH] backend - using engine driver from plugin --- packages/api/src/proc/connectProcess.js | 7 +++--- .../api/src/proc/databaseConnectionProcess.js | 13 +++++----- .../api/src/proc/serverConnectionProcess.js | 13 +++++----- packages/api/src/proc/sessionProcess.js | 9 ++++--- packages/api/src/shell/queryReader.js | 8 +++---- packages/api/src/shell/tableReader.js | 10 ++++---- packages/api/src/shell/tableWriter.js | 8 +++---- .../api/src/utility/requireEngineDriver.js | 24 +++++++++++++++++++ packages/types/engines.d.ts | 8 +++---- 9 files changed, 58 insertions(+), 42 deletions(-) create mode 100644 packages/api/src/utility/requireEngineDriver.js diff --git a/packages/api/src/proc/connectProcess.js b/packages/api/src/proc/connectProcess.js index 9ba05149..a4c916b2 100644 --- a/packages/api/src/proc/connectProcess.js +++ b/packages/api/src/proc/connectProcess.js @@ -1,13 +1,12 @@ -const engines = require('dbgate-engines'); -const driverConnect = require('../utility/driverConnect'); const childProcessChecker = require('../utility/childProcessChecker'); +const requireEngineDriver = require('../utility/requireEngineDriver'); function start() { childProcessChecker(); process.on('message', async (connection) => { try { - const driver = engines(connection); - const conn = await driverConnect(driver, connection); + const driver = requireEngineDriver(connection); + const conn = await driver.connect(connection); const res = await driver.getVersion(conn); process.send({ msgtype: 'connected', ...res }); } catch (e) { diff --git a/packages/api/src/proc/databaseConnectionProcess.js b/packages/api/src/proc/databaseConnectionProcess.js index 08665fcf..6611fcdc 100644 --- a/packages/api/src/proc/databaseConnectionProcess.js +++ b/packages/api/src/proc/databaseConnectionProcess.js @@ -1,7 +1,6 @@ -const engines = require('dbgate-engines'); const stableStringify = require('json-stable-stringify'); -const driverConnect = require('../utility/driverConnect'); const childProcessChecker = require('../utility/childProcessChecker'); +const requireEngineDriver = require('../utility/requireEngineDriver'); let systemConnection; let storedConnection; @@ -26,14 +25,14 @@ async function checkedAsyncCall(promise) { } async function handleFullRefresh() { - const driver = engines(storedConnection); + const driver = requireEngineDriver(storedConnection); analysedStructure = await checkedAsyncCall(driver.analyseFull(systemConnection)); process.send({ msgtype: 'structure', structure: analysedStructure }); setStatusName('ok'); } async function handleIncrementalRefresh() { - const driver = engines(storedConnection); + const driver = requireEngineDriver(storedConnection); const newStructure = await checkedAsyncCall(driver.analyseIncremental(systemConnection, analysedStructure)); if (newStructure != null) { analysedStructure = newStructure; @@ -58,8 +57,8 @@ async function handleConnect({ connection, structure }) { lastPing = new Date().getTime(); if (!structure) setStatusName('pending'); - const driver = engines(storedConnection); - systemConnection = await checkedAsyncCall(driverConnect(driver, storedConnection)); + const driver = requireEngineDriver(storedConnection); + systemConnection = await checkedAsyncCall(driver.connect(storedConnection)); if (structure) { analysedStructure = structure; handleIncrementalRefresh(); @@ -82,7 +81,7 @@ function waitConnected() { async function handleQueryData({ msgid, sql }) { await waitConnected(); - const driver = engines(storedConnection); + const driver = requireEngineDriver(storedConnection); try { const res = await driver.query(systemConnection, sql); process.send({ msgtype: 'response', msgid, ...res }); diff --git a/packages/api/src/proc/serverConnectionProcess.js b/packages/api/src/proc/serverConnectionProcess.js index 0d08b95c..225f8cdd 100644 --- a/packages/api/src/proc/serverConnectionProcess.js +++ b/packages/api/src/proc/serverConnectionProcess.js @@ -1,7 +1,6 @@ -const engines = require('dbgate-engines'); const stableStringify = require('json-stable-stringify'); -const driverConnect = require('../utility/driverConnect'); const childProcessChecker = require('../utility/childProcessChecker'); +const requireEngineDriver = require('../utility/requireEngineDriver'); let systemConnection; let storedConnection; @@ -10,7 +9,7 @@ let lastStatus = null; let lastPing = null; async function handleRefresh() { - const driver = engines(storedConnection); + const driver = requireEngineDriver(storedConnection); try { const databases = await driver.listDatabases(systemConnection); setStatusName('ok'); @@ -46,9 +45,9 @@ async function handleConnect(connection) { setStatusName('pending'); lastPing = new Date().getTime(); - const driver = engines(storedConnection); + const driver = requireEngineDriver(storedConnection); try { - systemConnection = await driverConnect(driver, storedConnection); + systemConnection = await driver.connect(storedConnection); handleRefresh(); setInterval(handleRefresh, 30 * 1000); } catch (err) { @@ -66,8 +65,8 @@ function handlePing() { } async function handleCreateDatabase({ name }) { - const driver = engines(storedConnection); - systemConnection = await driverConnect(driver, storedConnection); + const driver = requireEngineDriver(storedConnection); + systemConnection = await driver.connect(storedConnection); console.log(`RUNNING SCRIPT: CREATE DATABASE ${driver.dialect.quoteIdentifier(name)}`); await driver.query(systemConnection, `CREATE DATABASE ${driver.dialect.quoteIdentifier(name)}`); await handleRefresh(); diff --git a/packages/api/src/proc/sessionProcess.js b/packages/api/src/proc/sessionProcess.js index a24bf42c..eae01137 100644 --- a/packages/api/src/proc/sessionProcess.js +++ b/packages/api/src/proc/sessionProcess.js @@ -1,4 +1,3 @@ -const engines = require('dbgate-engines'); const uuidv1 = require('uuid/v1'); const path = require('path'); const fs = require('fs'); @@ -6,8 +5,8 @@ const _ = require('lodash'); const childProcessChecker = require('../utility/childProcessChecker'); const goSplit = require('../utility/goSplit'); -const driverConnect = require('../utility/driverConnect'); const { jsldir } = require('../utility/directories'); +const requireEngineDriver = require('../utility/requireEngineDriver'); let systemConnection; let storedConnection; @@ -119,8 +118,8 @@ class StreamHandler { async function handleConnect(connection) { storedConnection = connection; - const driver = engines(storedConnection); - systemConnection = await driverConnect(driver, storedConnection); + const driver = requireEngineDriver(storedConnection); + systemConnection = await driver.connect(storedConnection); for (const [resolve] of afterConnectCallbacks) { resolve(); } @@ -142,7 +141,7 @@ function waitConnected() { async function handleExecuteQuery({ sql }) { await waitConnected(); - const driver = engines(storedConnection); + const driver = requireEngineDriver(storedConnection); let resultIndex = 0; for (const sqlItem of goSplit(sql)) { diff --git a/packages/api/src/shell/queryReader.js b/packages/api/src/shell/queryReader.js index 12ae13be..13b2e415 100644 --- a/packages/api/src/shell/queryReader.js +++ b/packages/api/src/shell/queryReader.js @@ -1,12 +1,10 @@ -const driverConnect = require('../utility/driverConnect'); - -const engines = require('dbgate-engines'); +const requireEngineDriver = require("../utility/requireEngineDriver"); async function queryReader({ connection, sql }) { console.log(`Reading query ${sql}`); - const driver = engines(connection); - const pool = await driverConnect(driver, connection); + const driver = requireEngineDriver(connection); + const pool = await driver.connect(connection); console.log(`Connected.`); return await driver.readQuery(pool, sql); } diff --git a/packages/api/src/shell/tableReader.js b/packages/api/src/shell/tableReader.js index 172fabb0..54a09b51 100644 --- a/packages/api/src/shell/tableReader.js +++ b/packages/api/src/shell/tableReader.js @@ -1,11 +1,9 @@ const { quoteFullName } = require('dbgate-tools'); -const driverConnect = require('../utility/driverConnect'); - -const engines = require('dbgate-engines'); +const requireEngineDriver = require('../utility/requireEngineDriver'); async function tableReader({ connection, pureName, schemaName }) { - const driver = engines(connection); - const pool = await driverConnect(driver, connection); + const driver = requireEngineDriver(connection); + const pool = await driver.connect(connection); console.log(`Connected.`); const fullName = { pureName, schemaName }; @@ -14,11 +12,13 @@ async function tableReader({ connection, pureName, schemaName }) { const query = `select * from ${quoteFullName(driver.dialect, fullName)}`; if (table) { console.log(`Reading table ${table.pureName}`); + // @ts-ignore return await driver.readQuery(pool, query, table); } const view = await driver.analyseSingleObject(pool, fullName, 'views'); if (view) { console.log(`Reading view ${view.pureName}`); + // @ts-ignore return await driver.readQuery(pool, query, view); } diff --git a/packages/api/src/shell/tableWriter.js b/packages/api/src/shell/tableWriter.js index 0d7b1653..f59e3e4c 100644 --- a/packages/api/src/shell/tableWriter.js +++ b/packages/api/src/shell/tableWriter.js @@ -1,12 +1,10 @@ -const driverConnect = require('../utility/driverConnect'); - -const engines = require('dbgate-engines'); +const requireEngineDriver = require("../utility/requireEngineDriver"); async function tableWriter({ connection, schemaName, pureName, ...options }) { console.log(`Write table ${schemaName}.${pureName}`); - const driver = engines(connection); - const pool = await driverConnect(driver, connection); + const driver = requireEngineDriver(connection); + const pool = await driver.connect(connection); console.log(`Connected.`); return await driver.writeTable(pool, { schemaName, pureName }, options); } diff --git a/packages/api/src/utility/requireEngineDriver.js b/packages/api/src/utility/requireEngineDriver.js new file mode 100644 index 00000000..d263111a --- /dev/null +++ b/packages/api/src/utility/requireEngineDriver.js @@ -0,0 +1,24 @@ +const _ = require('lodash'); +const requirePlugin = require('../shell/requirePlugin'); + + +/** @returns {import('dbgate-types').EngineDriver} */ +function requireEngineDriver(connection) { + let engine = null; + if (_.isString(connection)) { + engine = connection; + } else if (_.isPlainObject(connection)) { + engine = connection.engine; + } + if (!engine) { + throw new Error('Could not get driver from connection'); + } + if (engine.includes('@')) { + const [shortName, packageName] = engine.split('@'); + const plugin = requirePlugin(packageName); + return plugin.driver; + } + throw new Error(`Could not found engine driver ${engine}`); +} + +module.exports = requireEngineDriver; diff --git a/packages/types/engines.d.ts b/packages/types/engines.d.ts index 07bdfd4b..59b2328d 100644 --- a/packages/types/engines.d.ts +++ b/packages/types/engines.d.ts @@ -7,9 +7,9 @@ import { DatabaseInfo, NamedObjectInfo, TableInfo, ViewInfo, ProcedureInfo, Func export interface StreamOptions { recordset: (columns) => void; row: (row) => void; - error: (error) => void; - done: (result) => void; - info: (info) => void; + error?: (error) => void; + done?: (result) => void; + info?: (info) => void; } export interface WriteTableOptions { @@ -21,7 +21,7 @@ export interface WriteTableOptions { export interface EngineDriver { engine: string; title: string; - connect(nativeModules, { server, port, user, password, database }): any; + connect({ server, port, user, password, database }): any; query(pool: any, sql: string): Promise; stream(pool: any, sql: string, options: StreamOptions); readQuery(pool: any, sql: string, structure?: TableInfo): Promise;