From fb510907523c20d09f8e7b66581899265868d86a Mon Sep 17 00:00:00 2001 From: Mel O'Hagan Date: Wed, 17 Aug 2022 18:16:11 +0100 Subject: [PATCH 1/2] Allow list of named tables to be fetched --- .../PlusConfigForm.svelte | 19 ++++++++++++++++++- .../builder/src/stores/backend/datasources.js | 7 +++++-- packages/frontend-core/src/api/datasources.js | 6 +++++- .../server/src/api/controllers/datasource.js | 17 ++++++++++++++--- 4 files changed, 42 insertions(+), 7 deletions(-) diff --git a/packages/builder/src/components/backend/DatasourceNavigator/TableIntegrationMenu/PlusConfigForm.svelte b/packages/builder/src/components/backend/DatasourceNavigator/TableIntegrationMenu/PlusConfigForm.svelte index cd19523476..b81e818d5f 100644 --- a/packages/builder/src/components/backend/DatasourceNavigator/TableIntegrationMenu/PlusConfigForm.svelte +++ b/packages/builder/src/components/backend/DatasourceNavigator/TableIntegrationMenu/PlusConfigForm.svelte @@ -8,6 +8,7 @@ notifications, Modal, Table, + Toggle, } from "@budibase/bbui" import { datasources, integrations, tables } from "stores/backend" import CreateEditRelationship from "components/backend/Datasources/CreateEditRelationship.svelte" @@ -15,6 +16,7 @@ import ArrayRenderer from "components/common/renderers/ArrayRenderer.svelte" import ConfirmDialog from "components/common/ConfirmDialog.svelte" import { goto } from "@roxi/routify" + import ValuesList from "components/common/ValuesList.svelte" export let datasource export let save @@ -31,6 +33,8 @@ let createExternalTableModal let selectedFromRelationship, selectedToRelationship let confirmDialog + let specificTables = null + let requireSpecificTables = false $: integration = datasource && $integrations[datasource.source] $: plusTables = datasource?.plus @@ -87,7 +91,7 @@ async function updateDatasourceSchema() { try { - await datasources.updateSchema(datasource) + await datasources.updateSchema(datasource, specificTables) notifications.success(`Datasource ${name} tables updated successfully.`) await tables.fetch() } catch (error) { @@ -150,6 +154,19 @@ warning={false} title="Confirm table fetch" > + { + requireSpecificTables = e.detail + specificTables = null + }} + thin + text="Fetch listed tables only (one per line)" + /> + {#if requireSpecificTables} + + {/if} +
If you have fetched tables from this database before, this action may overwrite any changes you made after your initial fetch. diff --git a/packages/builder/src/stores/backend/datasources.js b/packages/builder/src/stores/backend/datasources.js index 2423394c6a..07aeab1921 100644 --- a/packages/builder/src/stores/backend/datasources.js +++ b/packages/builder/src/stores/backend/datasources.js @@ -62,8 +62,11 @@ export function createDatasourcesStore() { unselect: () => { update(state => ({ ...state, selected: null })) }, - updateSchema: async datasource => { - const response = await API.buildDatasourceSchema(datasource?._id) + updateSchema: async (datasource, tablesFilter) => { + const response = await API.buildDatasourceSchema({ + datasourceId: datasource?._id, + tablesFilter, + }) return await updateDatasource(response) }, save: async (body, fetchSchema = false) => { diff --git a/packages/frontend-core/src/api/datasources.js b/packages/frontend-core/src/api/datasources.js index ff72fbf25b..eda7b3c860 100644 --- a/packages/frontend-core/src/api/datasources.js +++ b/packages/frontend-core/src/api/datasources.js @@ -11,10 +11,14 @@ export const buildDatasourceEndpoints = API => ({ /** * Prompts the server to build the schema for a datasource. * @param datasourceId the datasource ID to build the schema for + * @param tablesFilter list of specific table names to be build the schema */ - buildDatasourceSchema: async datasourceId => { + buildDatasourceSchema: async ({ datasourceId, tablesFilter }) => { return await API.post({ url: `/api/datasources/${datasourceId}/schema`, + body: { + tablesFilter, + }, }) }, diff --git a/packages/server/src/api/controllers/datasource.js b/packages/server/src/api/controllers/datasource.js index 8b9b765a5f..75fd1db02a 100644 --- a/packages/server/src/api/controllers/datasource.js +++ b/packages/server/src/api/controllers/datasource.js @@ -50,9 +50,21 @@ exports.fetch = async function (ctx) { exports.buildSchemaFromDb = async function (ctx) { const db = getAppDB() const datasource = await db.get(ctx.params.datasourceId) + const tablesFilter = ctx.request.body.tablesFilter - const { tables, error } = await buildSchemaHelper(datasource) - datasource.entities = tables + let { tables, error } = await buildSchemaHelper(datasource) + if (tablesFilter) { + if (!datasource.entities) { + datasource.entities = {} + } + for (let key in tables) { + if (tablesFilter.includes(key)) { + datasource.entities[key] = tables[key] + } + } + } else { + datasource.entities = tables + } const dbResp = await db.put(datasource) datasource._rev = dbResp.rev @@ -223,7 +235,6 @@ const buildSchemaHelper = async datasource => { // 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 // make sure they all have a display name selected for (let entity of Object.values(datasource.entities)) { From 6e29e0a7572184d6fb8daed676a2cc09d0791143 Mon Sep 17 00:00:00 2001 From: Mel O'Hagan Date: Tue, 13 Sep 2022 10:55:38 +0100 Subject: [PATCH 2/2] Case insensitive table fetch --- packages/server/src/api/controllers/datasource.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/server/src/api/controllers/datasource.js b/packages/server/src/api/controllers/datasource.js index 75fd1db02a..f7784d31d6 100644 --- a/packages/server/src/api/controllers/datasource.js +++ b/packages/server/src/api/controllers/datasource.js @@ -58,7 +58,9 @@ exports.buildSchemaFromDb = async function (ctx) { datasource.entities = {} } for (let key in tables) { - if (tablesFilter.includes(key)) { + if ( + tablesFilter.some(filter => filter.toLowerCase() === key.toLowerCase()) + ) { datasource.entities[key] = tables[key] } } @@ -237,7 +239,7 @@ const buildSchemaHelper = async datasource => { await connector.buildSchema(datasource._id, datasource.entities) // make sure they all have a display name selected - for (let entity of Object.values(datasource.entities)) { + for (let entity of Object.values(datasource.entities ?? {})) { if (entity.primaryDisplay) { continue }