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

Enrich relationship from backend

This commit is contained in:
Adria Navarro 2024-08-21 11:10:50 +02:00
parent e8f33e5fd8
commit de61754312
2 changed files with 51 additions and 1 deletions

View file

@ -91,7 +91,9 @@ export async function find(ctx: UserCtx<void, TableResponse>) {
const tableId = ctx.params.tableId const tableId = ctx.params.tableId
const table = await sdk.tables.getTable(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>) { export async function save(ctx: UserCtx<SaveTableRequest, SaveTableResponse>) {

View file

@ -7,7 +7,9 @@ import {
} from "../../../integrations/utils" } from "../../../integrations/utils"
import { import {
Database, Database,
FieldType,
INTERNAL_TABLE_SOURCE_ID, INTERNAL_TABLE_SOURCE_ID,
RelationshipFieldMetadata,
Table, Table,
TableResponse, TableResponse,
TableSourceType, TableSourceType,
@ -142,6 +144,52 @@ export async function getTables(tableIds: string[]): Promise<Table[]> {
return processTables(tables) 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 { export function enrichViewSchemas(table: Table): TableResponse {
return { return {
...table, ...table,