From 590ba5de60ca9c23ae572aa8fbc80745ae24bc66 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Tue, 17 Dec 2024 16:31:40 +0000 Subject: [PATCH 1/8] Updating datasource and sorted integration store to be classes, that extend the BudiStore base implementation. --- .../modals/UpdateDatasourceModal.svelte | 2 +- .../CreateEditRelationshipModal.svelte | 2 +- .../integration/RestQueryViewer.svelte | 2 +- .../_components/EditDatasourceConfig.svelte | 2 +- .../panels/SaveDatasourceButton.svelte | 2 +- .../builder/src/stores/builder/datasources.ts | 201 ++++++++++-------- .../src/stores/builder/sortedIntegrations.ts | 51 +++-- 7 files changed, 147 insertions(+), 115 deletions(-) diff --git a/packages/builder/src/components/backend/DatasourceNavigator/modals/UpdateDatasourceModal.svelte b/packages/builder/src/components/backend/DatasourceNavigator/modals/UpdateDatasourceModal.svelte index e6d6026f7c..845a1b8549 100644 --- a/packages/builder/src/components/backend/DatasourceNavigator/modals/UpdateDatasourceModal.svelte +++ b/packages/builder/src/components/backend/DatasourceNavigator/modals/UpdateDatasourceModal.svelte @@ -33,7 +33,7 @@ ...datasource, name, } - await datasources.update({ + await datasources.save({ datasource: updatedDatasource, integration: integrationForDatasource(get(integrations), datasource), }) diff --git a/packages/builder/src/components/backend/Datasources/CreateEditRelationshipModal.svelte b/packages/builder/src/components/backend/Datasources/CreateEditRelationshipModal.svelte index d2682597d4..247c1b8041 100644 --- a/packages/builder/src/components/backend/Datasources/CreateEditRelationshipModal.svelte +++ b/packages/builder/src/components/backend/Datasources/CreateEditRelationshipModal.svelte @@ -41,7 +41,7 @@ get(integrations), datasource ) - await datasources.update({ datasource, integration }) + await datasources.save({ datasource, integration }) await afterSave({ datasource, action }) } catch (err) { diff --git a/packages/builder/src/components/integration/RestQueryViewer.svelte b/packages/builder/src/components/integration/RestQueryViewer.svelte index 3b1356efe2..16d73e8863 100644 --- a/packages/builder/src/components/integration/RestQueryViewer.svelte +++ b/packages/builder/src/components/integration/RestQueryViewer.svelte @@ -176,7 +176,7 @@ notifications.success(`Request saved successfully`) if (dynamicVariables) { datasource.config.dynamicVariables = rebuildVariables(saveId) - datasource = await datasources.update({ + datasource = await datasources.save({ integration: integrationInfo, datasource, }) diff --git a/packages/builder/src/pages/builder/app/[application]/data/datasource/[datasourceId]/_components/EditDatasourceConfig.svelte b/packages/builder/src/pages/builder/app/[application]/data/datasource/[datasourceId]/_components/EditDatasourceConfig.svelte index c4f0861f45..b472a18514 100644 --- a/packages/builder/src/pages/builder/app/[application]/data/datasource/[datasourceId]/_components/EditDatasourceConfig.svelte +++ b/packages/builder/src/pages/builder/app/[application]/data/datasource/[datasourceId]/_components/EditDatasourceConfig.svelte @@ -13,7 +13,7 @@ async function saveDatasource({ config, name }) { try { - await datasources.update({ + await datasources.save({ integration, datasource: { ...datasource, config, name }, }) diff --git a/packages/builder/src/pages/builder/app/[application]/data/datasource/[datasourceId]/_components/panels/SaveDatasourceButton.svelte b/packages/builder/src/pages/builder/app/[application]/data/datasource/[datasourceId]/_components/panels/SaveDatasourceButton.svelte index 83a8e13c9e..b0f8b86a89 100644 --- a/packages/builder/src/pages/builder/app/[application]/data/datasource/[datasourceId]/_components/panels/SaveDatasourceButton.svelte +++ b/packages/builder/src/pages/builder/app/[application]/data/datasource/[datasourceId]/_components/panels/SaveDatasourceButton.svelte @@ -16,7 +16,7 @@ get(integrations), updatedDatasource ) - await datasources.update({ datasource: updatedDatasource, integration }) + await datasources.save({ datasource: updatedDatasource, integration }) notifications.success( `Datasource ${updatedDatasource.name} updated successfully` ) diff --git a/packages/builder/src/stores/builder/datasources.ts b/packages/builder/src/stores/builder/datasources.ts index e167b10c2c..6eeaeceaaf 100644 --- a/packages/builder/src/stores/builder/datasources.ts +++ b/packages/builder/src/stores/builder/datasources.ts @@ -1,4 +1,4 @@ -import { writable, derived, get } from "svelte/store" +import { derived, get } from "svelte/store" import { IntegrationTypes, DEFAULT_BB_DATASOURCE_ID, @@ -17,6 +17,7 @@ import { } from "@budibase/types" // @ts-ignore import { TableNames } from "constants" +import BudiStore from "stores/BudiStore" // when building the internal DS - seems to represent it slightly differently to the backend typing of a DS interface InternalDatasource extends Omit { @@ -41,102 +42,131 @@ class TableImportError extends Error { } } -interface DatasourceStore { +interface BuilderDatasourceStore { list: Datasource[] selectedDatasourceId: null | string } -export function createDatasourcesStore() { - const store = writable({ - list: [], - selectedDatasourceId: null, - }) +interface DerivedDatasourceStore extends Omit { + list: (Datasource | InternalDatasource)[] + selected?: Datasource | InternalDatasource + hasDefaultData: boolean + hasData: boolean +} - 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: Datasource | InternalDatasource | undefined = - $store.list?.find(ds => ds._id === BUDIBASE_INTERNAL_DB_ID) - let otherDS = $store.list?.filter(ds => ds._id !== BUDIBASE_INTERNAL_DB_ID) - if (internalDS) { - const tables: Table[] = $tables.list?.filter((table: Table) => { - return ( - table.sourceId === BUDIBASE_INTERNAL_DB_ID && - table._id !== TableNames.USERS - ) - }) - internalDS = { - ...internalDS, - entities: tables, +export class DatasourceStore extends BudiStore { + constructor() { + super({ + list: [], + selectedDatasourceId: null, + hasDefaultData: false, + hasData: false, + }) + + const derivedStore = derived< + [DatasourceStore, BudiStore], + DerivedDatasourceStore + >([this, tables as any], ([$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: Datasource | InternalDatasource | undefined = + $store.list?.find(ds => ds._id === BUDIBASE_INTERNAL_DB_ID) + let otherDS = $store.list?.filter( + ds => ds._id !== BUDIBASE_INTERNAL_DB_ID + ) + if (internalDS) { + const tables: Table[] = $tables.list?.filter((table: Table) => { + return ( + table.sourceId === BUDIBASE_INTERNAL_DB_ID && + table._id !== TableNames.USERS + ) + }) + internalDS = { + ...internalDS, + entities: tables, + } } - } - // Build up enriched DS list - // Only add the internal DS if we have at least one non-users table - let list: (InternalDatasource | Datasource)[] = [] - if (internalDS?.entities?.length) { - list.push(internalDS) - } - list = list.concat(otherDS || []) + // Build up enriched DS list + // Only add the internal DS if we have at least one non-users table + let list: (InternalDatasource | Datasource)[] = [] + 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(ds => ds._id === DEFAULT_BB_DATASOURCE_ID), - hasData: list?.length > 0, - } - }) + return { + ...$store, + list, + selected: list?.find(ds => ds._id === $store.selectedDatasourceId), + hasDefaultData: list?.some(ds => ds._id === DEFAULT_BB_DATASOURCE_ID), + hasData: list?.length > 0, + } + }) - const fetch = async () => { + this.fetch = this.fetch.bind(this) + this.init = this.fetch.bind(this) + this.select = this.select.bind(this) + this.updateSchema = this.updateSchema.bind(this) + this.create = this.create.bind(this) + this.delete = this.deleteDatasource.bind(this) + this.save = this.save.bind(this) + this.replaceDatasource = this.replaceDatasource.bind(this) + this.getTableNames = this.getTableNames.bind(this) + this.subscribe = derivedStore.subscribe + } + + async fetch() { const datasources = await API.getDatasources() - store.update(state => ({ + this.store.update(state => ({ ...state, list: datasources, })) } - const select = (id: string) => { - store.update(state => ({ + async init() { + return this.fetch() + } + + select(id: string) { + this.store.update(state => ({ ...state, selectedDatasourceId: id, })) } - const updateDatasource = ( + private updateDatasourceInStore( response: { datasource: Datasource; errors?: Record }, { ignoreErrors }: { ignoreErrors?: boolean } = {} - ) => { + ) { const { datasource, errors } = response if (!ignoreErrors && errors && Object.keys(errors).length > 0) { throw new TableImportError(errors) } - replaceDatasource(datasource._id!, datasource) - select(datasource._id!) + this.replaceDatasource(datasource._id!, datasource) + this.select(datasource._id!) return datasource } - const updateSchema = async ( - datasource: Datasource, - tablesFilter: string[] - ) => { + async updateSchema(datasource: Datasource, tablesFilter: string[]) { const response = await API.buildDatasourceSchema( datasource?._id!, tablesFilter ) - updateDatasource(response) + this.updateDatasourceInStore(response) } - const sourceCount = (source: string) => { - return get(store).list.filter(datasource => datasource.source === source) - .length + sourceCount(source: string) { + return get(this.store).list.filter( + datasource => datasource.source === source + ).length } - const checkDatasourceValidity = async ( + async checkDatasourceValidity( integration: Integration, datasource: Datasource - ): Promise<{ valid: boolean; error?: string }> => { + ): Promise<{ valid: boolean; error?: string }> { if (integration.features?.[DatasourceFeature.CONNECTION_CHECKING]) { const { connected, error } = await API.validateDatasource(datasource) if (connected) { @@ -148,14 +178,14 @@ export function createDatasourcesStore() { return { valid: true } } - const create = async ({ + async create({ integration, config, }: { integration: UIIntegration config: Record - }) => { - const count = sourceCount(integration.name) + }) { + const count = this.sourceCount(integration.name) const nameModifier = count === 0 ? "" : ` ${count + 1}` const datasource: Datasource = { @@ -167,7 +197,7 @@ export function createDatasourcesStore() { isSQL: integration.isSQL, } - const { valid, error } = await checkDatasourceValidity( + const { valid, error } = await this.checkDatasourceValidity( integration, datasource ) @@ -180,41 +210,45 @@ export function createDatasourcesStore() { fetchSchema: integration.plus, }) - return updateDatasource(response, { ignoreErrors: true }) + return this.updateDatasourceInStore(response, { ignoreErrors: true }) } - const update = async ({ + async save({ integration, datasource, }: { integration: Integration datasource: Datasource - }) => { - if (await checkDatasourceValidity(integration, datasource)) { + }) { + if (await this.checkDatasourceValidity(integration, datasource)) { throw new Error("Unable to connect") } const response = await API.updateDatasource(datasource) - return updateDatasource(response) + return this.updateDatasourceInStore(response) } - const deleteDatasource = async (datasource: Datasource) => { + async deleteDatasource(datasource: Datasource) { if (!datasource?._id || !datasource?._rev) { return } await API.deleteDatasource(datasource._id, datasource._rev) - replaceDatasource(datasource._id) + this.replaceDatasource(datasource._id) } - const replaceDatasource = (datasourceId: string, datasource?: Datasource) => { + async delete(datasource: Datasource) { + return this.deleteDatasource(datasource) + } + + replaceDatasource(datasourceId: string, datasource?: Datasource) { if (!datasourceId) { return } // Handle deletion if (!datasource) { - store.update(state => ({ + this.store.update(state => ({ ...state, list: state.list.filter(x => x._id !== datasourceId), })) @@ -224,9 +258,9 @@ export function createDatasourcesStore() { } // Add new datasource - const index = get(store).list.findIndex(x => x._id === datasource._id) + const index = get(this.store).list.findIndex(x => x._id === datasource._id) if (index === -1) { - store.update(state => ({ + this.store.update(state => ({ ...state, list: [...state.list, datasource], })) @@ -238,30 +272,21 @@ export function createDatasourcesStore() { // Update existing datasource else if (datasource) { - store.update(state => { + this.store.update(state => { state.list[index] = datasource return state }) } } - const getTableNames = async (datasource: Datasource) => { + async getTableNames(datasource: Datasource) { const info = await API.fetchInfoForDatasource(datasource) return info.tableNames || [] } - return { - subscribe: derivedStore.subscribe, - fetch, - init: fetch, - select, - updateSchema, - create, - update, - delete: deleteDatasource, - replaceDatasource, - getTableNames, - } + // subscribe() { + // return this.derivedStore.subscribe() + // } } -export const datasources = createDatasourcesStore() +export const datasources = new DatasourceStore() diff --git a/packages/builder/src/stores/builder/sortedIntegrations.ts b/packages/builder/src/stores/builder/sortedIntegrations.ts index bd8bb8154f..7872d74d7f 100644 --- a/packages/builder/src/stores/builder/sortedIntegrations.ts +++ b/packages/builder/src/stores/builder/sortedIntegrations.ts @@ -3,6 +3,7 @@ import { derived } from "svelte/store" import { DatasourceTypes } from "constants/backend" import { UIIntegration, Integration } from "@budibase/types" +import BudiStore from "stores/BudiStore" const getIntegrationOrder = (type: string | undefined) => { // if type is not known, sort to end @@ -18,29 +19,35 @@ const getIntegrationOrder = (type: string | undefined) => { return type.charCodeAt(0) + 4 } -export const createSortedIntegrationsStore = () => { - return derived( - integrations, - $integrations => { - const entries: [string, Integration][] = Object.entries($integrations) - const integrationsAsArray = entries.map(([name, integration]) => ({ - name, - ...integration, - })) +class SortedIntegrationStore extends BudiStore { + constructor() { + super([]) - return integrationsAsArray.sort((integrationA, integrationB) => { - const integrationASortOrder = getIntegrationOrder(integrationA.type) - const integrationBSortOrder = getIntegrationOrder(integrationB.type) - if (integrationASortOrder === integrationBSortOrder) { - return integrationA.friendlyName.localeCompare( - integrationB.friendlyName - ) - } + const derivedStore = derived( + integrations, + $integrations => { + const entries: [string, Integration][] = Object.entries($integrations) + const integrationsAsArray = entries.map(([name, integration]) => ({ + name, + ...integration, + })) - return integrationASortOrder < integrationBSortOrder ? -1 : 1 - }) - } - ) + return integrationsAsArray.sort((integrationA, integrationB) => { + const integrationASortOrder = getIntegrationOrder(integrationA.type) + const integrationBSortOrder = getIntegrationOrder(integrationB.type) + if (integrationASortOrder === integrationBSortOrder) { + return integrationA.friendlyName.localeCompare( + integrationB.friendlyName + ) + } + + return integrationASortOrder < integrationBSortOrder ? -1 : 1 + }) + } + ) + + this.subscribe = derivedStore.subscribe + } } -export const sortedIntegrations = createSortedIntegrationsStore() +export const sortedIntegrations = new SortedIntegrationStore() From 33c1f7364da9a926570a32f53b804345855cb06e Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Tue, 17 Dec 2024 17:16:44 +0000 Subject: [PATCH 2/8] Updating test. --- .../src/stores/builder/sortedIntegrations.ts | 2 +- ...ons.test.js => sortedIntegrations.test.ts} | 33 +++++++++++-------- 2 files changed, 21 insertions(+), 14 deletions(-) rename packages/builder/src/stores/builder/tests/{sortedIntegrations.test.js => sortedIntegrations.test.ts} (79%) diff --git a/packages/builder/src/stores/builder/sortedIntegrations.ts b/packages/builder/src/stores/builder/sortedIntegrations.ts index 7872d74d7f..2c12e61b7f 100644 --- a/packages/builder/src/stores/builder/sortedIntegrations.ts +++ b/packages/builder/src/stores/builder/sortedIntegrations.ts @@ -19,7 +19,7 @@ const getIntegrationOrder = (type: string | undefined) => { return type.charCodeAt(0) + 4 } -class SortedIntegrationStore extends BudiStore { +export class SortedIntegrationStore extends BudiStore { constructor() { super([]) diff --git a/packages/builder/src/stores/builder/tests/sortedIntegrations.test.js b/packages/builder/src/stores/builder/tests/sortedIntegrations.test.ts similarity index 79% rename from packages/builder/src/stores/builder/tests/sortedIntegrations.test.js rename to packages/builder/src/stores/builder/tests/sortedIntegrations.test.ts index d57aa19148..6bab3ad6db 100644 --- a/packages/builder/src/stores/builder/tests/sortedIntegrations.test.js +++ b/packages/builder/src/stores/builder/tests/sortedIntegrations.test.ts @@ -1,12 +1,14 @@ import { it, expect, describe, beforeEach, vi } from "vitest" -import { createSortedIntegrationsStore } from "stores/builder/sortedIntegrations" +import { SortedIntegrationStore } from "stores/builder/sortedIntegrations" import { DatasourceTypes } from "constants/backend" import { derived } from "svelte/store" import { integrations } from "stores/builder/integrations" vi.mock("svelte/store", () => ({ - derived: vi.fn(), + derived: vi.fn(() => ({ + subscribe: vi.fn(), + })), writable: vi.fn(() => ({ subscribe: vi.fn(), })), @@ -14,6 +16,8 @@ vi.mock("svelte/store", () => ({ vi.mock("stores/builder/integrations", () => ({ integrations: vi.fn() })) +const mockedDerived = vi.mocked(derived) + const inputA = { nonRelationalA: { friendlyName: "non-relational A", @@ -104,25 +108,28 @@ const expectedOutput = [ ] describe("sorted integrations store", () => { - beforeEach(ctx => { + interface LocalContext { + returnedStore: SortedIntegrationStore + derivedCallback: any + } + + beforeEach(ctx => { vi.clearAllMocks() - ctx.returnedStore = createSortedIntegrationsStore() - - ctx.derivedCallback = derived.mock.calls[0][1] + ctx.returnedStore = new SortedIntegrationStore() + ctx.derivedCallback = mockedDerived.mock.calls[0]?.[1] }) it("calls derived with the correct parameters", () => { - expect(derived).toHaveBeenCalledTimes(1) - expect(derived).toHaveBeenCalledWith(integrations, expect.toBeFunc()) + expect(mockedDerived).toHaveBeenCalledTimes(1) + expect(mockedDerived).toHaveBeenCalledWith( + integrations, + expect.any(Function) + ) }) describe("derived callback", () => { - it("When no integrations are loaded", ctx => { - expect(ctx.derivedCallback({})).toEqual([]) - }) - - it("When integrations are present", ctx => { + it("When integrations are present", ctx => { expect(ctx.derivedCallback(inputA)).toEqual(expectedOutput) expect(ctx.derivedCallback(inputB)).toEqual(expectedOutput) }) From 84a67bb3460a04999e62cdba9b5431eb0c9720c9 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 17 Dec 2024 18:42:19 +0100 Subject: [PATCH 3/8] Add test capturing --- .../server/src/api/routes/tests/row.spec.ts | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/packages/server/src/api/routes/tests/row.spec.ts b/packages/server/src/api/routes/tests/row.spec.ts index fb728a3fea..a3012c3760 100644 --- a/packages/server/src/api/routes/tests/row.spec.ts +++ b/packages/server/src/api/routes/tests/row.spec.ts @@ -1333,6 +1333,62 @@ if (descriptions.length) { expect(resp.relationship.length).toBe(1) }) + it("should be able to keep linked data when updating from views that trims links from the main table", async () => { + let row = await config.api.row.save(table._id!, { + name: "main", + description: "main description", + }) + const row2 = await config.api.row.save(otherTable._id!, { + name: "link", + description: "link description", + relationship: [row._id], + }) + + const view = await config.api.viewV2.create({ + tableId: table._id!, + name: "view", + schema: { + name: { visible: true }, + }, + }) + const resp = await config.api.row.patch(view.id, { + _id: row._id!, + _rev: row._rev!, + tableId: row.tableId!, + name: "test2", + relationship: [row2._id], + }) + expect(resp.relationship).toBeUndefined() + + const updatedRow = await config.api.row.get(table._id!, row._id!) + expect(updatedRow.relationship.length).toBe(1) + }) + + it("should be able to keep linked data when updating from views that trims links from the foreign table", async () => { + let row = await config.api.row.save(table._id!, { + name: "main", + description: "main description", + }) + const row2 = await config.api.row.save(otherTable._id!, { + name: "link", + description: "link description", + relationship: [row._id], + }) + + const view = await config.api.viewV2.create({ + tableId: otherTable._id!, + name: "view", + }) + await config.api.row.patch(view.id, { + _id: row2._id!, + _rev: row2._rev!, + tableId: row2.tableId!, + }) + + const updatedRow = await config.api.row.get(table._id!, row._id!) + expect(updatedRow.relationship.length).toBe(1) + }) + !isInternal && // MSSQL needs a setting called IDENTITY_INSERT to be set to ON to allow writing // to identity columns. This is not something Budibase does currently. From d061f44eda07dd1e08129da983edd5a5218299a7 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 17 Dec 2024 18:50:17 +0100 Subject: [PATCH 4/8] Fix external patches --- .../src/api/controllers/row/external.ts | 20 +++++++++++++------ .../src/api/controllers/row/utils/utils.ts | 15 ++++++++++++++ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/packages/server/src/api/controllers/row/external.ts b/packages/server/src/api/controllers/row/external.ts index 082d07283b..d56fb1a344 100644 --- a/packages/server/src/api/controllers/row/external.ts +++ b/packages/server/src/api/controllers/row/external.ts @@ -52,10 +52,22 @@ export async function patch(ctx: UserCtx) { const table = await utils.getTableFromSource(source) const { _id, ...rowData } = ctx.request.body - const dataToUpdate = await inputProcessing( + const beforeRow = await sdk.rows.external.getRow(table._id!, _id, { + relationships: true, + }) + + let dataToUpdate = cloneDeep(beforeRow) + const allowedField = utils.getSourceFields(source) + for (const key of Object.keys(rowData)) { + if (!allowedField.includes(key)) continue + + dataToUpdate[key] = rowData[key] + } + + dataToUpdate = await inputProcessing( ctx.user?._id, cloneDeep(source), - rowData + dataToUpdate ) const validateResult = await sdk.rows.utils.validate({ @@ -66,10 +78,6 @@ export async function patch(ctx: UserCtx) { throw { validation: validateResult.errors } } - const beforeRow = await sdk.rows.external.getRow(table._id!, _id, { - relationships: true, - }) - const response = await handleRequest(Operation.UPDATE, source, { id: breakRowIdField(_id), row: dataToUpdate, diff --git a/packages/server/src/api/controllers/row/utils/utils.ts b/packages/server/src/api/controllers/row/utils/utils.ts index baa811fe90..9d1711d000 100644 --- a/packages/server/src/api/controllers/row/utils/utils.ts +++ b/packages/server/src/api/controllers/row/utils/utils.ts @@ -110,6 +110,21 @@ function fixBooleanFields(row: Row, table: Table) { return row } +export function getSourceFields(source: Table | ViewV2): string[] { + const isView = sdk.views.isView(source) + if (isView) { + const fields = Object.keys( + helpers.views.basicFields(source, { visible: true }) + ) + return fields + } + + const fields = Object.entries(source.schema) + .filter(([_, field]) => field.visible !== false) + .map(([columnName]) => columnName) + return fields +} + export async function sqlOutputProcessing( rows: DatasourcePlusQueryResponse, source: Table | ViewV2, From f3caba81763933c8bb3af54487607bf3a86bcfae Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Wed, 18 Dec 2024 15:24:37 +0000 Subject: [PATCH 5/8] Fix onboarding users --- .../_components/BuilderSidePanel.svelte | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/builder/src/pages/builder/app/[application]/_components/BuilderSidePanel.svelte b/packages/builder/src/pages/builder/app/[application]/_components/BuilderSidePanel.svelte index 687678c08a..b80d3137f0 100644 --- a/packages/builder/src/pages/builder/app/[application]/_components/BuilderSidePanel.svelte +++ b/packages/builder/src/pages/builder/app/[application]/_components/BuilderSidePanel.svelte @@ -368,20 +368,22 @@ const payload = [ { email: newUserEmail, - builder: { - global: creationRoleType === Constants.BudibaseRoles.Admin, - creator: creationRoleType === Constants.BudibaseRoles.Creator, + userInfo: { + builder: { + global: creationRoleType === Constants.BudibaseRoles.Admin, + creator: creationRoleType === Constants.BudibaseRoles.Creator, + }, + admin: { global: creationRoleType === Constants.BudibaseRoles.Admin }, }, - admin: { global: creationRoleType === Constants.BudibaseRoles.Admin }, }, ] const notCreatingAdmin = creationRoleType !== Constants.BudibaseRoles.Admin const isCreator = creationAccessType === Constants.Roles.CREATOR if (notCreatingAdmin && isCreator) { - payload[0].builder.apps = [prodAppId] + payload[0].userInfo.builder.apps = [prodAppId] } else if (notCreatingAdmin && !isCreator) { - payload[0].apps = { [prodAppId]: creationAccessType } + payload[0].userInfo.apps = { [prodAppId]: creationAccessType } } let userInviteResponse From fd388c3d36e037f2725b3069b35f71aea90035de Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Wed, 18 Dec 2024 15:43:05 +0000 Subject: [PATCH 6/8] Fix datasource validity checking --- packages/builder/src/stores/builder/datasources.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/builder/src/stores/builder/datasources.ts b/packages/builder/src/stores/builder/datasources.ts index 6eeaeceaaf..e02774f1d5 100644 --- a/packages/builder/src/stores/builder/datasources.ts +++ b/packages/builder/src/stores/builder/datasources.ts @@ -220,7 +220,7 @@ export class DatasourceStore extends BudiStore { integration: Integration datasource: Datasource }) { - if (await this.checkDatasourceValidity(integration, datasource)) { + if (!(await this.checkDatasourceValidity(integration, datasource)).valid) { throw new Error("Unable to connect") } From 9650b388c4b6440ed9de4c73b3fd6029962205cd Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Wed, 18 Dec 2024 16:45:28 +0000 Subject: [PATCH 7/8] Refactor derived BudiStores --- packages/builder/src/stores/BudiStore.ts | 41 +++--- packages/builder/src/stores/builder/app.js | 2 +- .../builder/src/stores/builder/builder.js | 2 +- .../builder/src/stores/builder/components.js | 2 +- .../builder/src/stores/builder/datasources.ts | 132 +++++++++--------- packages/builder/src/stores/builder/hover.js | 2 +- .../builder/src/stores/builder/layouts.js | 2 +- .../builder/src/stores/builder/navigation.js | 2 +- .../builder/src/stores/builder/rowActions.js | 2 +- .../builder/src/stores/builder/screens.js | 2 +- .../src/stores/builder/sortedIntegrations.ts | 2 +- packages/builder/src/stores/portal/apps.ts | 2 +- .../builder/src/stores/portal/auditLogs.ts | 2 +- packages/builder/src/stores/portal/auth.ts | 2 +- 14 files changed, 97 insertions(+), 100 deletions(-) diff --git a/packages/builder/src/stores/BudiStore.ts b/packages/builder/src/stores/BudiStore.ts index c645ea6a24..706fa9474a 100644 --- a/packages/builder/src/stores/BudiStore.ts +++ b/packages/builder/src/stores/BudiStore.ts @@ -1,40 +1,22 @@ -import { writable, Writable } from "svelte/store" +import { writable, Writable, Readable } from "svelte/store" interface BudiStoreOpts { debug?: boolean } -export default class BudiStore implements Writable { +export class BudiStore { store: Writable subscribe: Writable["subscribe"] update: Writable["update"] set: Writable["set"] constructor(init: T, opts?: BudiStoreOpts) { - const store = writable(init) - - /** - * Internal Svelte store - */ - this.store = store - - /** - * Exposes the svelte subscribe fn to allow $ notation access - * @example - * $navigation.selectedScreenId - */ + this.store = writable(init) this.subscribe = this.store.subscribe - - /** - * Exposes the svelte update fn. - * *Store modification should be kept to a minimum - */ this.update = this.store.update this.set = this.store.set - /** - * Optional debug mode to output the store updates to console - */ + // Optional debug mode to output the store updates to console if (opts?.debug) { this.subscribe(state => { console.warn(`${this.constructor.name}`, state) @@ -42,3 +24,18 @@ export default class BudiStore implements Writable { } } } + +export class DerivedBudiStore extends BudiStore { + derivedStore: Readable + subscribe: Readable["subscribe"] + + constructor( + init: T, + makeDerivedStore: (store: Writable) => Readable, + opts?: BudiStoreOpts + ) { + super(init, opts) + this.derivedStore = makeDerivedStore(this.store) + this.subscribe = this.derivedStore.subscribe + } +} diff --git a/packages/builder/src/stores/builder/app.js b/packages/builder/src/stores/builder/app.js index e57a079185..3b9e4e0b3c 100644 --- a/packages/builder/src/stores/builder/app.js +++ b/packages/builder/src/stores/builder/app.js @@ -1,5 +1,5 @@ import { API } from "api" -import BudiStore from "../BudiStore" +import { BudiStore } from "../BudiStore" export const INITIAL_APP_META_STATE = { appId: "", diff --git a/packages/builder/src/stores/builder/builder.js b/packages/builder/src/stores/builder/builder.js index d002062da9..9b5a847680 100644 --- a/packages/builder/src/stores/builder/builder.js +++ b/packages/builder/src/stores/builder/builder.js @@ -1,7 +1,7 @@ import { get } from "svelte/store" import { createBuilderWebsocket } from "./websocket.js" import { BuilderSocketEvent } from "@budibase/shared-core" -import BudiStore from "../BudiStore.js" +import { BudiStore } from "../BudiStore.js" import { TOUR_KEYS } from "components/portal/onboarding/tours.js" export const INITIAL_BUILDER_STATE = { diff --git a/packages/builder/src/stores/builder/components.js b/packages/builder/src/stores/builder/components.js index faa6a086ca..e3fafd6f83 100644 --- a/packages/builder/src/stores/builder/components.js +++ b/packages/builder/src/stores/builder/components.js @@ -28,7 +28,7 @@ import { DB_TYPE_INTERNAL, DB_TYPE_EXTERNAL, } from "constants/backend" -import BudiStore from "../BudiStore" +import { BudiStore } from "../BudiStore" import { Utils } from "@budibase/frontend-core" import { FieldType } from "@budibase/types" import { utils } from "@budibase/shared-core" diff --git a/packages/builder/src/stores/builder/datasources.ts b/packages/builder/src/stores/builder/datasources.ts index e02774f1d5..6d94c1b26f 100644 --- a/packages/builder/src/stores/builder/datasources.ts +++ b/packages/builder/src/stores/builder/datasources.ts @@ -1,4 +1,4 @@ -import { derived, get } from "svelte/store" +import { derived, get, Writable } from "svelte/store" import { IntegrationTypes, DEFAULT_BB_DATASOURCE_ID, @@ -17,12 +17,7 @@ import { } from "@budibase/types" // @ts-ignore import { TableNames } from "constants" -import BudiStore from "stores/BudiStore" - -// when building the internal DS - seems to represent it slightly differently to the backend typing of a DS -interface InternalDatasource extends Omit { - entities: Table[] -} +import { DerivedBudiStore } from "stores/BudiStore" class TableImportError extends Error { errors: Record @@ -42,68 +37,76 @@ class TableImportError extends Error { } } +// when building the internal DS - seems to represent it slightly differently to the backend typing of a DS +interface InternalDatasource extends Omit { + entities: Table[] +} + interface BuilderDatasourceStore { - list: Datasource[] + datasources: Datasource[] selectedDatasourceId: null | string } -interface DerivedDatasourceStore extends Omit { +interface DerivedDatasourceStore extends BuilderDatasourceStore { list: (Datasource | InternalDatasource)[] selected?: Datasource | InternalDatasource hasDefaultData: boolean hasData: boolean } -export class DatasourceStore extends BudiStore { +export class DatasourceStore extends DerivedBudiStore< + BuilderDatasourceStore, + DerivedDatasourceStore +> { constructor() { - super({ - list: [], - selectedDatasourceId: null, - hasDefaultData: false, - hasData: false, - }) - - const derivedStore = derived< - [DatasourceStore, BudiStore], - DerivedDatasourceStore - >([this, tables as any], ([$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: Datasource | InternalDatasource | undefined = - $store.list?.find(ds => ds._id === BUDIBASE_INTERNAL_DB_ID) - let otherDS = $store.list?.filter( - ds => ds._id !== BUDIBASE_INTERNAL_DB_ID - ) - if (internalDS) { - const tables: Table[] = $tables.list?.filter((table: Table) => { - return ( - table.sourceId === BUDIBASE_INTERNAL_DB_ID && - table._id !== TableNames.USERS - ) - }) - internalDS = { - ...internalDS, - entities: tables, + const makeDerivedStore = (store: Writable) => { + return 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: Datasource | InternalDatasource | undefined = + $store.datasources?.find(ds => ds._id === BUDIBASE_INTERNAL_DB_ID) + let otherDS = $store.datasources?.filter( + ds => ds._id !== BUDIBASE_INTERNAL_DB_ID + ) + if (internalDS) { + const tables: Table[] = $tables.list?.filter((table: Table) => { + return ( + table.sourceId === BUDIBASE_INTERNAL_DB_ID && + table._id !== TableNames.USERS + ) + }) + internalDS = { + ...internalDS, + entities: tables, + } } - } - // Build up enriched DS list - // Only add the internal DS if we have at least one non-users table - let list: (InternalDatasource | Datasource)[] = [] - if (internalDS?.entities?.length) { - list.push(internalDS) - } - list = list.concat(otherDS || []) + // Build up enriched DS list + // Only add the internal DS if we have at least one non-users table + let list: (InternalDatasource | Datasource)[] = [] + 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(ds => ds._id === DEFAULT_BB_DATASOURCE_ID), - hasData: list?.length > 0, - } - }) + return { + ...$store, + list, + selected: list?.find(ds => ds._id === $store.selectedDatasourceId), + hasDefaultData: list?.some(ds => ds._id === DEFAULT_BB_DATASOURCE_ID), + hasData: list?.length > 0, + } + }) + } + + super( + { + datasources: [], + selectedDatasourceId: null, + }, + makeDerivedStore + ) this.fetch = this.fetch.bind(this) this.init = this.fetch.bind(this) @@ -114,14 +117,13 @@ export class DatasourceStore extends BudiStore { this.save = this.save.bind(this) this.replaceDatasource = this.replaceDatasource.bind(this) this.getTableNames = this.getTableNames.bind(this) - this.subscribe = derivedStore.subscribe } async fetch() { const datasources = await API.getDatasources() this.store.update(state => ({ ...state, - list: datasources, + datasources, })) } @@ -158,7 +160,7 @@ export class DatasourceStore extends BudiStore { } sourceCount(source: string) { - return get(this.store).list.filter( + return get(this.store).datasources.filter( datasource => datasource.source === source ).length } @@ -250,7 +252,7 @@ export class DatasourceStore extends BudiStore { if (!datasource) { this.store.update(state => ({ ...state, - list: state.list.filter(x => x._id !== datasourceId), + datasources: state.datasources.filter(x => x._id !== datasourceId), })) tables.removeDatasourceTables(datasourceId) queries.removeDatasourceQueries(datasourceId) @@ -258,11 +260,13 @@ export class DatasourceStore extends BudiStore { } // Add new datasource - const index = get(this.store).list.findIndex(x => x._id === datasource._id) + const index = get(this.store).datasources.findIndex( + x => x._id === datasource._id + ) if (index === -1) { this.store.update(state => ({ ...state, - list: [...state.list, datasource], + datasources: [...state.datasources, datasource], })) // If this is a new datasource then we should refresh the tables list, @@ -273,7 +277,7 @@ export class DatasourceStore extends BudiStore { // Update existing datasource else if (datasource) { this.store.update(state => { - state.list[index] = datasource + state.datasources[index] = datasource return state }) } @@ -283,10 +287,6 @@ export class DatasourceStore extends BudiStore { const info = await API.fetchInfoForDatasource(datasource) return info.tableNames || [] } - - // subscribe() { - // return this.derivedStore.subscribe() - // } } export const datasources = new DatasourceStore() diff --git a/packages/builder/src/stores/builder/hover.js b/packages/builder/src/stores/builder/hover.js index 98cdc9e416..8da7191cf5 100644 --- a/packages/builder/src/stores/builder/hover.js +++ b/packages/builder/src/stores/builder/hover.js @@ -1,6 +1,6 @@ import { get } from "svelte/store" import { previewStore } from "stores/builder" -import BudiStore from "../BudiStore" +import { BudiStore } from "../BudiStore" export const INITIAL_HOVER_STATE = { componentId: null, diff --git a/packages/builder/src/stores/builder/layouts.js b/packages/builder/src/stores/builder/layouts.js index b105989746..6b68850a93 100644 --- a/packages/builder/src/stores/builder/layouts.js +++ b/packages/builder/src/stores/builder/layouts.js @@ -1,6 +1,6 @@ import { derived, get } from "svelte/store" import { componentStore } from "stores/builder" -import BudiStore from "../BudiStore" +import { BudiStore } from "../BudiStore" import { API } from "api" export const INITIAL_LAYOUT_STATE = { diff --git a/packages/builder/src/stores/builder/navigation.js b/packages/builder/src/stores/builder/navigation.js index c3e19a4327..abdd9638f3 100644 --- a/packages/builder/src/stores/builder/navigation.js +++ b/packages/builder/src/stores/builder/navigation.js @@ -1,7 +1,7 @@ import { get } from "svelte/store" import { API } from "api" import { appStore } from "stores/builder" -import BudiStore from "../BudiStore" +import { BudiStore } from "../BudiStore" export const INITIAL_NAVIGATION_STATE = { navigation: "Top", diff --git a/packages/builder/src/stores/builder/rowActions.js b/packages/builder/src/stores/builder/rowActions.js index a7ed45e707..553a574d53 100644 --- a/packages/builder/src/stores/builder/rowActions.js +++ b/packages/builder/src/stores/builder/rowActions.js @@ -1,5 +1,5 @@ import { get, derived } from "svelte/store" -import BudiStore from "stores/BudiStore" +import { BudiStore } from "stores/BudiStore" import { tables } from "./tables" import { viewsV2 } from "./viewsV2" import { automationStore } from "./automations" diff --git a/packages/builder/src/stores/builder/screens.js b/packages/builder/src/stores/builder/screens.js index 55fa3d5433..4e3d9b1ec6 100644 --- a/packages/builder/src/stores/builder/screens.js +++ b/packages/builder/src/stores/builder/screens.js @@ -12,7 +12,7 @@ import { } from "stores/builder" import { createHistoryStore } from "stores/builder/history" import { API } from "api" -import BudiStore from "../BudiStore" +import { BudiStore } from "../BudiStore" export const INITIAL_SCREENS_STATE = { screens: [], diff --git a/packages/builder/src/stores/builder/sortedIntegrations.ts b/packages/builder/src/stores/builder/sortedIntegrations.ts index 2c12e61b7f..76044af331 100644 --- a/packages/builder/src/stores/builder/sortedIntegrations.ts +++ b/packages/builder/src/stores/builder/sortedIntegrations.ts @@ -3,7 +3,7 @@ import { derived } from "svelte/store" import { DatasourceTypes } from "constants/backend" import { UIIntegration, Integration } from "@budibase/types" -import BudiStore from "stores/BudiStore" +import { BudiStore } from "stores/BudiStore" const getIntegrationOrder = (type: string | undefined) => { // if type is not known, sort to end diff --git a/packages/builder/src/stores/portal/apps.ts b/packages/builder/src/stores/portal/apps.ts index 867a554f00..f74ae4bfe2 100644 --- a/packages/builder/src/stores/portal/apps.ts +++ b/packages/builder/src/stores/portal/apps.ts @@ -3,7 +3,7 @@ import { derived } from "svelte/store" import { AppStatus } from "constants" import { API } from "api" import { auth } from "./auth" -import BudiStore from "../BudiStore" +import { BudiStore } from "../BudiStore" import { App, UpdateAppRequest } from "@budibase/types" interface AppIdentifierMetadata { diff --git a/packages/builder/src/stores/portal/auditLogs.ts b/packages/builder/src/stores/portal/auditLogs.ts index 10d79120ee..e97b301360 100644 --- a/packages/builder/src/stores/portal/auditLogs.ts +++ b/packages/builder/src/stores/portal/auditLogs.ts @@ -1,7 +1,7 @@ import { get } from "svelte/store" import { API } from "api" import { licensing } from "./licensing" -import BudiStore from "../BudiStore" +import { BudiStore } from "../BudiStore" import { DownloadAuditLogsRequest, SearchAuditLogsRequest, diff --git a/packages/builder/src/stores/portal/auth.ts b/packages/builder/src/stores/portal/auth.ts index 1f9646dd56..2affea85a7 100644 --- a/packages/builder/src/stores/portal/auth.ts +++ b/packages/builder/src/stores/portal/auth.ts @@ -2,7 +2,7 @@ import { get } from "svelte/store" import { API } from "api" import { admin } from "stores/portal" import analytics from "analytics" -import BudiStore from "stores/BudiStore" +import { BudiStore } from "stores/BudiStore" import { isSSOUser, SetInitInfoRequest, From 54c97186dfdcc0407a706adb211c92273ec9faea Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Wed, 18 Dec 2024 16:53:09 +0000 Subject: [PATCH 8/8] Rename datasources to rawList --- .../builder/src/stores/builder/datasources.ts | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/builder/src/stores/builder/datasources.ts b/packages/builder/src/stores/builder/datasources.ts index 6d94c1b26f..54e9f85dbd 100644 --- a/packages/builder/src/stores/builder/datasources.ts +++ b/packages/builder/src/stores/builder/datasources.ts @@ -43,7 +43,7 @@ interface InternalDatasource extends Omit { } interface BuilderDatasourceStore { - datasources: Datasource[] + rawList: Datasource[] selectedDatasourceId: null | string } @@ -65,8 +65,8 @@ export class DatasourceStore extends DerivedBudiStore< // able to keep updated unlike the egress generated definition of the // internal datasource let internalDS: Datasource | InternalDatasource | undefined = - $store.datasources?.find(ds => ds._id === BUDIBASE_INTERNAL_DB_ID) - let otherDS = $store.datasources?.filter( + $store.rawList?.find(ds => ds._id === BUDIBASE_INTERNAL_DB_ID) + let otherDS = $store.rawList?.filter( ds => ds._id !== BUDIBASE_INTERNAL_DB_ID ) if (internalDS) { @@ -102,7 +102,7 @@ export class DatasourceStore extends DerivedBudiStore< super( { - datasources: [], + rawList: [], selectedDatasourceId: null, }, makeDerivedStore @@ -123,7 +123,7 @@ export class DatasourceStore extends DerivedBudiStore< const datasources = await API.getDatasources() this.store.update(state => ({ ...state, - datasources, + rawList: datasources, })) } @@ -160,7 +160,7 @@ export class DatasourceStore extends DerivedBudiStore< } sourceCount(source: string) { - return get(this.store).datasources.filter( + return get(this.store).rawList.filter( datasource => datasource.source === source ).length } @@ -252,7 +252,7 @@ export class DatasourceStore extends DerivedBudiStore< if (!datasource) { this.store.update(state => ({ ...state, - datasources: state.datasources.filter(x => x._id !== datasourceId), + rawList: state.rawList.filter(x => x._id !== datasourceId), })) tables.removeDatasourceTables(datasourceId) queries.removeDatasourceQueries(datasourceId) @@ -260,13 +260,13 @@ export class DatasourceStore extends DerivedBudiStore< } // Add new datasource - const index = get(this.store).datasources.findIndex( + const index = get(this.store).rawList.findIndex( x => x._id === datasource._id ) if (index === -1) { this.store.update(state => ({ ...state, - datasources: [...state.datasources, datasource], + rawList: [...state.rawList, datasource], })) // If this is a new datasource then we should refresh the tables list, @@ -277,7 +277,7 @@ export class DatasourceStore extends DerivedBudiStore< // Update existing datasource else if (datasource) { this.store.update(state => { - state.datasources[index] = datasource + state.rawList[index] = datasource return state }) }