Make sure calculation views are created and returned correctly.

This commit is contained in:
Sam Rose 2024-10-02 10:36:45 +01:00
parent ab386e5047
commit 0679ec8993
No known key found for this signature in database
2 changed files with 17 additions and 18 deletions

View File

@ -542,7 +542,7 @@ describe.each([
}) })
}) })
it.only("can create a view with calculation fields", async () => { it("can create a view with calculation fields", async () => {
let view = await config.api.viewV2.create({ let view = await config.api.viewV2.create({
tableId: table._id!, tableId: table._id!,
name: generator.guid(), name: generator.guid(),
@ -555,6 +555,8 @@ describe.each([
}, },
}) })
expect(Object.keys(view.schema!)).toHaveLength(1)
let sum = view.schema!.sum as ViewCalculationFieldMetadata let sum = view.schema!.sum as ViewCalculationFieldMetadata
expect(sum).toBeDefined() expect(sum).toBeDefined()
expect(sum.calculationType).toEqual(CalculationType.SUM) expect(sum.calculationType).toEqual(CalculationType.SUM)

View File

@ -258,19 +258,12 @@ export async function enrichSchema(
view: ViewV2, view: ViewV2,
tableSchema: TableSchema tableSchema: TableSchema
): Promise<ViewV2Enriched> { ): Promise<ViewV2Enriched> {
const tableCache: Record<string, Table> = {}
async function populateRelTableSchema( async function populateRelTableSchema(
tableId: string, tableId: string,
viewFields: Record<string, RelationSchemaField> viewFields: Record<string, RelationSchemaField>
) { ) {
if (!tableCache[tableId]) { const relTable = await sdk.tables.getTable(tableId)
tableCache[tableId] = await sdk.tables.getTable(tableId)
}
const relTable = tableCache[tableId]
const result: Record<string, ViewV2ColumnEnriched> = {} const result: Record<string, ViewV2ColumnEnriched> = {}
for (const relTableFieldName of Object.keys(relTable.schema)) { for (const relTableFieldName of Object.keys(relTable.schema)) {
const relTableField = relTable.schema[relTableFieldName] const relTableField = relTable.schema[relTableFieldName]
if ([FieldType.LINK, FieldType.FORMULA].includes(relTableField.type)) { if ([FieldType.LINK, FieldType.FORMULA].includes(relTableField.type)) {
@ -299,15 +292,22 @@ export async function enrichSchema(
const viewSchema = view.schema || {} const viewSchema = view.schema || {}
const anyViewOrder = Object.values(viewSchema).some(ui => ui.order != null) const anyViewOrder = Object.values(viewSchema).some(ui => ui.order != null)
for (const key of Object.keys(tableSchema).filter(
k => tableSchema[k].visible !== false const visibleSchemaFields = Object.keys(viewSchema).filter(
)) { key => viewSchema[key].visible !== false
)
const visibleTableFields = Object.keys(tableSchema).filter(
key => tableSchema[key].visible !== false
)
const visibleFields = new Set([...visibleSchemaFields, ...visibleTableFields])
for (const key of visibleFields) {
// if nothing specified in view, then it is not visible // if nothing specified in view, then it is not visible
const ui = viewSchema[key] || { visible: false } const ui = viewSchema[key] || { visible: false }
schema[key] = { schema[key] = {
...tableSchema[key], ...(tableSchema[key] || {}),
...ui, ...ui,
order: anyViewOrder ? ui?.order ?? undefined : tableSchema[key].order, order: anyViewOrder ? ui?.order ?? undefined : tableSchema[key]?.order,
columns: undefined, columns: undefined,
} }
@ -319,10 +319,7 @@ export async function enrichSchema(
} }
} }
return { return { ...view, schema }
...view,
schema: schema,
}
} }
export function syncSchema( export function syncSchema(