Implement the check as part of the integration
This commit is contained in:
parent
f5fb4f8850
commit
ffef2499cc
|
@ -20,7 +20,7 @@ import env from "../environment"
|
||||||
import { cloneDeep } from "lodash"
|
import { cloneDeep } from "lodash"
|
||||||
import sdk from "../sdk"
|
import sdk from "../sdk"
|
||||||
|
|
||||||
const DEFINITIONS: Record<string, Integration> = {
|
const DEFINITIONS: Record<SourceName, Integration | undefined> = {
|
||||||
[SourceName.POSTGRES]: postgres.schema,
|
[SourceName.POSTGRES]: postgres.schema,
|
||||||
[SourceName.DYNAMODB]: dynamodb.schema,
|
[SourceName.DYNAMODB]: dynamodb.schema,
|
||||||
[SourceName.MONGODB]: mongodb.schema,
|
[SourceName.MONGODB]: mongodb.schema,
|
||||||
|
@ -36,6 +36,7 @@ const DEFINITIONS: Record<string, Integration> = {
|
||||||
[SourceName.GOOGLE_SHEETS]: googlesheets.schema,
|
[SourceName.GOOGLE_SHEETS]: googlesheets.schema,
|
||||||
[SourceName.REDIS]: redis.schema,
|
[SourceName.REDIS]: redis.schema,
|
||||||
[SourceName.SNOWFLAKE]: snowflake.schema,
|
[SourceName.SNOWFLAKE]: snowflake.schema,
|
||||||
|
[SourceName.ORACLE]: undefined,
|
||||||
}
|
}
|
||||||
|
|
||||||
const INTEGRATIONS: Record<SourceName, any> = {
|
const INTEGRATIONS: Record<SourceName, any> = {
|
||||||
|
@ -68,7 +69,9 @@ if (
|
||||||
INTEGRATIONS[SourceName.ORACLE] = oracle.integration
|
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
|
// check if its integrated, faster
|
||||||
const definition = DEFINITIONS[source]
|
const definition = DEFINITIONS[source]
|
||||||
if (definition) {
|
if (definition) {
|
||||||
|
@ -109,7 +112,7 @@ export async function getIntegration(integration: SourceName) {
|
||||||
for (let plugin of plugins) {
|
for (let plugin of plugins) {
|
||||||
if (plugin.name === integration) {
|
if (plugin.name === integration) {
|
||||||
// need to use commonJS require due to its dynamic runtime nature
|
// 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) {
|
if (retrieved.integration) {
|
||||||
return retrieved.integration
|
return retrieved.integration
|
||||||
} else {
|
} else {
|
||||||
|
@ -121,12 +124,7 @@ export async function getIntegration(integration: SourceName) {
|
||||||
throw new Error("No datasource implementation found.")
|
throw new Error("No datasource implementation found.")
|
||||||
}
|
}
|
||||||
|
|
||||||
const VALIDATORS = {
|
|
||||||
[SourceName.POSTGRES]: postgres.validateConnection,
|
|
||||||
}
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
getDefinitions,
|
getDefinitions,
|
||||||
getIntegration,
|
getIntegration,
|
||||||
getValidator: VALIDATORS,
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -150,6 +150,17 @@ class PostgresIntegration extends Sql implements DatasourcePlus {
|
||||||
this.open = false
|
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 {
|
getBindingIdentifier(): string {
|
||||||
return `$${this.index++}`
|
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 {
|
export default {
|
||||||
schema: SCHEMA,
|
schema: SCHEMA,
|
||||||
integration: PostgresIntegration,
|
integration: PostgresIntegration,
|
||||||
validateConnection,
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,10 @@
|
||||||
import { SourceName } from "@budibase/types"
|
import postgres from "../../../integrations/postgres"
|
||||||
import integrations from "../../../integrations"
|
|
||||||
import { GenericContainer } from "testcontainers"
|
import { GenericContainer } from "testcontainers"
|
||||||
|
|
||||||
jest.unmock("pg")
|
jest.unmock("pg")
|
||||||
|
|
||||||
describe("datasource validators", () => {
|
describe("datasource validators", () => {
|
||||||
describe("postgres", () => {
|
describe("postgres", () => {
|
||||||
const validator = integrations.getValidator[SourceName.POSTGRES]!
|
|
||||||
|
|
||||||
let host: string
|
let host: string
|
||||||
let port: number
|
let port: number
|
||||||
|
|
||||||
|
@ -22,7 +19,7 @@ describe("datasource validators", () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
it("test valid connection string", async () => {
|
it("test valid connection string", async () => {
|
||||||
const result = await validator({
|
const integration = new postgres.integration({
|
||||||
host,
|
host,
|
||||||
port,
|
port,
|
||||||
database: "postgres",
|
database: "postgres",
|
||||||
|
@ -32,11 +29,12 @@ describe("datasource validators", () => {
|
||||||
ssl: false,
|
ssl: false,
|
||||||
rejectUnauthorized: false,
|
rejectUnauthorized: false,
|
||||||
})
|
})
|
||||||
|
const result = await integration.testConnection()
|
||||||
expect(result).toBeTruthy()
|
expect(result).toBeTruthy()
|
||||||
})
|
})
|
||||||
|
|
||||||
it("test invalid connection string", async () => {
|
it("test invalid connection string", async () => {
|
||||||
const result = await validator({
|
const integration = new postgres.integration({
|
||||||
host,
|
host,
|
||||||
port,
|
port,
|
||||||
database: "postgres",
|
database: "postgres",
|
||||||
|
@ -46,6 +44,7 @@ describe("datasource validators", () => {
|
||||||
ssl: false,
|
ssl: false,
|
||||||
rejectUnauthorized: false,
|
rejectUnauthorized: false,
|
||||||
})
|
})
|
||||||
|
const result = await integration.testConnection()
|
||||||
expect(result).toEqual({
|
expect(result).toEqual({
|
||||||
error: 'password authentication failed for user "wrong"',
|
error: 'password authentication failed for user "wrong"',
|
||||||
})
|
})
|
||||||
|
|
|
@ -128,6 +128,12 @@ export interface IntegrationBase {
|
||||||
read?(query: any): Promise<any[] | any>
|
read?(query: any): Promise<any[] | any>
|
||||||
update?(query: any): Promise<any[] | any>
|
update?(query: any): Promise<any[] | any>
|
||||||
delete?(query: any): Promise<any[] | any>
|
delete?(query: any): Promise<any[] | any>
|
||||||
|
testConnection?(): Promise<
|
||||||
|
| true
|
||||||
|
| {
|
||||||
|
error: string
|
||||||
|
}
|
||||||
|
>
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface DatasourcePlus extends IntegrationBase {
|
export interface DatasourcePlus extends IntegrationBase {
|
||||||
|
|
Loading…
Reference in New Issue