Enrich relationship from backend
This commit is contained in:
parent
e8f33e5fd8
commit
de61754312
|
@ -91,7 +91,9 @@ export async function find(ctx: UserCtx<void, TableResponse>) {
|
|||
const tableId = ctx.params.tableId
|
||||
const table = await sdk.tables.getTable(tableId)
|
||||
|
||||
ctx.body = sdk.tables.enrichViewSchemas(table)
|
||||
let result = await sdk.tables.enrichRelationshipSchemas(table)
|
||||
result = sdk.tables.enrichViewSchemas(result)
|
||||
ctx.body = result
|
||||
}
|
||||
|
||||
export async function save(ctx: UserCtx<SaveTableRequest, SaveTableResponse>) {
|
||||
|
|
|
@ -7,7 +7,9 @@ import {
|
|||
} from "../../../integrations/utils"
|
||||
import {
|
||||
Database,
|
||||
FieldType,
|
||||
INTERNAL_TABLE_SOURCE_ID,
|
||||
RelationshipFieldMetadata,
|
||||
Table,
|
||||
TableResponse,
|
||||
TableSourceType,
|
||||
|
@ -142,6 +144,52 @@ export async function getTables(tableIds: string[]): Promise<Table[]> {
|
|||
return processTables(tables)
|
||||
}
|
||||
|
||||
export async function enrichRelationshipSchemas(
|
||||
table: Table
|
||||
): Promise<TableResponse> {
|
||||
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]
|
||||
|
||||
for (const relTableFieldName of Object.keys(relTable.schema)) {
|
||||
const relTableField = relTable.schema[relTableFieldName]
|
||||
if (relTableField.type === FieldType.LINK) {
|
||||
continue
|
||||
}
|
||||
|
||||
if (relTableField.visible === false) {
|
||||
continue
|
||||
}
|
||||
|
||||
field.schema ??= {}
|
||||
const isPrimaryDisplay = relTableFieldName === relTable.primaryDisplay
|
||||
const isReadonly =
|
||||
isPrimaryDisplay || !!field.schema[relTableFieldName]?.readonly
|
||||
field.schema[relTableFieldName] = {
|
||||
primaryDisplay: isPrimaryDisplay,
|
||||
type: relTableField.type,
|
||||
visible: isReadonly,
|
||||
readonly: isReadonly,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const result: TableResponse = { ...table, schema: {}, views: {} }
|
||||
for (const fieldName of Object.keys(table.schema)) {
|
||||
const field = { ...table.schema[fieldName] }
|
||||
if (field.type === FieldType.LINK) {
|
||||
await populateRelTableSchema(field)
|
||||
}
|
||||
|
||||
result.schema[fieldName] = field
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
export function enrichViewSchemas(table: Table): TableResponse {
|
||||
return {
|
||||
...table,
|
||||
|
|
Loading…
Reference in New Issue