From c41a0f8d78aada2b4c83f611461802041333245b Mon Sep 17 00:00:00 2001 From: Dean Date: Tue, 15 Nov 2022 14:29:58 +0000 Subject: [PATCH] Default datasource behaviour with order of preference: DefaultDB, Internal then External --- .../src/builderStore/store/frontend.js | 43 +++++++++++++++++++ .../builder/app/[application]/_layout.svelte | 4 -- .../builder/src/stores/backend/datasources.js | 1 + 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/packages/builder/src/builderStore/store/frontend.js b/packages/builder/src/builderStore/store/frontend.js index 1291f81294..9814bcfc74 100644 --- a/packages/builder/src/builderStore/store/frontend.js +++ b/packages/builder/src/builderStore/store/frontend.js @@ -481,6 +481,40 @@ export const getFrontendStore = () => { return null } + const dataSourceField = definition.settings.find( + setting => setting.type == "dataSource" + ) + + let defaultDatasource + if (dataSourceField) { + const _tables = get(tables) + const filteredTables = _tables.list.filter( + table => table._id != "ta_users" + ) + + const internalTable = filteredTables.find( + table => + table.sourceId === "bb_internal" && table.type == "internal" + ) + + const defaultSourceTable = filteredTables.find( + table => + table.sourceId !== "bb_internal" && table.type == "internal" + ) + + const defaultExternalTable = filteredTables.find( + table => table.type == "external" + ) + + if (defaultSourceTable) { + defaultDatasource = defaultSourceTable + } else if (internalTable) { + defaultDatasource = internalTable + } else if (defaultExternalTable) { + defaultDatasource = defaultExternalTable + } + } + // Generate default props const settings = getComponentSettings(componentName) let props = { ...presetProps } @@ -490,6 +524,15 @@ export const getFrontendStore = () => { } }) + // Set a default datasource + if (dataSourceField && defaultDatasource) { + props["dataSource"] = { + label: defaultDatasource.name, + tableId: defaultDatasource._id, + type: "table", + } + } + // Add any extra properties the component needs let extras = {} if (definition.hasChildren) { diff --git a/packages/builder/src/pages/builder/app/[application]/_layout.svelte b/packages/builder/src/pages/builder/app/[application]/_layout.svelte index a854d09304..5296604dc7 100644 --- a/packages/builder/src/pages/builder/app/[application]/_layout.svelte +++ b/packages/builder/src/pages/builder/app/[application]/_layout.svelte @@ -1,7 +1,6 @@