Enrich configuration only for views

This commit is contained in:
Adria Navarro 2024-08-28 17:58:19 +02:00
parent 4826a5fbbf
commit de29d31c35
9 changed files with 74 additions and 92 deletions

View File

@ -18,8 +18,6 @@
import GridEditColumnModal from "components/backend/DataTable/modals/grid/GridEditColumnModal.svelte"
import GridUsersTableButton from "components/backend/DataTable/modals/grid/GridUsersTableButton.svelte"
import { DB_TYPE_EXTERNAL } from "constants/backend"
import { isEnabled } from "helpers/featureFlags"
import { FeatureFlag } from "@budibase/types"
const userSchemaOverrides = {
firstName: { displayName: "First name", disabled: true },
@ -68,7 +66,6 @@
canDeleteRows={!isUsersTable}
canEditRows={!isUsersTable || !$appStore.features.disableUserMetadata}
canEditColumns={!isUsersTable || !$appStore.features.disableUserMetadata}
canSetRelationshipSchemas={isEnabled(FeatureFlag.ENRICHED_RELATIONSHIPS)}
schemaOverrides={isUsersTable ? userSchemaOverrides : null}
showAvatars={false}
on:updatedatasource={handleGridTableUpdate}

View File

@ -123,7 +123,7 @@
const table = await cache.actions.getTable(relationshipField.tableId)
relationshipPanelColumns = Object.entries(
relationshipField?.schema || {}
relationshipField?.columns || {}
).map(([name, column]) => {
return {
name: name,

View File

@ -62,12 +62,12 @@ export const deriveStores = context => {
}
if ($subSchemaMutations[field]) {
enrichedSchema[field].schema ??= {}
enrichedSchema[field].columns ??= {}
for (const [fieldName, mutation] of Object.entries(
$subSchemaMutations[field]
)) {
enrichedSchema[field].schema[fieldName] = {
...enrichedSchema[field].schema[fieldName],
enrichedSchema[field].columns[fieldName] = {
...enrichedSchema[field].columns[fieldName],
...mutation,
}
}
@ -239,12 +239,12 @@ export const createActions = context => {
...$schemaMutations[column],
}
if ($subSchemaMutations[column]) {
newSchema[column].schema ??= {}
newSchema[column].columns ??= {}
for (const [fieldName, mutation] of Object.entries(
$subSchemaMutations[column]
)) {
newSchema[column].schema[fieldName] = {
...newSchema[column].schema[fieldName],
newSchema[column].columns[fieldName] = {
...newSchema[column].columns[fieldName],
...mutation,
}
}

View File

@ -95,10 +95,7 @@ export async function find(ctx: UserCtx<void, TableResponse>) {
const tableId = ctx.params.tableId
const table = await sdk.tables.getTable(tableId)
const result = await sdk.tables.enrichViewSchemas({
...table,
schema: await sdk.tables.enrichRelationshipSchema(table.schema),
})
const result = await sdk.tables.enrichViewSchemas(table)
ctx.body = result
}

View File

@ -22,8 +22,8 @@ async function parseSchema(view: CreateViewRequest) {
let fieldRelatedSchema:
| Record<string, RequiredKeys<RelationSchemaField>>
| undefined
if (schemaValue.schema) {
fieldRelatedSchema = Object.entries(schemaValue.schema).reduce<
if (schemaValue.columns) {
fieldRelatedSchema = Object.entries(schemaValue.columns).reduce<
NonNullable<typeof fieldRelatedSchema>
>((acc, [key, fieldSchema]) => {
acc[key] = {
@ -36,7 +36,7 @@ async function parseSchema(view: CreateViewRequest) {
const fieldSchema: RequiredKeys<
ViewFieldMetadata & {
schema: typeof fieldRelatedSchema
columns: typeof fieldRelatedSchema
}
> = {
order: schemaValue.order,
@ -44,7 +44,7 @@ async function parseSchema(view: CreateViewRequest) {
visible: schemaValue.visible,
readonly: schemaValue.readonly,
icon: schemaValue.icon,
schema: fieldRelatedSchema,
columns: fieldRelatedSchema,
}
Object.entries(fieldSchema)
.filter(([, val]) => val === undefined)

View File

@ -11,10 +11,9 @@ import { USER_METDATA_PREFIX } from "../utils"
import partition from "lodash/partition"
import { getGlobalUsersFromMetadata } from "../../utilities/global"
import { processFormulas } from "../../utilities/rowProcessor"
import { context, features } from "@budibase/backend-core"
import { context } from "@budibase/backend-core"
import {
ContextUser,
FeatureFlag,
FieldType,
LinkDocumentValue,
Row,
@ -259,11 +258,7 @@ export async function squashLinksToPrimaryDisplay(
for (const row of enrichedArray) {
// this only fetches the table if its not already in array
const rowTable = await getLinkedTable(row.tableId!, linkedTables)
const safeSchema =
(rowTable?.schema &&
(await sdk.tables.enrichRelationshipSchema(rowTable.schema))) ||
{}
for (let [column, schema] of Object.entries(safeSchema)) {
for (let [column, schema] of Object.entries(rowTable.schema)) {
if (schema.type !== FieldType.LINK || !Array.isArray(row[column])) {
continue
}
@ -275,16 +270,17 @@ export async function squashLinksToPrimaryDisplay(
const obj: any = { _id: link._id }
obj.primaryDisplay = getPrimaryDisplayValue(link, linkedTable)
const allowRelationshipSchemas = await features.flags.isEnabled(
FeatureFlag.ENRICHED_RELATIONSHIPS
)
if (schema.schema && allowRelationshipSchemas) {
for (const relField of Object.entries(schema.schema)
.filter(([_, field]) => field.visible !== false)
.map(([fieldKey]) => fieldKey)) {
obj[relField] = link[relField]
}
}
// TODO
// const allowRelationshipSchemas = await features.flags.isEnabled(
// FeatureFlag.ENRICHED_RELATIONSHIPS
// )
// if (schema.schema && allowRelationshipSchemas) {
// for (const relField of Object.entries(schema.schema)
// .filter(([_, field]) => field.visible !== false)
// .map(([fieldKey]) => fieldKey)) {
// obj[relField] = link[relField]
// }
// }
newLinks.push(obj)
}

View File

@ -146,53 +146,6 @@ export async function getTables(tableIds: string[]): Promise<Table[]> {
return processTables(tables)
}
export async function enrichRelationshipSchema(
schema: TableSchema
): Promise<TableSchema> {
const tableCache: Record<string, Table> = {}
async function populateRelTableSchema(field: RelationshipFieldMetadata) {
if (!tableCache[field.tableId]) {
tableCache[field.tableId] = await sdk.tables.getTable(field.tableId)
}
const relTable = tableCache[field.tableId]
const fieldSchema = field.schema || {}
const resultSchema: Record<string, RelationSchemaField> = {}
for (const relTableFieldName of Object.keys(relTable.schema)) {
const relTableField = relTable.schema[relTableFieldName]
if ([FieldType.LINK, FieldType.FORMULA].includes(relTableField.type)) {
continue
}
if (relTableField.visible === false) {
continue
}
const isVisible = !!fieldSchema[relTableFieldName]?.visible
const isReadonly = !!fieldSchema[relTableFieldName]?.readonly
resultSchema[relTableFieldName] = {
visible: isVisible,
readonly: isReadonly,
}
}
field.schema = resultSchema
}
const result: TableSchema = {}
for (const fieldName of Object.keys(schema)) {
const field = { ...schema[fieldName] }
if (field.type === FieldType.LINK) {
await populateRelTableSchema(field)
}
result[fieldName] = field
}
return result
}
export async function enrichViewSchemas(table: Table): Promise<TableResponse> {
const views = []
for (const view of Object.values(table.views ?? [])) {

View File

@ -1,5 +1,8 @@
import {
FieldType,
RelationSchemaField,
RenameColumn,
Table,
TableSchema,
View,
ViewFieldMetadata,
@ -162,23 +165,59 @@ export async function enrichSchema(
view: ViewV2,
tableSchema: TableSchema
): Promise<ViewV2Enriched> {
const tableCache: Record<string, Table> = {}
async function populateRelTableSchema(
tableId: string,
viewFields: Record<string, RelationSchemaField>
) {
if (!tableCache[tableId]) {
tableCache[tableId] = await sdk.tables.getTable(tableId)
}
const relTable = tableCache[tableId]
const result: Record<string, RelationSchemaField> = {}
for (const relTableFieldName of Object.keys(relTable.schema)) {
const relTableField = relTable.schema[relTableFieldName]
if ([FieldType.LINK, FieldType.FORMULA].includes(relTableField.type)) {
continue
}
if (relTableField.visible === false) {
continue
}
const isVisible = !!viewFields[relTableFieldName]?.visible
const isReadonly = !!viewFields[relTableFieldName]?.readonly
result[relTableFieldName] = {
visible: isVisible,
readonly: isReadonly,
}
}
return result
}
let schema: TableSchema = {}
const anyViewOrder = Object.values(view.schema || {}).some(
ui => ui.order != null
)
for (const key of Object.keys(tableSchema).filter(
key => tableSchema[key].visible !== false
)) {
const viewSchema = view.schema || {}
const anyViewOrder = Object.values(viewSchema).some(ui => ui.order != null)
for (const key of Object.keys(schema)) {
// if nothing specified in view, then it is not visible
const ui = view.schema?.[key] || { visible: false }
const ui = viewSchema[key] || { visible: false }
schema[key] = {
...tableSchema[key],
...ui,
order: anyViewOrder ? ui?.order ?? undefined : tableSchema[key].order,
}
}
schema = await sdk.tables.enrichRelationshipSchema(schema)
if (schema[key].type === FieldType.LINK) {
schema[key].columns = await populateRelTableSchema(
schema[key].tableId,
viewSchema[key].columns || {}
)
}
}
return {
...view,

View File

@ -38,7 +38,7 @@ export type ViewFieldMetadata = UIFieldMetadata & {
columns?: Record<string, RelationSchemaField>
}
type RelationSchemaField = {
export type RelationSchemaField = {
visible?: boolean
readonly?: boolean
}