From 7bd84bd0484311d5b8f2eff963e3b625d846d3cb Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Tue, 26 Sep 2023 17:01:30 +0100 Subject: [PATCH 1/6] Some typing fixes as well as a fix for the frontend issue of tables not correctly having their schemas updated in the table store when relationships added. --- packages/builder/src/stores/backend/tables.js | 28 ++++++++++++++++++- packages/frontend-core/src/api/tables.js | 12 +++++++- .../server/src/api/controllers/table/index.ts | 14 ++++++---- packages/server/src/integrations/utils.ts | 6 ++-- 4 files changed, 50 insertions(+), 10 deletions(-) diff --git a/packages/builder/src/stores/backend/tables.js b/packages/builder/src/stores/backend/tables.js index 201a67824d..dae8674146 100644 --- a/packages/builder/src/stores/backend/tables.js +++ b/packages/builder/src/stores/backend/tables.js @@ -1,7 +1,7 @@ import { get, writable, derived } from "svelte/store" import { cloneDeep } from "lodash/fp" import { API } from "api" -import { SWITCHABLE_TYPES } from "constants/backend" +import { SWITCHABLE_TYPES, FIELDS } from "constants/backend" export function createTablesStore() { const store = writable({ @@ -21,6 +21,23 @@ export function createTablesStore() { })) } + const singleFetch = async tableId => { + const table = await API.getTable(tableId) + store.update(state => { + const list = [] + // update the list, keep order accurate + for (let tbl of state.list) { + if (table._id === tbl._id) { + list.push(table) + } else { + list.push(tbl) + } + } + state.list = list + return state + }) + } + const select = tableId => { store.update(state => ({ ...state, @@ -63,6 +80,14 @@ export function createTablesStore() { const savedTable = await API.saveTable(updatedTable) replaceTable(savedTable._id, savedTable) select(savedTable._id) + // make sure tables up to date (related) + const tableUpdates = [] + for (let column of Object.values(updatedTable?.schema || {})) { + if (column.type === FIELDS.LINK.type) { + tableUpdates.push(singleFetch(column.tableId)) + } + } + await Promise.all(tableUpdates) return savedTable } @@ -84,6 +109,7 @@ export function createTablesStore() { indexes, }) => { let draft = cloneDeep(get(derivedStore).selected) + console.log(draft) // delete the original if renaming // need to handle if the column had no name, empty string diff --git a/packages/frontend-core/src/api/tables.js b/packages/frontend-core/src/api/tables.js index b84bd3076a..a08e35d3d8 100644 --- a/packages/frontend-core/src/api/tables.js +++ b/packages/frontend-core/src/api/tables.js @@ -82,8 +82,9 @@ export const buildTableEndpoints = API => ({ }, }) }, + /** - * Gets a list o tables. + * Gets a list of tables. */ getTables: async () => { return await API.get({ @@ -91,6 +92,15 @@ export const buildTableEndpoints = API => ({ }) }, + /** + * Get a single table based on table ID. + */ + getTable: async tableId => { + return await API.get({ + url: `/api/tables/${tableId}`, + }) + }, + /** * Saves a table. * @param table the table to save diff --git a/packages/server/src/api/controllers/table/index.ts b/packages/server/src/api/controllers/table/index.ts index 29c41ad985..5029856cf4 100644 --- a/packages/server/src/api/controllers/table/index.ts +++ b/packages/server/src/api/controllers/table/index.ts @@ -14,10 +14,12 @@ import { Table, TableResponse, UserCtx, + Datasource, } from "@budibase/types" import sdk from "../../../sdk" import { jsonFromCsvString } from "../../../utilities/csv" import { builderSocket } from "../../../websockets" +import { cloneDeep } from "lodash" function pickApi({ tableId, table }: { tableId?: string; table?: Table }) { if (table && !tableId) { @@ -35,16 +37,16 @@ function pickApi({ tableId, table }: { tableId?: string; table?: Table }) { export async function fetch(ctx: UserCtx) { const internal = await sdk.tables.getAllInternalTables() - const externalTables = await sdk.datasources.getExternalDatasources() + const datasources = await sdk.datasources.getExternalDatasources() - const external = externalTables.flatMap(table => { - let entities = table.entities + const external = datasources.flatMap(datasource => { + let entities = datasource.entities if (entities) { return Object.values(entities).map((entity: Table) => ({ ...entity, type: "external", - sourceId: table._id, - sql: isSQL(table), + sourceId: datasource._id, + sql: isSQL(datasource), })) } else { return [] @@ -80,7 +82,7 @@ export async function save(ctx: UserCtx) { ctx.eventEmitter && ctx.eventEmitter.emitTable(`table:save`, appId, { ...savedTable }) ctx.body = savedTable - builderSocket?.emitTableUpdate(ctx, { ...savedTable }) + builderSocket?.emitTableUpdate(ctx, cloneDeep(savedTable)) } export async function destroy(ctx: UserCtx) { diff --git a/packages/server/src/integrations/utils.ts b/packages/server/src/integrations/utils.ts index 75f4bcbfa1..b6b6f60fee 100644 --- a/packages/server/src/integrations/utils.ts +++ b/packages/server/src/integrations/utils.ts @@ -1,4 +1,4 @@ -import { SqlQuery, Table, SearchFilters } from "@budibase/types" +import { SqlQuery, Table, SearchFilters, Datasource } from "@budibase/types" import { DocumentType, SEPARATOR } from "../db/utils" import { FieldTypes, @@ -184,7 +184,9 @@ export function getSqlQuery(query: SqlQuery | string): SqlQuery { } } -export const isSQL = helpers.isSQL +export function isSQL(datasource: Datasource) { + return helpers.isSQL(datasource) +} export function isIsoDateString(str: string) { const trimmedValue = str.trim() From a8c96848a5c443b00a014efce8e0ebafcfce5b41 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Tue, 26 Sep 2023 17:13:16 +0100 Subject: [PATCH 2/6] Adding a quick check incase too many related tables - switch to just fetching everything. --- packages/builder/src/stores/backend/tables.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/builder/src/stores/backend/tables.js b/packages/builder/src/stores/backend/tables.js index dae8674146..7e01e0d511 100644 --- a/packages/builder/src/stores/backend/tables.js +++ b/packages/builder/src/stores/backend/tables.js @@ -81,13 +81,18 @@ export function createTablesStore() { replaceTable(savedTable._id, savedTable) select(savedTable._id) // make sure tables up to date (related) - const tableUpdates = [] + const tableIdsToFetch = [] for (let column of Object.values(updatedTable?.schema || {})) { if (column.type === FIELDS.LINK.type) { - tableUpdates.push(singleFetch(column.tableId)) + tableIdsToFetch.push(column.tableId) } } - await Promise.all(tableUpdates) + // too many tables to fetch, just get all + if (tableIdsToFetch.length > 3) { + await fetch() + } else { + await Promise.all(tableIdsToFetch.map(id => singleFetch(id))) + } return savedTable } From 4168b45a77a78c6eaf8ca947a37177e64c106cee Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Wed, 27 Sep 2023 11:14:14 +0100 Subject: [PATCH 3/6] PR comments. --- packages/builder/src/stores/backend/tables.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/builder/src/stores/backend/tables.js b/packages/builder/src/stores/backend/tables.js index 7e01e0d511..457a58fdbb 100644 --- a/packages/builder/src/stores/backend/tables.js +++ b/packages/builder/src/stores/backend/tables.js @@ -81,12 +81,13 @@ export function createTablesStore() { replaceTable(savedTable._id, savedTable) select(savedTable._id) // make sure tables up to date (related) - const tableIdsToFetch = [] + let tableIdsToFetch = [] for (let column of Object.values(updatedTable?.schema || {})) { if (column.type === FIELDS.LINK.type) { tableIdsToFetch.push(column.tableId) } } + tableIdsToFetch = [...new Set(tableIdsToFetch)] // too many tables to fetch, just get all if (tableIdsToFetch.length > 3) { await fetch() @@ -114,7 +115,6 @@ export function createTablesStore() { indexes, }) => { let draft = cloneDeep(get(derivedStore).selected) - console.log(draft) // delete the original if renaming // need to handle if the column had no name, empty string From 6c9ff55f8a69fb4c51ead2743d944fccb32dd168 Mon Sep 17 00:00:00 2001 From: melohagan <101575380+melohagan@users.noreply.github.com> Date: Wed, 27 Sep 2023 13:31:27 +0100 Subject: [PATCH 4/6] Check manyRelationships length (#11907) --- packages/server/src/api/controllers/row/ExternalRequest.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/server/src/api/controllers/row/ExternalRequest.ts b/packages/server/src/api/controllers/row/ExternalRequest.ts index e5b2a82311..064e4d1792 100644 --- a/packages/server/src/api/controllers/row/ExternalRequest.ts +++ b/packages/server/src/api/controllers/row/ExternalRequest.ts @@ -818,7 +818,10 @@ export class ExternalRequest { // can't really use response right now const response = await getDatasourceAndQuery(json) // handle many to many relationships now if we know the ID (could be auto increment) - if (operation !== Operation.READ && processed.manyRelationships) { + if ( + operation !== Operation.READ && + processed.manyRelationships?.length > 0 + ) { await this.handleManyRelationships( table._id || "", response[0], From 3f9a36f51263d36316d996bc4c9297891ff7d64f Mon Sep 17 00:00:00 2001 From: Budibase Staging Release Bot <> Date: Wed, 27 Sep 2023 12:31:52 +0000 Subject: [PATCH 5/6] Bump version to 2.10.12-alpha.24 --- lerna.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lerna.json b/lerna.json index c4d2219e21..5ce9c65bdd 100644 --- a/lerna.json +++ b/lerna.json @@ -1,5 +1,5 @@ { - "version": "2.10.12-alpha.23", + "version": "2.10.12-alpha.24", "npmClient": "yarn", "packages": [ "packages/*" From ec8827bf8570516767b6a7053c90138cfa688f8d Mon Sep 17 00:00:00 2001 From: Budibase Staging Release Bot <> Date: Wed, 27 Sep 2023 12:44:13 +0000 Subject: [PATCH 6/6] Bump version to 2.10.12-alpha.25 --- lerna.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lerna.json b/lerna.json index 5ce9c65bdd..b7c61852b9 100644 --- a/lerna.json +++ b/lerna.json @@ -1,5 +1,5 @@ { - "version": "2.10.12-alpha.24", + "version": "2.10.12-alpha.25", "npmClient": "yarn", "packages": [ "packages/*"