From a3204ad17a0814f04400881b0fc97c8c0dd812e7 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Mon, 26 Jun 2023 16:34:22 +0100 Subject: [PATCH] Update data section routing to properly handle datasource entities/table sync and handle having no data --- .../app/[application]/data/_layout.svelte | 11 +++- .../data/datasource/index.svelte | 6 ++- .../app/[application]/data/index.svelte | 16 +++--- .../app/[application]/data/table/index.svelte | 16 +++--- .../builder/src/stores/backend/datasources.js | 50 ++++++++++++++++--- 5 files changed, 71 insertions(+), 28 deletions(-) diff --git a/packages/builder/src/pages/builder/app/[application]/data/_layout.svelte b/packages/builder/src/pages/builder/app/[application]/data/_layout.svelte index c0813fd2b8..a2db33306c 100644 --- a/packages/builder/src/pages/builder/app/[application]/data/_layout.svelte +++ b/packages/builder/src/pages/builder/app/[application]/data/_layout.svelte @@ -2,8 +2,17 @@ import { Button, Layout } from "@budibase/bbui" import DatasourceNavigator from "components/backend/DatasourceNavigator/DatasourceNavigator.svelte" import Panel from "components/design/Panel.svelte" - import { isActive, goto } from "@roxi/routify" + import { isActive, goto, redirect } from "@roxi/routify" import BetaButton from "./_components/BetaButton.svelte" + import { datasources } from "stores/backend" + + $: { + // If we ever don't have any data other than the users table, prompt the + // user to add some + if (!$datasources.hasData) { + $redirect("./new") + } + } diff --git a/packages/builder/src/pages/builder/app/[application]/data/datasource/index.svelte b/packages/builder/src/pages/builder/app/[application]/data/datasource/index.svelte index d3b962a226..347e1138d4 100644 --- a/packages/builder/src/pages/builder/app/[application]/data/datasource/index.svelte +++ b/packages/builder/src/pages/builder/app/[application]/data/datasource/index.svelte @@ -4,11 +4,13 @@ import { onMount } from "svelte" onMount(async () => { - const { list, selected } = $datasources + const { list, selected, hasData } = $datasources if (selected) { $redirect(`./${selected?._id}`) - } else { + } else if (hasData && list?.length) { $redirect(`./${list[0]._id}`) + } else { + $redirect("../new") } }) 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 47939f09b4..9ccc9bffcf 100644 --- a/packages/builder/src/pages/builder/app/[application]/data/index.svelte +++ b/packages/builder/src/pages/builder/app/[application]/data/index.svelte @@ -1,17 +1,13 @@ diff --git a/packages/builder/src/pages/builder/app/[application]/data/table/index.svelte b/packages/builder/src/pages/builder/app/[application]/data/table/index.svelte index 6f12ae38ac..f513d8ceb9 100644 --- a/packages/builder/src/pages/builder/app/[application]/data/table/index.svelte +++ b/packages/builder/src/pages/builder/app/[application]/data/table/index.svelte @@ -1,14 +1,16 @@ diff --git a/packages/builder/src/stores/backend/datasources.js b/packages/builder/src/stores/backend/datasources.js index 56c0a1b688..592437b629 100644 --- a/packages/builder/src/stores/backend/datasources.js +++ b/packages/builder/src/stores/backend/datasources.js @@ -1,9 +1,14 @@ import { writable, derived, get } from "svelte/store" -import { IntegrationTypes, DEFAULT_BB_DATASOURCE_ID } from "constants/backend" +import { + IntegrationTypes, + DEFAULT_BB_DATASOURCE_ID, + BUDIBASE_INTERNAL_DB_ID, +} from "constants/backend" import { queries, tables } from "./" import { API } from "api" import { DatasourceFeature } from "@budibase/types" import { notifications } from "@budibase/bbui" +import { TableNames } from "constants" export class ImportTableError extends Error { constructor(message) { @@ -24,13 +29,42 @@ export function createDatasourcesStore() { schemaError: null, }) - const derivedStore = derived(store, $store => ({ - ...$store, - selected: $store.list?.find(ds => ds._id === $store.selectedDatasourceId), - hasDefaultData: $store.list.some( - datasource => datasource._id === DEFAULT_BB_DATASOURCE_ID - ), - })) + const derivedStore = derived([store, tables], ([$store, $tables]) => { + // Set the internal datasource entities from the table list, which we're + // able to keep updated unlike the egress generated definition of the + // internal datasource + let internalDS = $store.list?.find(ds => ds._id === BUDIBASE_INTERNAL_DB_ID) + let otherDS = $store.list?.find(ds => ds._id !== BUDIBASE_INTERNAL_DB_ID) + if (internalDS) { + internalDS = { + ...internalDS, + entities: $tables.list?.filter(table => { + return ( + table.sourceId === BUDIBASE_INTERNAL_DB_ID && + table._id !== TableNames.USERS + ) + }), + } + } + + // Build up enriched DS list + // Only add the internal DS if we have at least one non-users table + let list = [] + if (internalDS?.entities?.length) { + list.push(internalDS) + } + list = list.concat(otherDS || []) + + return { + ...$store, + list, + selected: list?.find(ds => ds._id === $store.selectedDatasourceId), + hasDefaultData: list?.some( + datasource => datasource._id === DEFAULT_BB_DATASOURCE_ID + ), + hasData: !!internalDS?.entities?.length || list?.length > 1, + } + }) const fetch = async () => { const datasources = await API.getDatasources()