From 6b7269b9542bd4e5ac52d17d429527855c077a8e Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Mon, 15 May 2023 17:36:16 +0100 Subject: [PATCH] Refactoring response from integrations to a simple type. --- packages/server/src/api/controllers/datasource.ts | 2 +- packages/server/src/integrations/airtable.ts | 2 +- packages/server/src/integrations/arangodb.ts | 9 +++++++-- packages/server/src/integrations/couchdb.ts | 9 +++++++-- packages/server/src/integrations/dynamodb.ts | 9 +++++++-- packages/server/src/integrations/elasticsearch.ts | 2 +- packages/server/src/integrations/firebase.ts | 2 +- packages/server/src/integrations/googlesheets.ts | 2 +- .../server/src/integrations/microsoftSqlServer.ts | 9 +++++++-- packages/server/src/integrations/mongodb.ts | 9 +++++++-- packages/server/src/integrations/mysql.ts | 9 +++++++-- packages/server/src/integrations/oracle.ts | 9 ++------- packages/server/src/integrations/postgres.ts | 9 +++++++-- packages/server/src/integrations/redis.ts | 9 +++++++-- packages/server/src/integrations/s3.ts | 15 ++++++++++----- packages/server/src/integrations/snowflake.ts | 2 +- packages/types/src/sdk/datasources.ts | 12 ++++++------ 17 files changed, 80 insertions(+), 40 deletions(-) diff --git a/packages/server/src/api/controllers/datasource.ts b/packages/server/src/api/controllers/datasource.ts index 363cde2e6f..3534fc3076 100644 --- a/packages/server/src/api/controllers/datasource.ts +++ b/packages/server/src/api/controllers/datasource.ts @@ -140,7 +140,7 @@ export async function verify( } else { ctx.body = { connected: false, - error: response.error + error: response.error, } } } diff --git a/packages/server/src/integrations/airtable.ts b/packages/server/src/integrations/airtable.ts index cb13de4f10..d606cd570b 100644 --- a/packages/server/src/integrations/airtable.ts +++ b/packages/server/src/integrations/airtable.ts @@ -19,7 +19,7 @@ const SCHEMA: Integration = { "Airtable is a spreadsheet-database hybrid, with the features of a database but applied to a spreadsheet.", friendlyName: "Airtable", type: "Spreadsheet", - features: [DatasourceFeature.CONNECTION_CHECKING], + features: [], datasource: { apiKey: { type: DatasourceFieldType.PASSWORD, diff --git a/packages/server/src/integrations/arangodb.ts b/packages/server/src/integrations/arangodb.ts index 544dc12377..b486748a68 100644 --- a/packages/server/src/integrations/arangodb.ts +++ b/packages/server/src/integrations/arangodb.ts @@ -4,6 +4,7 @@ import { QueryType, IntegrationBase, DatasourceFeature, + ConnectionInfo, } from "@budibase/types" import { Database, aql } from "arangojs" @@ -77,12 +78,16 @@ class ArangoDBIntegration implements IntegrationBase { } async testConnection() { + const response: ConnectionInfo = { + connected: false, + } try { await this.client.get() - return true + response.connected = true } catch (e: any) { - return { error: e.message as string } + response.error = e.message as string } + return response } async read(query: { sql: any }) { diff --git a/packages/server/src/integrations/couchdb.ts b/packages/server/src/integrations/couchdb.ts index 663081edd3..4ccbd5456d 100644 --- a/packages/server/src/integrations/couchdb.ts +++ b/packages/server/src/integrations/couchdb.ts @@ -1,4 +1,5 @@ import { + ConnectionInfo, DatasourceFeature, DatasourceFieldType, Document, @@ -70,12 +71,16 @@ class CouchDBIntegration implements IntegrationBase { } async testConnection() { + const response: ConnectionInfo = { + connected: false, + } try { const result = await this.query("exists", "validation error", {}) - return result === true + response.connected = result === true } catch (e: any) { - return { error: e.message as string } + response.error = e.message as string } + return response } async query( diff --git a/packages/server/src/integrations/dynamodb.ts b/packages/server/src/integrations/dynamodb.ts index 9f63edb465..28b42c7a54 100644 --- a/packages/server/src/integrations/dynamodb.ts +++ b/packages/server/src/integrations/dynamodb.ts @@ -4,6 +4,7 @@ import { QueryType, IntegrationBase, DatasourceFeature, + ConnectionInfo, } from "@budibase/types" import AWS from "aws-sdk" @@ -152,12 +153,16 @@ class DynamoDBIntegration implements IntegrationBase { } async testConnection() { + const response: ConnectionInfo = { + connected: false, + } try { const scanRes = await new AWS.DynamoDB(this.config).listTables().promise() - return !!scanRes.$response + response.connected = !!scanRes.$response } catch (e: any) { - return { error: e.message as string } + response.error = e.message as string } + return response } async create(query: { diff --git a/packages/server/src/integrations/elasticsearch.ts b/packages/server/src/integrations/elasticsearch.ts index 475ddc0bd1..621e81f367 100644 --- a/packages/server/src/integrations/elasticsearch.ts +++ b/packages/server/src/integrations/elasticsearch.ts @@ -21,7 +21,7 @@ const SCHEMA: Integration = { "Elasticsearch is a search engine based on the Lucene library. It provides a distributed, multitenant-capable full-text search engine with an HTTP web interface and schema-free JSON documents.", friendlyName: "ElasticSearch", type: "Non-relational", - features: [DatasourceFeature.CONNECTION_CHECKING], + features: [], datasource: { url: { type: DatasourceFieldType.STRING, diff --git a/packages/server/src/integrations/firebase.ts b/packages/server/src/integrations/firebase.ts index 0646c98eba..e8b3c8d92c 100644 --- a/packages/server/src/integrations/firebase.ts +++ b/packages/server/src/integrations/firebase.ts @@ -19,7 +19,7 @@ const SCHEMA: Integration = { type: "Non-relational", description: "Cloud Firestore is a flexible, scalable database for mobile, web, and server development from Firebase and Google Cloud.", - features: [DatasourceFeature.CONNECTION_CHECKING], + features: [], datasource: { email: { type: DatasourceFieldType.STRING, diff --git a/packages/server/src/integrations/googlesheets.ts b/packages/server/src/integrations/googlesheets.ts index fef4689fdf..0b2be54fb3 100644 --- a/packages/server/src/integrations/googlesheets.ts +++ b/packages/server/src/integrations/googlesheets.ts @@ -65,7 +65,7 @@ const SCHEMA: Integration = { "Create and collaborate on online spreadsheets in real-time and from any device. ", friendlyName: "Google Sheets", type: "Spreadsheet", - features: [DatasourceFeature.CONNECTION_CHECKING], + features: [], datasource: { spreadsheetId: { display: "Google Sheet URL", diff --git a/packages/server/src/integrations/microsoftSqlServer.ts b/packages/server/src/integrations/microsoftSqlServer.ts index eb65be43da..47f36f60e9 100644 --- a/packages/server/src/integrations/microsoftSqlServer.ts +++ b/packages/server/src/integrations/microsoftSqlServer.ts @@ -9,6 +9,7 @@ import { SqlQuery, DatasourcePlus, DatasourceFeature, + ConnectionInfo, } from "@budibase/types" import { getSqlQuery, @@ -124,12 +125,16 @@ class SqlServerIntegration extends Sql implements DatasourcePlus { } async testConnection() { + const response: ConnectionInfo = { + connected: false, + } try { await this.connect() - return true + response.connected = true } catch (e: any) { - return { error: e.message as string } + response.error = e.message as string } + return response } getBindingIdentifier(): string { diff --git a/packages/server/src/integrations/mongodb.ts b/packages/server/src/integrations/mongodb.ts index 9e3d7344b4..ee7302c501 100644 --- a/packages/server/src/integrations/mongodb.ts +++ b/packages/server/src/integrations/mongodb.ts @@ -4,6 +4,7 @@ import { QueryType, IntegrationBase, DatasourceFeature, + ConnectionInfo, } from "@budibase/types" import { MongoClient, @@ -361,12 +362,16 @@ class MongoIntegration implements IntegrationBase { } async testConnection() { + const response: ConnectionInfo = { + connected: false, + } try { await this.connect() - return true + response.connected = true } catch (e: any) { - return { error: e.message as string } + response.error = e.message as string } + return response } async connect() { diff --git a/packages/server/src/integrations/mysql.ts b/packages/server/src/integrations/mysql.ts index 1f3ea4ea1c..eb721a6e0f 100644 --- a/packages/server/src/integrations/mysql.ts +++ b/packages/server/src/integrations/mysql.ts @@ -8,6 +8,7 @@ import { TableSchema, DatasourcePlus, DatasourceFeature, + ConnectionInfo, } from "@budibase/types" import { getSqlQuery, @@ -155,15 +156,19 @@ class MySQLIntegration extends Sql implements DatasourcePlus { } async testConnection() { + const response: ConnectionInfo = { + connected: false, + } try { const [result] = await this.internalQuery( { sql: "SELECT 1+1 AS checkRes" }, { connect: true } ) - return result?.checkRes == 2 + response.connected = result?.checkRes == 2 } catch (e: any) { - return { error: e.message as string } + response.error = e.message as string } + return response } getBindingIdentifier(): string { diff --git a/packages/server/src/integrations/oracle.ts b/packages/server/src/integrations/oracle.ts index bbe2189bdc..82d8382a31 100644 --- a/packages/server/src/integrations/oracle.ts +++ b/packages/server/src/integrations/oracle.ts @@ -25,12 +25,7 @@ import { ExecuteOptions, Result, } from "oracledb" -import { - OracleTable, - OracleColumn, - OracleColumnsResponse, - OracleConstraint, -} from "./base/types" +import { OracleTable, OracleColumn, OracleColumnsResponse } from "./base/types" let oracledb: any try { oracledb = require("oracledb") @@ -54,7 +49,7 @@ const SCHEMA: Integration = { type: "Relational", description: "Oracle Database is an object-relational database management system developed by Oracle Corporation", - features: [DatasourceFeature.CONNECTION_CHECKING], + features: [], datasource: { host: { type: DatasourceFieldType.STRING, diff --git a/packages/server/src/integrations/postgres.ts b/packages/server/src/integrations/postgres.ts index 2ffa04c2e4..bf77ec08c6 100644 --- a/packages/server/src/integrations/postgres.ts +++ b/packages/server/src/integrations/postgres.ts @@ -7,6 +7,7 @@ import { Table, DatasourcePlus, DatasourceFeature, + ConnectionInfo, } from "@budibase/types" import { getSqlQuery, @@ -153,14 +154,18 @@ class PostgresIntegration extends Sql implements DatasourcePlus { } async testConnection() { + const response: ConnectionInfo = { + connected: false, + } try { await this.openConnection() - return true + response.connected = true } catch (e: any) { - return { error: e.message as string } + response.error = e.message as string } finally { await this.closeConnection() } + return response } getBindingIdentifier(): string { diff --git a/packages/server/src/integrations/redis.ts b/packages/server/src/integrations/redis.ts index 1d1d705026..d71f66edc1 100644 --- a/packages/server/src/integrations/redis.ts +++ b/packages/server/src/integrations/redis.ts @@ -1,4 +1,5 @@ import { + ConnectionInfo, DatasourceFeature, DatasourceFieldType, Integration, @@ -107,14 +108,18 @@ class RedisIntegration { } async testConnection() { + const response: ConnectionInfo = { + connected: false, + } try { await this.client.ping() - return true + response.connected = true } catch (e: any) { - return { error: e.message as string } + response.error = e.message as string } finally { await this.disconnect() } + return response } async disconnect() { diff --git a/packages/server/src/integrations/s3.ts b/packages/server/src/integrations/s3.ts index d09c7f890d..0f9848ed59 100644 --- a/packages/server/src/integrations/s3.ts +++ b/packages/server/src/integrations/s3.ts @@ -4,6 +4,7 @@ import { IntegrationBase, DatasourceFieldType, DatasourceFeature, + ConnectionInfo, } from "@budibase/types" import AWS from "aws-sdk" @@ -168,12 +169,16 @@ class S3Integration implements IntegrationBase { } async testConnection() { - try { - const buckets = await this.client.listBuckets().promise() - return true - } catch (e: any) { - return { error: e.message as string } + const response: ConnectionInfo = { + connected: false, } + try { + await this.client.listBuckets().promise() + response.connected = true + } catch (e: any) { + response.error = e.message as string + } + return response } async create(query: { diff --git a/packages/server/src/integrations/snowflake.ts b/packages/server/src/integrations/snowflake.ts index 877405d447..46a40b8b03 100644 --- a/packages/server/src/integrations/snowflake.ts +++ b/packages/server/src/integrations/snowflake.ts @@ -21,7 +21,7 @@ const SCHEMA: Integration = { "Snowflake is a solution for data warehousing, data lakes, data engineering, data science, data application development, and securely sharing and consuming shared data.", friendlyName: "Snowflake", type: "Relational", - features: [DatasourceFeature.CONNECTION_CHECKING], + features: [], datasource: { account: { type: "string", diff --git a/packages/types/src/sdk/datasources.ts b/packages/types/src/sdk/datasources.ts index 000abb7590..9df9670877 100644 --- a/packages/types/src/sdk/datasources.ts +++ b/packages/types/src/sdk/datasources.ts @@ -128,17 +128,17 @@ export interface Integration { extra?: ExtraQueryConfig } +export type ConnectionInfo = { + connected: boolean + error?: string +} + export interface IntegrationBase { create?(query: any): Promise read?(query: any): Promise update?(query: any): Promise delete?(query: any): Promise - testConnection?(): Promise< - | boolean - | { - error: string - } - > + testConnection?(): Promise } export interface DatasourcePlus extends IntegrationBase {