Enrich at schema level

This commit is contained in:
Adria Navarro 2024-09-09 12:31:00 +02:00
parent 6d8505cee6
commit 083e6ae15b
2 changed files with 24 additions and 52 deletions

View File

@ -151,6 +151,7 @@ export const initialise = context => {
readonly: fieldSchema.readonly,
order: fieldSchema.order ?? oldColumn?.order,
conditions: fieldSchema.conditions,
related: fieldSchema.related,
}
// Override a few properties for primary display
if (field === primaryDisplay) {
@ -160,32 +161,6 @@ export const initialise = context => {
}
return column
})
.flatMap(field => {
const relatedColumns = []
const schemaColumns = $enrichedSchema?.[field.name]?.columns
if (field.visible && schemaColumns) {
for (const relColumn of Object.keys(schemaColumns)) {
const relFieldSchema = schemaColumns[relColumn]
if (!relFieldSchema.visible) {
continue
}
relatedColumns.push({
name: `${field.name}.${relColumn}`,
label: `${relColumn} (${field.name})`,
schema: relFieldSchema,
width: relFieldSchema.width || DefaultColumnWidth,
visible: relFieldSchema.visible ?? true,
readonly: relFieldSchema.readonly,
order: relFieldSchema.order,
conditions: relFieldSchema.conditions,
related: true,
})
}
}
return [field, ...relatedColumns]
})
.sort((a, b) => {
// Display column should always come first
if (a.name === primaryDisplay) {

View File

@ -53,10 +53,31 @@ export const deriveStores = context => {
if (!$schema) {
return null
}
const schemaWithRelatedColumns = Object.keys($schema || {}).reduce(
(acc, c) => {
const field = $schema[c]
acc[c] = field
if (field.columns) {
for (const relColumn of Object.keys(field.columns)) {
const name = `${field.name}.${relColumn}`
acc[name] = {
...field.columns[relColumn],
name,
related: { field: c, subField: relColumn },
}
}
}
return acc
},
{}
)
let enrichedSchema = {}
Object.keys($schema).forEach(field => {
Object.keys(schemaWithRelatedColumns).forEach(field => {
enrichedSchema[field] = {
...$schema[field],
...schemaWithRelatedColumns[field],
...$schemaOverrides?.[field],
...$schemaMutations[field],
}
@ -73,30 +94,6 @@ export const deriveStores = context => {
}
}
})
if ($schemaOverrides) {
Object.keys($schemaOverrides).forEach(field => {
if (!$schemaOverrides[field].related) {
return
}
const { field: relField, subField: relSubField } =
$schemaOverrides[field].related
if (
!$schema[relField].visible ||
!$schema[relField]?.columns?.[relSubField]?.visible
) {
return
}
enrichedSchema[field] = {
...$schemaOverrides[field],
name: field,
type: $schema[relField]?.columns?.[relSubField]?.type,
readonly: true,
}
})
}
return enrichedSchema
}
)