From 3388008f0b27c10b902fcc7b99d0f2eebe073176 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Mon, 15 Aug 2022 18:38:09 +0100 Subject: [PATCH] Adding custom datasources to UI. --- .../DatasourceNavigator/icons/Custom.svelte | 39 +++++++++++++++++++ .../DatasourceNavigator/icons/index.js | 2 + .../modals/CreateDatasourceModal.svelte | 10 ++++- .../server/src/api/controllers/datasource.js | 8 ++-- .../server/src/integrations/base/query.ts | 4 +- packages/server/src/integrations/index.ts | 20 ++++++++-- packages/server/src/threads/query.ts | 4 +- 7 files changed, 75 insertions(+), 12 deletions(-) create mode 100644 packages/builder/src/components/backend/DatasourceNavigator/icons/Custom.svelte diff --git a/packages/builder/src/components/backend/DatasourceNavigator/icons/Custom.svelte b/packages/builder/src/components/backend/DatasourceNavigator/icons/Custom.svelte new file mode 100644 index 0000000000..354b7a3358 --- /dev/null +++ b/packages/builder/src/components/backend/DatasourceNavigator/icons/Custom.svelte @@ -0,0 +1,39 @@ + + + + + + + + + + diff --git a/packages/builder/src/components/backend/DatasourceNavigator/icons/index.js b/packages/builder/src/components/backend/DatasourceNavigator/icons/index.js index 404895f05a..267ed32b6e 100644 --- a/packages/builder/src/components/backend/DatasourceNavigator/icons/index.js +++ b/packages/builder/src/components/backend/DatasourceNavigator/icons/index.js @@ -15,6 +15,7 @@ import GoogleSheets from "./GoogleSheets.svelte" import Firebase from "./Firebase.svelte" import Redis from "./Redis.svelte" import Snowflake from "./Snowflake.svelte" +import Custom from "./Custom.svelte" export default { BUDIBASE: Budibase, @@ -34,4 +35,5 @@ export default { FIRESTORE: Firebase, REDIS: Redis, SNOWFLAKE: Snowflake, + CUSTOM: Custom, } diff --git a/packages/builder/src/components/backend/DatasourceNavigator/modals/CreateDatasourceModal.svelte b/packages/builder/src/components/backend/DatasourceNavigator/modals/CreateDatasourceModal.svelte index 8d34c292f3..2d5e6976bf 100644 --- a/packages/builder/src/components/backend/DatasourceNavigator/modals/CreateDatasourceModal.svelte +++ b/packages/builder/src/components/backend/DatasourceNavigator/modals/CreateDatasourceModal.svelte @@ -92,6 +92,14 @@ } integrations = newIntegrations } + + function getIcon(integrationType, schema) { + if (schema.custom) { + return ICONS.CUSTOM + } else { + return ICONS[integrationType] + } + } @@ -158,7 +166,7 @@ >
diff --git a/packages/server/src/api/controllers/datasource.js b/packages/server/src/api/controllers/datasource.js index 8b9b765a5f..36d345912e 100644 --- a/packages/server/src/api/controllers/datasource.js +++ b/packages/server/src/api/controllers/datasource.js @@ -7,7 +7,7 @@ const { getTableParams, } = require("../../db/utils") const { BuildSchemaErrors, InvalidColumns } = require("../../constants") -const { integrations } = require("../../integrations") +const { getIntegration } = require("../../integrations") const { getDatasourceAndQuery } = require("./row/utils") const { invalidateDynamicVariables } = require("../../threads/utils") const { getAppDB } = require("@budibase/backend-core/context") @@ -114,7 +114,7 @@ exports.update = async function (ctx) { // Drain connection pools when configuration is changed if (datasource.source) { - const source = integrations[datasource.source] + const source = await getIntegration(datasource.source) if (source && source.pool) { await source.pool.end() } @@ -149,7 +149,7 @@ exports.save = async function (ctx) { // Drain connection pools when configuration is changed if (datasource.source) { - const source = integrations[datasource.source] + const source = await getIntegration(datasource.source) if (source && source.pool) { await source.pool.end() } @@ -218,7 +218,7 @@ function updateError(error, newError, tables) { } const buildSchemaHelper = async datasource => { - const Connector = integrations[datasource.source] + const Connector = await getIntegration(datasource.source) // Connect to the DB and build the schema const connector = new Connector(datasource.config) diff --git a/packages/server/src/integrations/base/query.ts b/packages/server/src/integrations/base/query.ts index 1f3ed3dd74..7435b28141 100644 --- a/packages/server/src/integrations/base/query.ts +++ b/packages/server/src/integrations/base/query.ts @@ -1,11 +1,11 @@ import { QueryJson, Datasource } from "@budibase/types" -const { integrations } = require("../index") +const { getIntegration } = require("../index") export async function makeExternalQuery( datasource: Datasource, json: QueryJson ) { - const Integration = integrations[datasource.source] + const Integration = await getIntegration(datasource.source) // query is the opinionated function if (Integration.prototype.query) { const integration = new Integration(datasource.config) diff --git a/packages/server/src/integrations/index.ts b/packages/server/src/integrations/index.ts index bde7235ac8..bf267e46cc 100644 --- a/packages/server/src/integrations/index.ts +++ b/packages/server/src/integrations/index.ts @@ -67,8 +67,22 @@ if (environment.SELF_HOSTED) { module.exports = { getDefinitions: async () => { - const custom = await getPlugins(PluginType.DATASOURCE) - return cloneDeep(DEFINITIONS) + const plugins = await getPlugins(PluginType.DATASOURCE) + // extract the actual schema from each custom + const pluginSchemas: { [key: string]: Integration } = {} + for (let plugin of plugins) { + const sourceId = plugin.name + pluginSchemas[sourceId] = { + ...plugin.schema["schema"], + custom: true, + } + } + return { + ...cloneDeep(DEFINITIONS), + ...pluginSchemas, + } + }, + getIntegration: async () => { + return INTEGRATIONS }, - integrations: INTEGRATIONS, } diff --git a/packages/server/src/threads/query.ts b/packages/server/src/threads/query.ts index 6b93a00200..86cbf89c87 100644 --- a/packages/server/src/threads/query.ts +++ b/packages/server/src/threads/query.ts @@ -2,7 +2,7 @@ import { default as threadUtils } from "./utils" threadUtils.threadSetup() import { WorkerCallback, QueryEvent, QueryVariable } from "./definitions" const ScriptRunner = require("../utilities/scriptRunner") -const { integrations } = require("../integrations") +const { getIntegration } = require("../integrations") const { processStringSync } = require("@budibase/string-templates") const { doInAppContext, getAppDB } = require("@budibase/backend-core/context") const { @@ -62,7 +62,7 @@ class QueryRunner { let datasourceClone = cloneDeep(datasource) let fieldsClone = cloneDeep(fields) - const Integration = integrations[datasourceClone.source] + const Integration = await getIntegration(datasourceClone.source) if (!Integration) { throw "Integration type does not exist." }