diff --git a/packages/builder/src/builderStore/store/frontend.js b/packages/builder/src/builderStore/store/frontend.js index 1291f81294..fb486c6d27 100644 --- a/packages/builder/src/builderStore/store/frontend.js +++ b/packages/builder/src/builderStore/store/frontend.js @@ -20,6 +20,11 @@ import { } from "../componentUtils" import { Helpers } from "@budibase/bbui" import { Utils } from "@budibase/frontend-core" +import { + BUDIBASE_INTERNAL_DB_ID, + DB_TYPE_INTERNAL, + DB_TYPE_EXTERNAL, +} from "constants/backend" const INITIAL_FRONTEND_STATE = { apps: [], @@ -481,8 +486,41 @@ export const getFrontendStore = () => { return null } - // Generate default props + // Flattened settings const settings = getComponentSettings(componentName) + + let dataSourceField = settings.find( + setting => setting.type == "dataSource" || setting.type == "table" + ) + + 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 === BUDIBASE_INTERNAL_DB_ID && + table.type == DB_TYPE_INTERNAL + ) + + const defaultSourceTable = filteredTables.find( + table => + table.sourceId !== BUDIBASE_INTERNAL_DB_ID && + table.type == DB_TYPE_INTERNAL + ) + + const defaultExternalTable = filteredTables.find( + table => table.type == DB_TYPE_EXTERNAL + ) + + defaultDatasource = + defaultSourceTable || internalTable || defaultExternalTable + } + + // Generate default props let props = { ...presetProps } settings.forEach(setting => { if (setting.defaultValue !== undefined) { @@ -490,6 +528,15 @@ export const getFrontendStore = () => { } }) + // Set a default datasource + if (dataSourceField && defaultDatasource) { + props[dataSourceField.key] = { + 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/constants/backend/index.js b/packages/builder/src/constants/backend/index.js index 5ecc7bc324..f3898e1d9e 100644 --- a/packages/builder/src/constants/backend/index.js +++ b/packages/builder/src/constants/backend/index.js @@ -175,6 +175,8 @@ export const SWITCHABLE_TYPES = [ export const BUDIBASE_INTERNAL_DB_ID = "bb_internal" export const BUDIBASE_DATASOURCE_TYPE = "budibase" +export const DB_TYPE_INTERNAL = "internal" +export const DB_TYPE_EXTERNAL = "external" export const IntegrationTypes = { POSTGRES: "POSTGRES", 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 @@