Implement the check as part of the integration

This commit is contained in:
Adria Navarro 2023-05-12 12:19:05 +02:00
parent f5fb4f8850
commit ffef2499cc
4 changed files with 28 additions and 27 deletions

View File

@ -20,7 +20,7 @@ import env from "../environment"
import { cloneDeep } from "lodash"
import sdk from "../sdk"
const DEFINITIONS: Record<string, Integration> = {
const DEFINITIONS: Record<SourceName, Integration | undefined> = {
[SourceName.POSTGRES]: postgres.schema,
[SourceName.DYNAMODB]: dynamodb.schema,
[SourceName.MONGODB]: mongodb.schema,
@ -36,6 +36,7 @@ const DEFINITIONS: Record<string, Integration> = {
[SourceName.GOOGLE_SHEETS]: googlesheets.schema,
[SourceName.REDIS]: redis.schema,
[SourceName.SNOWFLAKE]: snowflake.schema,
[SourceName.ORACLE]: undefined,
}
const INTEGRATIONS: Record<SourceName, any> = {
@ -68,7 +69,9 @@ if (
INTEGRATIONS[SourceName.ORACLE] = oracle.integration
}
export async function getDefinition(source: SourceName): Promise<Integration> {
export async function getDefinition(
source: SourceName
): Promise<Integration | undefined> {
// 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,
}

View File

@ -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,
}

View File

@ -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"',
})

View File

@ -128,6 +128,12 @@ export interface IntegrationBase {
read?(query: any): Promise<any[] | any>
update?(query: any): Promise<any[] | any>
delete?(query: any): Promise<any[] | any>
testConnection?(): Promise<
| true
| {
error: string
}
>
}
export interface DatasourcePlus extends IntegrationBase {