From 1504cead0c06485fcb05ff280360deb8cf7aa4fa Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 21 Aug 2024 17:07:49 +0200 Subject: [PATCH] Persist view related schemas --- .../src/api/controllers/view/viewsV2.ts | 29 ++++++++++++++++--- .../src/api/routes/tests/viewV2.spec.ts | 4 +-- packages/server/src/sdk/app/views/index.ts | 4 +-- .../types/src/documents/app/table/schema.ts | 9 ++++++ packages/types/src/documents/app/view.ts | 12 ++++++-- 5 files changed, 47 insertions(+), 11 deletions(-) diff --git a/packages/server/src/api/controllers/view/viewsV2.ts b/packages/server/src/api/controllers/view/viewsV2.ts index 4208772fa6..f341b853c6 100644 --- a/packages/server/src/api/controllers/view/viewsV2.ts +++ b/packages/server/src/api/controllers/view/viewsV2.ts @@ -3,11 +3,12 @@ import { CreateViewRequest, Ctx, RequiredKeys, - ViewUIFieldMetadata, UpdateViewRequest, ViewResponse, ViewResponseEnriched, ViewV2, + ViewFieldMetadata, + RelationSchemaField, } from "@budibase/types" import { builderSocket, gridSocket } from "../../../websockets" @@ -18,21 +19,41 @@ async function parseSchema(view: CreateViewRequest) { const finalViewSchema = view.schema && Object.entries(view.schema).reduce((p, [fieldName, schemaValue]) => { - const fieldSchema: RequiredKeys = { + let fieldRelatedSchema: + | Record> + | undefined + if (schemaValue.schema) { + fieldRelatedSchema = Object.entries(schemaValue.schema).reduce< + NonNullable + >((acc, [key, fieldSchema]) => { + acc[key] = { + visible: fieldSchema.visible, + readonly: fieldSchema.readonly, + } + return acc + }, {}) + } + + const fieldSchema: RequiredKeys< + ViewFieldMetadata & { + schema: typeof fieldRelatedSchema + } + > = { order: schemaValue.order, width: schemaValue.width, visible: schemaValue.visible, readonly: schemaValue.readonly, icon: schemaValue.icon, + schema: fieldRelatedSchema, } Object.entries(fieldSchema) .filter(([, val]) => val === undefined) .forEach(([key]) => { - delete fieldSchema[key as keyof ViewUIFieldMetadata] + delete fieldSchema[key as keyof ViewFieldMetadata] }) p[fieldName] = fieldSchema return p - }, {} as Record>) + }, {} as Record>) return finalViewSchema } diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index 4401efc480..ef4deacd83 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -15,7 +15,7 @@ import { Table, TableSourceType, UpdateViewRequest, - ViewUIFieldMetadata, + ViewFieldMetadata, ViewV2, SearchResponse, BasicOperator, @@ -953,7 +953,7 @@ describe.each([ const updatedTable = await config.api.table.get(table._id!) const viewSchema = updatedTable.views![view!.name!].schema as Record< string, - ViewUIFieldMetadata + ViewFieldMetadata > expect(viewSchema.Price?.visible).toEqual(false) expect(viewSchema.Category?.visible).toEqual(true) diff --git a/packages/server/src/sdk/app/views/index.ts b/packages/server/src/sdk/app/views/index.ts index 1c09f710d7..bed6254943 100644 --- a/packages/server/src/sdk/app/views/index.ts +++ b/packages/server/src/sdk/app/views/index.ts @@ -2,7 +2,7 @@ import { RenameColumn, TableSchema, View, - ViewUIFieldMetadata, + ViewFieldMetadata, ViewV2, ViewV2Enriched, } from "@budibase/types" @@ -58,7 +58,7 @@ async function guardViewSchema( if (viewSchema[field].readonly) { if ( !(await features.isViewReadonlyColumnsEnabled()) && - !(tableSchemaField as ViewUIFieldMetadata).readonly + !(tableSchemaField as ViewFieldMetadata).readonly ) { throw new HTTPError(`Readonly fields are not enabled`, 400) } diff --git a/packages/types/src/documents/app/table/schema.ts b/packages/types/src/documents/app/table/schema.ts index 6078f73d1d..7b65837ecd 100644 --- a/packages/types/src/documents/app/table/schema.ts +++ b/packages/types/src/documents/app/table/schema.ts @@ -25,8 +25,17 @@ interface BaseRelationshipFieldMetadata tableId: string tableRev?: string subtype?: AutoFieldSubType.CREATED_BY | AutoFieldSubType.UPDATED_BY + schema: RelationFieldSchema } +export type RelationFieldSchema = Record< + string, + { + visible?: boolean + readonly?: boolean + } +> + // External tables use junction tables, internal tables don't require them type ManyToManyJunctionTableMetadata = | { diff --git a/packages/types/src/documents/app/view.ts b/packages/types/src/documents/app/view.ts index 2572ddba5d..2d9b22e773 100644 --- a/packages/types/src/documents/app/view.ts +++ b/packages/types/src/documents/app/view.ts @@ -1,5 +1,5 @@ import { SearchFilter, SortOrder, SortType } from "../../api" -import { UIFieldMetadata } from "./table" +import { RelationFieldSchema, UIFieldMetadata } from "./table" import { Document } from "../document" import { DBView } from "../../sdk" @@ -33,10 +33,16 @@ export interface View { groupBy?: string } -export type ViewUIFieldMetadata = UIFieldMetadata & { +export type RelationSchemaField = { + visible?: boolean readonly?: boolean } +export type ViewFieldMetadata = UIFieldMetadata & { + readonly?: boolean + schema?: Record +} + export interface ViewV2 { version: 2 id: string @@ -49,7 +55,7 @@ export interface ViewV2 { order?: SortOrder type?: SortType } - schema?: Record + schema?: Record } export type ViewSchema = ViewCountOrSumSchema | ViewStatisticsSchema