From 30e31e12544076ae1813ef7f5aac82d6626f9120 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Fri, 6 Sep 2024 13:22:50 +0200 Subject: [PATCH 1/6] Enrich view columns --- packages/server/src/sdk/app/views/index.ts | 5 ++++- packages/types/src/sdk/view.ts | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/server/src/sdk/app/views/index.ts b/packages/server/src/sdk/app/views/index.ts index a0cffb2634..4e45fcda87 100644 --- a/packages/server/src/sdk/app/views/index.ts +++ b/packages/server/src/sdk/app/views/index.ts @@ -1,4 +1,5 @@ import { + FieldSchema, FieldType, RelationSchemaField, RenameColumn, @@ -176,7 +177,7 @@ export async function enrichSchema( } const relTable = tableCache[tableId] - const result: Record = {} + const result: Record = {} for (const relTableFieldName of Object.keys(relTable.schema)) { const relTableField = relTable.schema[relTableFieldName] @@ -191,6 +192,7 @@ export async function enrichSchema( const isVisible = !!viewFields[relTableFieldName]?.visible const isReadonly = !!viewFields[relTableFieldName]?.readonly result[relTableFieldName] = { + ...relTableField, visible: isVisible, readonly: isReadonly, } @@ -211,6 +213,7 @@ export async function enrichSchema( ...tableSchema[key], ...ui, order: anyViewOrder ? ui?.order ?? undefined : tableSchema[key].order, + columns: undefined, } if (schema[key].type === FieldType.LINK) { diff --git a/packages/types/src/sdk/view.ts b/packages/types/src/sdk/view.ts index b330db3950..96a6807b69 100644 --- a/packages/types/src/sdk/view.ts +++ b/packages/types/src/sdk/view.ts @@ -3,7 +3,7 @@ import { FieldSchema, RelationSchemaField, ViewV2 } from "../documents" export interface ViewV2Enriched extends ViewV2 { schema?: { [key: string]: FieldSchema & { - columns?: Record + columns?: Record } } } From 77be1cd8694d8299bdc882d9abff26a043bb445d Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Fri, 6 Sep 2024 17:01:13 +0200 Subject: [PATCH 2/6] Add metadata on related columns --- .../server/src/api/controllers/view/viewsV2.ts | 3 +++ packages/server/src/sdk/app/views/index.ts | 13 ++++++++----- packages/types/src/documents/app/view.ts | 3 +-- packages/types/src/sdk/view.ts | 14 ++++++++++++-- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/packages/server/src/api/controllers/view/viewsV2.ts b/packages/server/src/api/controllers/view/viewsV2.ts index 90e80fe81d..40afb2e846 100644 --- a/packages/server/src/api/controllers/view/viewsV2.ts +++ b/packages/server/src/api/controllers/view/viewsV2.ts @@ -29,6 +29,9 @@ async function parseSchema(view: CreateViewRequest) { acc[key] = { visible: fieldSchema.visible, readonly: fieldSchema.readonly, + order: fieldSchema.order, + width: fieldSchema.width, + icon: fieldSchema.icon, } return acc }, {}) diff --git a/packages/server/src/sdk/app/views/index.ts b/packages/server/src/sdk/app/views/index.ts index 4e45fcda87..13d81d6802 100644 --- a/packages/server/src/sdk/app/views/index.ts +++ b/packages/server/src/sdk/app/views/index.ts @@ -1,5 +1,4 @@ import { - FieldSchema, FieldType, RelationSchemaField, RenameColumn, @@ -8,6 +7,7 @@ import { View, ViewFieldMetadata, ViewV2, + ViewV2ColumnEnriched, ViewV2Enriched, } from "@budibase/types" import { HTTPError } from "@budibase/backend-core" @@ -177,7 +177,7 @@ export async function enrichSchema( } const relTable = tableCache[tableId] - const result: Record = {} + const result: Record = {} for (const relTableFieldName of Object.keys(relTable.schema)) { const relTableField = relTable.schema[relTableFieldName] @@ -189,10 +189,13 @@ export async function enrichSchema( continue } - const isVisible = !!viewFields[relTableFieldName]?.visible - const isReadonly = !!viewFields[relTableFieldName]?.readonly + const viewFieldSchema = viewFields[relTableFieldName] + const isVisible = !!viewFieldSchema?.visible + const isReadonly = !!viewFieldSchema?.readonly result[relTableFieldName] = { - ...relTableField, + ...viewFieldSchema, + type: relTableField.type, + name: relTableField.name, visible: isVisible, readonly: isReadonly, } diff --git a/packages/types/src/documents/app/view.ts b/packages/types/src/documents/app/view.ts index b5fdcacefe..b847520526 100644 --- a/packages/types/src/documents/app/view.ts +++ b/packages/types/src/documents/app/view.ts @@ -38,8 +38,7 @@ export type ViewFieldMetadata = UIFieldMetadata & { columns?: Record } -export type RelationSchemaField = { - visible?: boolean +export type RelationSchemaField = UIFieldMetadata & { readonly?: boolean } diff --git a/packages/types/src/sdk/view.ts b/packages/types/src/sdk/view.ts index 96a6807b69..7480bf563f 100644 --- a/packages/types/src/sdk/view.ts +++ b/packages/types/src/sdk/view.ts @@ -1,9 +1,19 @@ -import { FieldSchema, RelationSchemaField, ViewV2 } from "../documents" +import { + FieldSchema, + FieldType, + RelationSchemaField, + ViewV2, +} from "../documents" export interface ViewV2Enriched extends ViewV2 { schema?: { [key: string]: FieldSchema & { - columns?: Record + columns?: Record } } } + +export interface ViewV2ColumnEnriched extends RelationSchemaField { + name: string + type: FieldType +} From 26db79d4218f7bc53fbaacfd014228f92c453887 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 9 Sep 2024 16:24:20 +0200 Subject: [PATCH 3/6] Remove cache dependency --- .../controls/ColumnsSettingContent.svelte | 36 +++++++------------ 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/packages/frontend-core/src/components/grid/controls/ColumnsSettingContent.svelte b/packages/frontend-core/src/components/grid/controls/ColumnsSettingContent.svelte index 02ed0a504c..2c6a21d65b 100644 --- a/packages/frontend-core/src/components/grid/controls/ColumnsSettingContent.svelte +++ b/packages/frontend-core/src/components/grid/controls/ColumnsSettingContent.svelte @@ -12,7 +12,7 @@ export let columns export let fromRelationshipField - const { datasource, dispatch, cache, config } = getContext("grid") + const { datasource, dispatch, config } = getContext("grid") $: canSetRelationshipSchemas = $config.canSetRelationshipSchemas @@ -114,29 +114,19 @@ return { ...c, options } }) - let relationshipPanelColumns = [] - async function fetchRelationshipPanelColumns(relationshipField) { - relationshipPanelColumns = [] - if (!relationshipField) { - return + $: relationshipPanelColumns = Object.entries( + relationshipField?.columns || {} + ).map(([name, column]) => { + return { + name: name, + label: name, + schema: { + type: column.type, + visible: column.visible, + readonly: column.readonly, + }, } - - const table = await cache.actions.getTable(relationshipField.tableId) - relationshipPanelColumns = Object.entries( - relationshipField?.columns || {} - ).map(([name, column]) => { - return { - name: name, - label: name, - schema: { - type: table.schema[name].type, - visible: column.visible, - readonly: column.readonly, - }, - } - }) - } - $: fetchRelationshipPanelColumns(relationshipField) + }) async function toggleColumn(column, permission) { const visible = permission !== FieldPermissions.HIDDEN From e40a08ceca50f7f1189f388b6de2a22a3330ad42 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 9 Sep 2024 16:27:54 +0200 Subject: [PATCH 4/6] Fix tests --- packages/server/src/sdk/app/views/tests/views.spec.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/server/src/sdk/app/views/tests/views.spec.ts b/packages/server/src/sdk/app/views/tests/views.spec.ts index 5a86702ab6..1d7360c5eb 100644 --- a/packages/server/src/sdk/app/views/tests/views.spec.ts +++ b/packages/server/src/sdk/app/views/tests/views.spec.ts @@ -355,10 +355,14 @@ describe("table sdk", () => { visible: true, columns: { title: { + name: "title", + type: "string", visible: true, readonly: true, }, age: { + name: "age", + type: "number", visible: false, readonly: false, }, From 1c5bab07aab725bdfb6ffeb9cec53d5165c9dc1c Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Mon, 9 Sep 2024 16:42:41 +0200 Subject: [PATCH 5/6] Fix tests --- .../src/api/routes/tests/viewV2.spec.ts | 90 +++++++++++++++---- 1 file changed, 72 insertions(+), 18 deletions(-) diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index 6d2d13e580..3ca28f31aa 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -1278,9 +1278,18 @@ describe.each([ schema: expect.objectContaining({ aux: expect.objectContaining({ columns: { - id: { visible: false, readonly: false }, - name: { visible: true, readonly: true }, - dob: { visible: true, readonly: true }, + id: expect.objectContaining({ + visible: false, + readonly: false, + }), + name: expect.objectContaining({ + visible: true, + readonly: true, + }), + dob: expect.objectContaining({ + visible: true, + readonly: true, + }), }, }), }), @@ -1323,16 +1332,34 @@ describe.each([ schema: expect.objectContaining({ aux: expect.objectContaining({ columns: { - id: { visible: false, readonly: false }, - name: { visible: true, readonly: true }, - dob: { visible: true, readonly: true }, + id: expect.objectContaining({ + visible: false, + readonly: false, + }), + name: expect.objectContaining({ + visible: true, + readonly: true, + }), + dob: expect.objectContaining({ + visible: true, + readonly: true, + }), }, }), aux2: expect.objectContaining({ columns: { - id: { visible: false, readonly: false }, - name: { visible: true, readonly: true }, - dob: { visible: true, readonly: true }, + id: expect.objectContaining({ + visible: false, + readonly: false, + }), + name: expect.objectContaining({ + visible: true, + readonly: true, + }), + dob: expect.objectContaining({ + visible: true, + readonly: true, + }), }, }), }), @@ -1375,16 +1402,34 @@ describe.each([ schema: expect.objectContaining({ aux: expect.objectContaining({ columns: { - id: { visible: false, readonly: false }, - fullName: { visible: true, readonly: true }, - age: { visible: false, readonly: false }, + id: expect.objectContaining({ + visible: false, + readonly: false, + }), + fullName: expect.objectContaining({ + visible: true, + readonly: true, + }), + age: expect.objectContaining({ + visible: false, + readonly: false, + }), }, }), aux2: expect.objectContaining({ columns: { - id: { visible: false, readonly: false }, - name: { visible: true, readonly: true }, - age: { visible: false, readonly: false }, + id: expect.objectContaining({ + visible: false, + readonly: false, + }), + name: expect.objectContaining({ + visible: true, + readonly: true, + }), + age: expect.objectContaining({ + visible: false, + readonly: false, + }), }, }), }), @@ -1427,9 +1472,18 @@ describe.each([ schema: expect.objectContaining({ aux: expect.objectContaining({ columns: { - id: { visible: false, readonly: false }, - name: { visible: true, readonly: true }, - dob: { visible: true, readonly: true }, + id: expect.objectContaining({ + visible: false, + readonly: false, + }), + name: expect.objectContaining({ + visible: true, + readonly: true, + }), + dob: expect.objectContaining({ + visible: true, + readonly: true, + }), }, }), }), From dc9e1cbbc74633377604109ca109810929d5084e Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 10 Sep 2024 10:44:56 +0200 Subject: [PATCH 6/6] Enrich view with all schema --- packages/server/src/sdk/app/views/index.ts | 2 +- packages/types/src/documents/app/table/constants.ts | 5 +++++ packages/types/src/sdk/view.ts | 12 ++---------- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/packages/server/src/sdk/app/views/index.ts b/packages/server/src/sdk/app/views/index.ts index 13d81d6802..d7e05abf2f 100644 --- a/packages/server/src/sdk/app/views/index.ts +++ b/packages/server/src/sdk/app/views/index.ts @@ -193,8 +193,8 @@ export async function enrichSchema( const isVisible = !!viewFieldSchema?.visible const isReadonly = !!viewFieldSchema?.readonly result[relTableFieldName] = { + ...relTableField, ...viewFieldSchema, - type: relTableField.type, name: relTableField.name, visible: isVisible, readonly: isReadonly, diff --git a/packages/types/src/documents/app/table/constants.ts b/packages/types/src/documents/app/table/constants.ts index 210ad1423d..fffaddc5df 100644 --- a/packages/types/src/documents/app/table/constants.ts +++ b/packages/types/src/documents/app/table/constants.ts @@ -10,6 +10,11 @@ export enum AutoReason { FOREIGN_KEY = "foreign_key", } +export type FieldSubType = + | AutoFieldSubType + | JsonFieldSubType + | BBReferenceFieldSubType + export enum AutoFieldSubType { CREATED_BY = "createdBy", CREATED_AT = "createdAt", diff --git a/packages/types/src/sdk/view.ts b/packages/types/src/sdk/view.ts index 7480bf563f..422207197d 100644 --- a/packages/types/src/sdk/view.ts +++ b/packages/types/src/sdk/view.ts @@ -1,9 +1,4 @@ -import { - FieldSchema, - FieldType, - RelationSchemaField, - ViewV2, -} from "../documents" +import { FieldSchema, RelationSchemaField, ViewV2 } from "../documents" export interface ViewV2Enriched extends ViewV2 { schema?: { @@ -13,7 +8,4 @@ export interface ViewV2Enriched extends ViewV2 { } } -export interface ViewV2ColumnEnriched extends RelationSchemaField { - name: string - type: FieldType -} +export type ViewV2ColumnEnriched = RelationSchemaField & FieldSchema