Persist view related schemas

This commit is contained in:
Adria Navarro 2024-08-21 17:07:49 +02:00
parent f8598ff5fa
commit 1504cead0c
5 changed files with 47 additions and 11 deletions

View File

@ -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<ViewUIFieldMetadata> = {
let fieldRelatedSchema:
| Record<string, RequiredKeys<RelationSchemaField>>
| undefined
if (schemaValue.schema) {
fieldRelatedSchema = Object.entries(schemaValue.schema).reduce<
NonNullable<typeof fieldRelatedSchema>
>((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<string, RequiredKeys<ViewUIFieldMetadata>>)
}, {} as Record<string, RequiredKeys<ViewFieldMetadata>>)
return finalViewSchema
}

View File

@ -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)

View File

@ -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)
}

View File

@ -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 =
| {

View File

@ -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<string, RelationSchemaField>
}
export interface ViewV2 {
version: 2
id: string
@ -49,7 +55,7 @@ export interface ViewV2 {
order?: SortOrder
type?: SortType
}
schema?: Record<string, ViewUIFieldMetadata>
schema?: Record<string, ViewFieldMetadata>
}
export type ViewSchema = ViewCountOrSumSchema | ViewStatisticsSchema