From 5ca4da11460cb88109291d77ab2ed859db0b5094 Mon Sep 17 00:00:00 2001 From: Peter Clement Date: Tue, 28 Sep 2021 12:25:57 +0100 Subject: [PATCH] check tables can be fetched before saving config --- .../modals/DatasourceConfigModal.svelte | 19 +++-------- .../app/[application]/data/index.svelte | 6 ---- .../builder/src/stores/backend/datasources.js | 7 ++-- .../server/src/api/controllers/datasource.js | 32 +++++++++++++------ 4 files changed, 32 insertions(+), 32 deletions(-) diff --git a/packages/builder/src/components/backend/DatasourceNavigator/modals/DatasourceConfigModal.svelte b/packages/builder/src/components/backend/DatasourceNavigator/modals/DatasourceConfigModal.svelte index 9221cbf083..56fa26ee0a 100644 --- a/packages/builder/src/components/backend/DatasourceNavigator/modals/DatasourceConfigModal.svelte +++ b/packages/builder/src/components/backend/DatasourceNavigator/modals/DatasourceConfigModal.svelte @@ -3,7 +3,7 @@ import { ModalContent, notifications, Body, Layout } from "@budibase/bbui" import analytics, { Events } from "analytics" import IntegrationConfigForm from "components/backend/DatasourceNavigator/TableIntegrationMenu/IntegrationConfigForm.svelte" - import { datasources, tables } from "stores/backend" + import { datasources } from "stores/backend" import { IntegrationNames } from "constants" export let integration @@ -27,13 +27,11 @@ return datasource } async function saveDatasource() { + const datasource = prepareData() try { // Create datasource - const resp = await datasources.save(prepareData()) + const resp = await datasources.save(datasource, datasource.plus) - if (integration.plus) { - updateDatasourceSchema(resp) - } await datasources.select(resp._id) $goto(`./datasource/${resp._id}`) notifications.success(`Datasource updated successfully.`) @@ -41,17 +39,10 @@ name: resp.name, source: resp.source, }) + return true } catch (err) { notifications.error(`Error saving datasource: ${err}`) - } - } - - async function updateDatasourceSchema(datasourceJson) { - try { - await datasources.updateSchema(datasourceJson) - await tables.fetch() - } catch (err) { - notifications.error(`Error updating datasource schema: ${err}`) + return false } } diff --git a/packages/builder/src/pages/builder/app/[application]/data/index.svelte b/packages/builder/src/pages/builder/app/[application]/data/index.svelte index 873f721a59..c27ee7b342 100644 --- a/packages/builder/src/pages/builder/app/[application]/data/index.svelte +++ b/packages/builder/src/pages/builder/app/[application]/data/index.svelte @@ -9,12 +9,6 @@ $datasources.list.find(x => (x._id = "bb_internal")).entities.length > 1 || $datasources.list.length > 1 - $: console.log( - $datasources.list.find(x => (x._id = "bb_internal")).entities.length > 1 - ) - $: console.log($datasources.list.length >= 1) - $: console.log($datasources.list) - onMount(() => { if (!setupComplete) { modal.show() diff --git a/packages/builder/src/stores/backend/datasources.js b/packages/builder/src/stores/backend/datasources.js index 5c6ed3f2cb..f7e689d8ad 100644 --- a/packages/builder/src/stores/backend/datasources.js +++ b/packages/builder/src/stores/backend/datasources.js @@ -58,7 +58,7 @@ export function createDatasourcesStore() { }) return json }, - save: async datasource => { + save: async (datasource, fetchSchema = false) => { let response if (datasource._id) { response = await api.put( @@ -66,7 +66,10 @@ export function createDatasourcesStore() { datasource ) } else { - response = await api.post("/api/datasources", datasource) + response = await api.post("/api/datasources", { + datasource: datasource, + fetchSchema, + }) } const json = await response.json() diff --git a/packages/server/src/api/controllers/datasource.js b/packages/server/src/api/controllers/datasource.js index 4a2fd7d86a..2ff7c7f9b8 100644 --- a/packages/server/src/api/controllers/datasource.js +++ b/packages/server/src/api/controllers/datasource.js @@ -41,15 +41,10 @@ exports.fetch = async function (ctx) { exports.buildSchemaFromDb = async function (ctx) { const db = new CouchDB(ctx.appId) - const datasourceId = ctx.params.datasourceId - const datasource = await db.get(datasourceId) + const datasource = await db.get(ctx.params.datasourceId) - const Connector = integrations[datasource.source] - - // Connect to the DB and build the schema - const connector = new Connector(datasource.config) - await connector.buildSchema(datasource._id, datasource.entities) - datasource.entities = connector.tables + const tables = await buildSchemaHelper(datasource) + datasource.entities = tables const response = await db.put(datasource) datasource._rev = response.rev @@ -81,12 +76,18 @@ exports.update = async function (ctx) { exports.save = async function (ctx) { const db = new CouchDB(ctx.appId) - const plus = ctx.request.body.plus + const plus = ctx.request.body.datasource.plus + const fetchSchema = ctx.request.body.fetchSchema const datasource = { _id: generateDatasourceID({ plus }), type: plus ? DocumentTypes.DATASOURCE_PLUS : DocumentTypes.DATASOURCE, - ...ctx.request.body, + ...ctx.request.body.datasource, + } + + if (fetchSchema) { + let tables = await buildSchemaHelper(datasource) + datasource.entities = tables } const response = await db.put(datasource) @@ -133,3 +134,14 @@ exports.query = async function (ctx) { ctx.throw(400, err) } } + +const buildSchemaHelper = async datasource => { + const Connector = integrations[datasource.source] + + // Connect to the DB and build the schema + const connector = new Connector(datasource.config) + await connector.buildSchema(datasource._id, datasource.entities) + datasource.entities = connector.tables + + return connector.tables +}