From 0b8199be746240670bafd3cfff247304a8498054 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Thu, 31 Oct 2024 12:19:57 +0000 Subject: [PATCH 1/3] Use existing _id values for non DS+ in tables, trusting that they are unique --- .../src/components/grid/lib/constants.js | 2 ++ .../src/components/grid/lib/utils.js | 19 +++++++++++++------ .../src/components/grid/stores/rows.js | 17 +++++++++++------ 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/packages/frontend-core/src/components/grid/lib/constants.js b/packages/frontend-core/src/components/grid/lib/constants.js index 6ea7a98178..81b4a2f823 100644 --- a/packages/frontend-core/src/components/grid/lib/constants.js +++ b/packages/frontend-core/src/components/grid/lib/constants.js @@ -10,6 +10,8 @@ export const DefaultColumnWidth = 200 export const MinColumnWidth = 80 export const NewRowID = "new" export const BlankRowID = "blank" +export const GeneratedIDPrefix = "‽‽" +export const CellIDSeparator = "‽‽" export const RowPageSize = 100 export const FocusedCellMinOffset = ScrollBarSize * 3 export const ControlsHeight = 50 diff --git a/packages/frontend-core/src/components/grid/lib/utils.js b/packages/frontend-core/src/components/grid/lib/utils.js index b5485abc36..f1f33d9950 100644 --- a/packages/frontend-core/src/components/grid/lib/utils.js +++ b/packages/frontend-core/src/components/grid/lib/utils.js @@ -1,18 +1,17 @@ -// We can't use "-" as a separator as this can be present in the ID -// or column name, so we use something very unusual to avoid this problem -const JOINING_CHARACTER = "‽‽" +import { GeneratedIDPrefix, CellIDSeparator } from "./constants" +import { Helpers } from "@budibase/bbui" export const parseCellID = cellId => { if (!cellId) { return { rowId: undefined, field: undefined } } - const parts = cellId.split(JOINING_CHARACTER) + const parts = cellId.split(CellIDSeparator) const field = parts.pop() - return { rowId: parts.join(JOINING_CHARACTER), field } + return { rowId: parts.join(CellIDSeparator), field } } export const getCellID = (rowId, fieldName) => { - return `${rowId}${JOINING_CHARACTER}${fieldName}` + return `${rowId}${CellIDSeparator}${fieldName}` } export const parseEventLocation = e => { @@ -21,3 +20,11 @@ export const parseEventLocation = e => { y: e.clientY ?? e.touches?.[0]?.clientY, } } + +export const generateRowID = () => { + return `${GeneratedIDPrefix}${Helpers.uuid()}` +} + +export const isGeneratedRowID = id => { + return id?.startsWith(GeneratedIDPrefix) +} diff --git a/packages/frontend-core/src/components/grid/stores/rows.js b/packages/frontend-core/src/components/grid/stores/rows.js index 4cfaa1cfd5..122bbc295a 100644 --- a/packages/frontend-core/src/components/grid/stores/rows.js +++ b/packages/frontend-core/src/components/grid/stores/rows.js @@ -1,7 +1,12 @@ import { writable, derived, get } from "svelte/store" import { fetchData } from "../../../fetch" import { NewRowID, RowPageSize } from "../lib/constants" -import { getCellID, parseCellID } from "../lib/utils" +import { + generateRowID, + getCellID, + isGeneratedRowID, + parseCellID, +} from "../lib/utils" import { tick } from "svelte" import { Helpers } from "@budibase/bbui" import { sleep } from "../../../utils/utils" @@ -634,10 +639,10 @@ export const createActions = context => { newRow = newRows[i] // Ensure we have a unique _id. - // This means generating one for non DS+, overwriting any that may already - // exist as we cannot allow duplicates. - if (!$hasBudibaseIdentifiers) { - newRow._id = Helpers.uuid() + // We generate one for non DS+ where required, but trust that any existing + // _id values are unique (e.g. Mongo) + if (!$hasBudibaseIdentifiers && !newRow._id?.length) { + newRow._id = generateRowID() } if (!rowCacheMap[newRow._id]) { @@ -674,7 +679,7 @@ export const createActions = context => { let clone = { ...row } delete clone.__idx delete clone.__metadata - if (!get(hasBudibaseIdentifiers)) { + if (!get(hasBudibaseIdentifiers) && isGeneratedRowID(clone._id)) { delete clone._id } return clone From afbec934f31e132f637aa944f04599da2c06395e Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Thu, 31 Oct 2024 12:28:52 +0000 Subject: [PATCH 2/3] Clean table data before using it as additional data context to better reflect usable structure --- packages/client/src/components/app/GridBlock.svelte | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/client/src/components/app/GridBlock.svelte b/packages/client/src/components/app/GridBlock.svelte index c6b08e3869..3d9dcc028e 100644 --- a/packages/client/src/components/app/GridBlock.svelte +++ b/packages/client/src/components/app/GridBlock.svelte @@ -60,8 +60,10 @@ // Provide additional data context for live binding eval export const getAdditionalDataContext = () => { - const rows = get(grid?.getContext()?.rows) - const goldenRow = generateGoldenSample(rows) + const gridContext = grid?.getContext() + const rows = get(gridContext?.rows) || [] + const cleaned = rows.map(gridContext?.rows.actions.cleanRow || (x => x)) + const goldenRow = generateGoldenSample(cleaned) const id = get(component).id return { // Not sure what this one is for... From b9ba76f639aefaed40c829910bbf3aa8523737db Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Thu, 31 Oct 2024 12:33:07 +0000 Subject: [PATCH 3/3] Avoid creating new function every iteration of array.map --- packages/client/src/components/app/GridBlock.svelte | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/client/src/components/app/GridBlock.svelte b/packages/client/src/components/app/GridBlock.svelte index 3d9dcc028e..4a8dfb4fcb 100644 --- a/packages/client/src/components/app/GridBlock.svelte +++ b/packages/client/src/components/app/GridBlock.svelte @@ -62,7 +62,8 @@ export const getAdditionalDataContext = () => { const gridContext = grid?.getContext() const rows = get(gridContext?.rows) || [] - const cleaned = rows.map(gridContext?.rows.actions.cleanRow || (x => x)) + const clean = gridContext?.rows.actions.cleanRow || (x => x) + const cleaned = rows.map(clean) const goldenRow = generateGoldenSample(cleaned) const id = get(component).id return {