From ffef2499cc1ee45d03138b7ada91304ca38d6da2 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Fri, 12 May 2023 12:19:05 +0200 Subject: [PATCH] Implement the check as part of the integration --- packages/server/src/integrations/index.ts | 14 +++++------ packages/server/src/integrations/postgres.ts | 24 +++++++++---------- .../sdk/tests/datasources/validators.spec.ts | 11 ++++----- packages/types/src/sdk/datasources.ts | 6 +++++ 4 files changed, 28 insertions(+), 27 deletions(-) diff --git a/packages/server/src/integrations/index.ts b/packages/server/src/integrations/index.ts index b78955f38d..90dd7cfcd6 100644 --- a/packages/server/src/integrations/index.ts +++ b/packages/server/src/integrations/index.ts @@ -20,7 +20,7 @@ import env from "../environment" import { cloneDeep } from "lodash" import sdk from "../sdk" -const DEFINITIONS: Record = { +const DEFINITIONS: Record = { [SourceName.POSTGRES]: postgres.schema, [SourceName.DYNAMODB]: dynamodb.schema, [SourceName.MONGODB]: mongodb.schema, @@ -36,6 +36,7 @@ const DEFINITIONS: Record = { [SourceName.GOOGLE_SHEETS]: googlesheets.schema, [SourceName.REDIS]: redis.schema, [SourceName.SNOWFLAKE]: snowflake.schema, + [SourceName.ORACLE]: undefined, } const INTEGRATIONS: Record = { @@ -68,7 +69,9 @@ if ( INTEGRATIONS[SourceName.ORACLE] = oracle.integration } -export async function getDefinition(source: SourceName): Promise { +export async function getDefinition( + source: SourceName +): Promise { // check if its integrated, faster const definition = DEFINITIONS[source] if (definition) { @@ -109,7 +112,7 @@ export async function getIntegration(integration: SourceName) { for (let plugin of plugins) { if (plugin.name === integration) { // need to use commonJS require due to its dynamic runtime nature - const retrieved: any = await getDatasourcePlugin(plugin) + const retrieved = await getDatasourcePlugin(plugin) if (retrieved.integration) { return retrieved.integration } else { @@ -121,12 +124,7 @@ export async function getIntegration(integration: SourceName) { throw new Error("No datasource implementation found.") } -const VALIDATORS = { - [SourceName.POSTGRES]: postgres.validateConnection, -} - export default { getDefinitions, getIntegration, - getValidator: VALIDATORS, } diff --git a/packages/server/src/integrations/postgres.ts b/packages/server/src/integrations/postgres.ts index da678854b4..f6a8f22441 100644 --- a/packages/server/src/integrations/postgres.ts +++ b/packages/server/src/integrations/postgres.ts @@ -150,6 +150,17 @@ class PostgresIntegration extends Sql implements DatasourcePlus { this.open = false } + async testConnection() { + try { + await this.openConnection() + return true + } catch (e: any) { + return { error: e.message as string } + } finally { + await this.closeConnection() + } + } + getBindingIdentifier(): string { return `$${this.index++}` } @@ -330,20 +341,7 @@ class PostgresIntegration extends Sql implements DatasourcePlus { } } -async function validateConnection(config: PostgresConfig) { - const integration = new PostgresIntegration(config) - try { - await integration.openConnection() - return true - } catch (e: any) { - return { error: e.message as string } - } finally { - await integration.closeConnection() - } -} - export default { schema: SCHEMA, integration: PostgresIntegration, - validateConnection, } diff --git a/packages/server/src/sdk/tests/datasources/validators.spec.ts b/packages/server/src/sdk/tests/datasources/validators.spec.ts index 9664c6c8fc..b1b4d83fc8 100644 --- a/packages/server/src/sdk/tests/datasources/validators.spec.ts +++ b/packages/server/src/sdk/tests/datasources/validators.spec.ts @@ -1,13 +1,10 @@ -import { SourceName } from "@budibase/types" -import integrations from "../../../integrations" +import postgres from "../../../integrations/postgres" import { GenericContainer } from "testcontainers" jest.unmock("pg") describe("datasource validators", () => { describe("postgres", () => { - const validator = integrations.getValidator[SourceName.POSTGRES]! - let host: string let port: number @@ -22,7 +19,7 @@ describe("datasource validators", () => { }) it("test valid connection string", async () => { - const result = await validator({ + const integration = new postgres.integration({ host, port, database: "postgres", @@ -32,11 +29,12 @@ describe("datasource validators", () => { ssl: false, rejectUnauthorized: false, }) + const result = await integration.testConnection() expect(result).toBeTruthy() }) it("test invalid connection string", async () => { - const result = await validator({ + const integration = new postgres.integration({ host, port, database: "postgres", @@ -46,6 +44,7 @@ describe("datasource validators", () => { ssl: false, rejectUnauthorized: false, }) + const result = await integration.testConnection() expect(result).toEqual({ error: 'password authentication failed for user "wrong"', }) diff --git a/packages/types/src/sdk/datasources.ts b/packages/types/src/sdk/datasources.ts index 938f4c9340..536aaa1fb1 100644 --- a/packages/types/src/sdk/datasources.ts +++ b/packages/types/src/sdk/datasources.ts @@ -128,6 +128,12 @@ export interface IntegrationBase { read?(query: any): Promise update?(query: any): Promise delete?(query: any): Promise + testConnection?(): Promise< + | true + | { + error: string + } + > } export interface DatasourcePlus extends IntegrationBase {