1
0
Fork 0
mirror of synced 2024-09-20 19:33:10 +12:00

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, CreateViewRequest,
Ctx, Ctx,
RequiredKeys, RequiredKeys,
ViewUIFieldMetadata,
UpdateViewRequest, UpdateViewRequest,
ViewResponse, ViewResponse,
ViewResponseEnriched, ViewResponseEnriched,
ViewV2, ViewV2,
ViewFieldMetadata,
RelationSchemaField,
} from "@budibase/types" } from "@budibase/types"
import { builderSocket, gridSocket } from "../../../websockets" import { builderSocket, gridSocket } from "../../../websockets"
@ -18,21 +19,41 @@ async function parseSchema(view: CreateViewRequest) {
const finalViewSchema = const finalViewSchema =
view.schema && view.schema &&
Object.entries(view.schema).reduce((p, [fieldName, schemaValue]) => { 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, order: schemaValue.order,
width: schemaValue.width, width: schemaValue.width,
visible: schemaValue.visible, visible: schemaValue.visible,
readonly: schemaValue.readonly, readonly: schemaValue.readonly,
icon: schemaValue.icon, icon: schemaValue.icon,
schema: fieldRelatedSchema,
} }
Object.entries(fieldSchema) Object.entries(fieldSchema)
.filter(([, val]) => val === undefined) .filter(([, val]) => val === undefined)
.forEach(([key]) => { .forEach(([key]) => {
delete fieldSchema[key as keyof ViewUIFieldMetadata] delete fieldSchema[key as keyof ViewFieldMetadata]
}) })
p[fieldName] = fieldSchema p[fieldName] = fieldSchema
return p return p
}, {} as Record<string, RequiredKeys<ViewUIFieldMetadata>>) }, {} as Record<string, RequiredKeys<ViewFieldMetadata>>)
return finalViewSchema return finalViewSchema
} }

View file

@ -15,7 +15,7 @@ import {
Table, Table,
TableSourceType, TableSourceType,
UpdateViewRequest, UpdateViewRequest,
ViewUIFieldMetadata, ViewFieldMetadata,
ViewV2, ViewV2,
SearchResponse, SearchResponse,
BasicOperator, BasicOperator,
@ -953,7 +953,7 @@ describe.each([
const updatedTable = await config.api.table.get(table._id!) const updatedTable = await config.api.table.get(table._id!)
const viewSchema = updatedTable.views![view!.name!].schema as Record< const viewSchema = updatedTable.views![view!.name!].schema as Record<
string, string,
ViewUIFieldMetadata ViewFieldMetadata
> >
expect(viewSchema.Price?.visible).toEqual(false) expect(viewSchema.Price?.visible).toEqual(false)
expect(viewSchema.Category?.visible).toEqual(true) expect(viewSchema.Category?.visible).toEqual(true)

View file

@ -2,7 +2,7 @@ import {
RenameColumn, RenameColumn,
TableSchema, TableSchema,
View, View,
ViewUIFieldMetadata, ViewFieldMetadata,
ViewV2, ViewV2,
ViewV2Enriched, ViewV2Enriched,
} from "@budibase/types" } from "@budibase/types"
@ -58,7 +58,7 @@ async function guardViewSchema(
if (viewSchema[field].readonly) { if (viewSchema[field].readonly) {
if ( if (
!(await features.isViewReadonlyColumnsEnabled()) && !(await features.isViewReadonlyColumnsEnabled()) &&
!(tableSchemaField as ViewUIFieldMetadata).readonly !(tableSchemaField as ViewFieldMetadata).readonly
) { ) {
throw new HTTPError(`Readonly fields are not enabled`, 400) throw new HTTPError(`Readonly fields are not enabled`, 400)
} }

View file

@ -25,8 +25,17 @@ interface BaseRelationshipFieldMetadata
tableId: string tableId: string
tableRev?: string tableRev?: string
subtype?: AutoFieldSubType.CREATED_BY | AutoFieldSubType.UPDATED_BY 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 // External tables use junction tables, internal tables don't require them
type ManyToManyJunctionTableMetadata = type ManyToManyJunctionTableMetadata =
| { | {

View file

@ -1,5 +1,5 @@
import { SearchFilter, SortOrder, SortType } from "../../api" import { SearchFilter, SortOrder, SortType } from "../../api"
import { UIFieldMetadata } from "./table" import { RelationFieldSchema, UIFieldMetadata } from "./table"
import { Document } from "../document" import { Document } from "../document"
import { DBView } from "../../sdk" import { DBView } from "../../sdk"
@ -33,10 +33,16 @@ export interface View {
groupBy?: string groupBy?: string
} }
export type ViewUIFieldMetadata = UIFieldMetadata & { export type RelationSchemaField = {
visible?: boolean
readonly?: boolean readonly?: boolean
} }
export type ViewFieldMetadata = UIFieldMetadata & {
readonly?: boolean
schema?: Record<string, RelationSchemaField>
}
export interface ViewV2 { export interface ViewV2 {
version: 2 version: 2
id: string id: string
@ -49,7 +55,7 @@ export interface ViewV2 {
order?: SortOrder order?: SortOrder
type?: SortType type?: SortType
} }
schema?: Record<string, ViewUIFieldMetadata> schema?: Record<string, ViewFieldMetadata>
} }
export type ViewSchema = ViewCountOrSumSchema | ViewStatisticsSchema export type ViewSchema = ViewCountOrSumSchema | ViewStatisticsSchema