From 590ba5de60ca9c23ae572aa8fbc80745ae24bc66 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Tue, 17 Dec 2024 16:31:40 +0000 Subject: [PATCH 1/9] 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/9] 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 15f2aaf950b15e15dcbae5fed8f04e3306018554 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 16 Dec 2024 16:55:38 +0100 Subject: [PATCH 3/9] Fix encodings --- packages/backend-core/src/sql/utils.ts | 4 ++++ packages/server/src/api/controllers/row/utils/utils.ts | 2 +- packages/server/src/db/utils.ts | 10 ++-------- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/packages/backend-core/src/sql/utils.ts b/packages/backend-core/src/sql/utils.ts index 14127a189f..16b352995b 100644 --- a/packages/backend-core/src/sql/utils.ts +++ b/packages/backend-core/src/sql/utils.ts @@ -70,6 +70,10 @@ export function encodeTableId(tableId: string) { } } +export function encodeViewId(viewId: string) { + return encodeURIComponent(viewId) +} + export function breakExternalTableId(tableId: string) { const parts = tableId.split(DOUBLE_SEPARATOR) let datasourceId = parts.shift() diff --git a/packages/server/src/api/controllers/row/utils/utils.ts b/packages/server/src/api/controllers/row/utils/utils.ts index baa811fe90..86986b64e8 100644 --- a/packages/server/src/api/controllers/row/utils/utils.ts +++ b/packages/server/src/api/controllers/row/utils/utils.ts @@ -66,7 +66,7 @@ export function getSourceId(ctx: Ctx): { tableId: string; viewId?: string } { if (docIds.isViewId(sourceId)) { return { tableId: utils.extractViewInfoFromID(sourceId).tableId, - viewId: sourceId, + viewId: sql.utils.encodeViewId(sourceId), } } return { tableId: sql.utils.encodeTableId(ctx.params.sourceId) } diff --git a/packages/server/src/db/utils.ts b/packages/server/src/db/utils.ts index 70c69b3c60..6c1065e847 100644 --- a/packages/server/src/db/utils.ts +++ b/packages/server/src/db/utils.ts @@ -1,10 +1,4 @@ -import { - context, - db as dbCore, - docIds, - utils, - sql, -} from "@budibase/backend-core" +import { context, db as dbCore, docIds, utils } from "@budibase/backend-core" import { DatabaseQueryOpts, Datasource, @@ -334,7 +328,7 @@ export function extractViewInfoFromID(viewId: string) { const regex = new RegExp(`^(?.+)${SEPARATOR}([^${SEPARATOR}]+)$`) const res = regex.exec(viewId) return { - tableId: sql.utils.encodeTableId(res!.groups!["tableId"]), + tableId: res!.groups!["tableId"], } } From 1349634d9d3261965e8110bf434ecccf120585e6 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 18 Dec 2024 10:22:17 +0100 Subject: [PATCH 4/9] Build builder in dev mode while on dev --- packages/builder/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/builder/package.json b/packages/builder/package.json index 485cced7ab..f2a829d5a9 100644 --- a/packages/builder/package.json +++ b/packages/builder/package.json @@ -7,7 +7,7 @@ "build": "routify -b && NODE_OPTIONS=\"--max_old_space_size=4096\" vite build --emptyOutDir", "start": "routify -c rollup", "dev": "routify -c dev:vite", - "dev:vite": "vite --host 0.0.0.0", + "dev:vite": "vite --host 0.0.0.0 --mode=dev", "rollup": "rollup -c -w", "test": "vitest run", "test:watch": "vitest", From b538125d1faf0af2ecf67e6600e825ba29eb9a18 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 18 Dec 2024 10:40:12 +0100 Subject: [PATCH 5/9] Add failing test --- .../src/api/routes/tests/viewV2.spec.ts | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index 244a0a23eb..632dbfec3a 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -55,7 +55,7 @@ if (descriptions.length) { let datasource: Datasource | undefined function saveTableRequest( - ...overrides: Partial>[] + ...overrides: Partial[] ): SaveTableRequest { const req: SaveTableRequest = { name: generator.guid().replaceAll("-", "").substring(0, 16), @@ -1898,6 +1898,36 @@ if (descriptions.length) { } expect(view.queryUI).toEqual(expected) }) + + it("tables and views can contain whitespaces", async () => { + const table = await config.api.table.save( + saveTableRequest({ + name: "table with spaces", + schema: { + name: { + type: FieldType.STRING, + name: "name", + }, + }, + }) + ) + + const view = await config.api.viewV2.create({ + tableId: table._id!, + name: `view name with spaces`, + schema: { + name: { visible: true }, + }, + }) + + expect(await getDelegate(view)).toEqual({ + ...view, + schema: { + id: { ...table.schema["id"], visible: false }, + name: { ...table.schema["name"], visible: true }, + }, + }) + }) }) describe("updating table schema", () => { From b81e43ed59855c15abce129894eeabf1637e7ba8 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 18 Dec 2024 10:55:32 +0100 Subject: [PATCH 6/9] Fix tests --- packages/server/src/api/routes/tests/viewV2.spec.ts | 2 +- packages/server/src/tests/utilities/api/viewV2.ts | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index 632dbfec3a..ba2ad422eb 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -1902,7 +1902,7 @@ if (descriptions.length) { it("tables and views can contain whitespaces", async () => { const table = await config.api.table.save( saveTableRequest({ - name: "table with spaces", + name: `table with spaces ${generator.hash()}`, schema: { name: { type: FieldType.STRING, diff --git a/packages/server/src/tests/utilities/api/viewV2.ts b/packages/server/src/tests/utilities/api/viewV2.ts index 7cc57673a0..d0350a5521 100644 --- a/packages/server/src/tests/utilities/api/viewV2.ts +++ b/packages/server/src/tests/utilities/api/viewV2.ts @@ -46,8 +46,11 @@ export class ViewV2API extends TestAPI { } get = async (viewId: string) => { - return (await this._get(`/api/v2/views/${viewId}`)) - .data + return ( + await this._get( + `/api/v2/views/${encodeURIComponent(viewId)}` + ) + ).data } fetch = async (expectations?: Expectations) => { From 6586b47c48ba6a96e9f865f2a25acdce2c3d0933 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Wed, 18 Dec 2024 10:32:18 +0000 Subject: [PATCH 7/9] Fix searching relationsgips --- .../src/components/grid/cells/RelationshipCell.svelte | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/frontend-core/src/components/grid/cells/RelationshipCell.svelte b/packages/frontend-core/src/components/grid/cells/RelationshipCell.svelte index 92186f19b1..bda0f5fd5d 100644 --- a/packages/frontend-core/src/components/grid/cells/RelationshipCell.svelte +++ b/packages/frontend-core/src/components/grid/cells/RelationshipCell.svelte @@ -102,9 +102,8 @@ lastSearchId = Math.random() searching = true const thisSearchId = lastSearchId - const results = await searchFunction({ + const results = await searchFunction(schema.tableId, { paginate: false, - tableId: schema.tableId, limit: 20, query: { string: { From 84a67bb3460a04999e62cdba9b5431eb0c9720c9 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 17 Dec 2024 18:42:19 +0100 Subject: [PATCH 8/9] 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 9/9] 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,