From f0d312fb59fd98d6e4501bc407959b91eb0650f2 Mon Sep 17 00:00:00 2001 From: Fabio Di Stasio Date: Fri, 8 Nov 2024 18:09:37 +0100 Subject: [PATCH] perf(PostgreSQL): improved support of connection strings, closes #893 --- package-lock.json | 8 --- package.json | 1 - src/common/interfaces/antares.ts | 1 + src/main/ipc-handlers/connection.ts | 2 + src/main/libs/clients/PostgreSQLClient.ts | 1 + .../WorkspaceAddConnectionPanel.vue | 6 +-- .../WorkspaceEditConnectionPanel.vue | 4 +- src/renderer/ipc-api/Connection.ts | 11 ++-- src/renderer/libs/connStringDecode.ts | 50 ------------------- src/renderer/stores/workspaces.ts | 4 +- 10 files changed, 15 insertions(+), 73 deletions(-) delete mode 100644 src/renderer/libs/connStringDecode.ts diff --git a/package-lock.json b/package-lock.json index 2b4874a7..8fa819a3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -42,7 +42,6 @@ "node-firebird": "~1.1.8", "node-loader": "~2.0.0", "pg": "~8.11.5", - "pg-connection-string": "~2.5.0", "pg-query-stream": "~4.2.3", "pgsql-ast-parser": "~7.2.1", "pinia": "~2.1.7", @@ -12142,10 +12141,6 @@ "license": "MIT", "optional": true }, - "node_modules/pg-connection-string": { - "version": "2.5.0", - "license": "MIT" - }, "node_modules/pg-cursor": { "version": "2.10.3", "license": "MIT", @@ -24507,9 +24502,6 @@ "version": "1.1.1", "optional": true }, - "pg-connection-string": { - "version": "2.5.0" - }, "pg-cursor": { "version": "2.10.3", "requires": {} diff --git a/package.json b/package.json index ee8834e3..27efd119 100644 --- a/package.json +++ b/package.json @@ -151,7 +151,6 @@ "node-firebird": "~1.1.8", "node-loader": "~2.0.0", "pg": "~8.11.5", - "pg-connection-string": "~2.5.0", "pg-query-stream": "~4.2.3", "pgsql-ast-parser": "~7.2.1", "pinia": "~2.1.7", diff --git a/src/common/interfaces/antares.ts b/src/common/interfaces/antares.ts index 5a79b647..ab17e709 100644 --- a/src/common/interfaces/antares.ts +++ b/src/common/interfaces/antares.ts @@ -57,6 +57,7 @@ export interface ConnectionParams { cert?: string; key?: string; ca?: string; + connString?: string; untrustedConnection: boolean; ciphers?: string; ssh: boolean; diff --git a/src/main/ipc-handlers/connection.ts b/src/main/ipc-handlers/connection.ts index 6dcf7cc5..6f564b86 100644 --- a/src/main/ipc-handlers/connection.ts +++ b/src/main/ipc-handlers/connection.ts @@ -26,6 +26,7 @@ export default (connections: Record) => { user: conn.user, password: conn.password, readonly: conn.readonly, + connectionString: conn.connString, database: '', schema: '', databasePath: '', @@ -122,6 +123,7 @@ export default (connections: Record) => { password: conn.password, application_name: 'Antares SQL', readonly: conn.readonly, + connectionString: conn.connString, database: '', schema: '', databasePath: '', diff --git a/src/main/libs/clients/PostgreSQLClient.ts b/src/main/libs/clients/PostgreSQLClient.ts index 4c83256a..9c7d86d5 100644 --- a/src/main/libs/clients/PostgreSQLClient.ts +++ b/src/main/libs/clients/PostgreSQLClient.ts @@ -155,6 +155,7 @@ export class PostgreSQLClient extends BaseClient { host: this._params.host, port: this._params.port, user: this._params.user, + connectionString: this._params.connectionString, database: 'postgres' as string, password: this._params.password, ssl: null as ConnectionOptions diff --git a/src/renderer/components/WorkspaceAddConnectionPanel.vue b/src/renderer/components/WorkspaceAddConnectionPanel.vue index 6cde6a28..1ed776f6 100644 --- a/src/renderer/components/WorkspaceAddConnectionPanel.vue +++ b/src/renderer/components/WorkspaceAddConnectionPanel.vue @@ -67,7 +67,7 @@
@@ -502,8 +502,8 @@ const connection = ref({ sshKey: '', sshPort: 22, sshKeepAliveInterval: 1800, - pgConnString: '' -}) as Ref; + connString: '' +}) as Ref; const firstInput: Ref = ref(null); const isConnecting = ref(false); diff --git a/src/renderer/components/WorkspaceEditConnectionPanel.vue b/src/renderer/components/WorkspaceEditConnectionPanel.vue index 0b5bb294..2f3fa061 100644 --- a/src/renderer/components/WorkspaceEditConnectionPanel.vue +++ b/src/renderer/components/WorkspaceEditConnectionPanel.vue @@ -68,7 +68,7 @@
@@ -502,7 +502,7 @@ const clients = [ ]; const firstInput: Ref = ref(null); -const localConnection: Ref = ref(null); +const localConnection: Ref = ref(null); const isConnecting = ref(false); const isTesting = ref(false); const isAsking = ref(false); diff --git a/src/renderer/ipc-api/Connection.ts b/src/renderer/ipc-api/Connection.ts index cf5a1fa0..538ebef4 100644 --- a/src/renderer/ipc-api/Connection.ts +++ b/src/renderer/ipc-api/Connection.ts @@ -1,18 +1,15 @@ import { ConnectionParams, IpcResponse } from 'common/interfaces/antares'; import { ipcRenderer } from 'electron'; -import connStringConstruct from '../libs/connStringDecode'; import { unproxify } from '../libs/unproxify'; export default class { - static makeTest (params: ConnectionParams & { pgConnString?: string }): Promise { - const newParams = connStringConstruct(params) as ConnectionParams; - return ipcRenderer.invoke('test-connection', unproxify(newParams)); + static makeTest (params: ConnectionParams & { connString?: string }): Promise { + return ipcRenderer.invoke('test-connection', unproxify(params)); } - static connect (params: ConnectionParams & { pgConnString?: string }): Promise { - const newParams = connStringConstruct(params) as ConnectionParams; - return ipcRenderer.invoke('connect', unproxify(newParams)); + static connect (params: ConnectionParams & { connString?: string }): Promise { + return ipcRenderer.invoke('connect', unproxify(params)); } static abortConnection (uid: string): void { diff --git a/src/renderer/libs/connStringDecode.ts b/src/renderer/libs/connStringDecode.ts deleted file mode 100644 index d8c40467..00000000 --- a/src/renderer/libs/connStringDecode.ts +++ /dev/null @@ -1,50 +0,0 @@ -import { ConnectionParams } from 'common/interfaces/antares'; -import * as formatter from 'pg-connection-string'; // parses a connection string - -const formatHost = (host: string) => { - const results = host === 'localhost' ? '127.0.0.1' : host; - return results; -}; - -const checkForSSl = (conn: string) => { - return conn.includes('ssl=true'); -}; - -const connStringConstruct = (args: ConnectionParams & { pgConnString?: string }): ConnectionParams => { - if (!args.pgConnString) - return args; - - if (typeof args.pgConnString !== 'string') - return args; - - // eslint-disable-next-line @typescript-eslint/no-explicit-any - const stringArgs: any = formatter.parse(args.pgConnString); - - const client = args.client || 'pg'; - - args.client = client; - args.host = formatHost(stringArgs.host); - args.database = stringArgs.database; - args.port = stringArgs.port || '5432'; - args.user = stringArgs.user; - args.password = stringArgs.password; - - // ssh - args.ssh = stringArgs.ssh || args.ssh; - args.sshHost = stringArgs.sshHost; - args.sshUser = stringArgs.sshUser; - args.sshPass = stringArgs.sshPass; - args.sshKey = stringArgs.sshKey; - args.sshPort = stringArgs.sshPort; - - // ssl mode - args.ssl = checkForSSl(args.pgConnString) || args.ssl; - args.cert = stringArgs.sslcert; - args.key = stringArgs.sslkey; - args.ca = stringArgs.sslrootcert; - args.ciphers = stringArgs.ciphers; - - return args; -}; - -export default connStringConstruct; diff --git a/src/renderer/stores/workspaces.ts b/src/renderer/stores/workspaces.ts index 1d599216..5314b65c 100644 --- a/src/renderer/stores/workspaces.ts +++ b/src/renderer/stores/workspaces.ts @@ -147,7 +147,7 @@ export const useWorkspacesStore = defineStore('workspaces', { else this.selectedWorkspace = uid; }, - async connectWorkspace (connection: ConnectionParams & { pgConnString?: string }, args?: {mode?: string; signal?: AbortSignal}) { + async connectWorkspace (connection: ConnectionParams & { connString?: string }, args?: {mode?: string; signal?: AbortSignal}) { this.workspaces = (this.workspaces as Workspace[]).map(workspace => workspace.uid === connection.uid ? { ...workspace, @@ -427,7 +427,7 @@ export const useWorkspacesStore = defineStore('workspaces', { this.selectTab({ uid, tab: 0 }); }, - async switchConnection (connection: ConnectionParams & { pgConnString?: string }) { + async switchConnection (connection: ConnectionParams & { connString?: string }) { await Connection.disconnect(connection.uid); return this.connectWorkspace(connection, { mode: 'switch' }); },