1
0
Fork 0
mirror of synced 2024-07-07 15:25:52 +12:00

Use new table schema

This commit is contained in:
Adria Navarro 2023-07-24 12:40:55 +02:00
parent ed02aa4d8b
commit 396c4ad439
3 changed files with 127 additions and 89 deletions

View file

@ -11,7 +11,9 @@ import {
FetchTablesResponse,
Table,
TableResponse,
TableSchema,
TableViewsResponse,
UIFieldMetadata,
UserCtx,
} from "@budibase/types"
import sdk from "../../../sdk"
@ -55,6 +57,25 @@ export async function fetch(ctx: UserCtx<void, FetchTablesResponse>) {
ctx.body = response
}
function enrichSchema(
tableSchema: TableSchema,
viewOverrides: Record<string, UIFieldMetadata>
) {
const result: TableSchema = {}
for (const [columnName, columnUIMetadata] of Object.entries(viewOverrides)) {
if (!columnUIMetadata.visible) {
continue
}
if (!tableSchema[columnName]) {
continue
}
result[columnName] = _.merge(tableSchema[columnName], columnUIMetadata)
}
return result
}
function enrichTable(table: Table): TableResponse {
const result: TableResponse = {
...table,
@ -64,26 +85,16 @@ function enrichTable(table: Table): TableResponse {
} else {
p[v.name] = {
...v,
schema: !v?.columns?.length
? table.schema
: _.pick(table.schema, ...v.columns),
schema:
!v?.columns || !Object.entries(v?.columns).length
? table.schema
: enrichSchema(table.schema, v.columns),
}
}
return p
}, {} as TableViewsResponse),
}
for (const [viewName, view] of Object.entries(table.views!)) {
if (sdk.views.isV2(view)) {
table.views![viewName] = {
...view,
schema: !view?.columns?.length
? table.schema
: _.pick(table.schema, ...view.columns),
}
}
}
return result
}

View file

@ -184,7 +184,46 @@ describe("/tables", () => {
let testTable: Table
beforeEach(async () => {
testTable = await config.createTable(testTable)
testTable = await config.createTable({
name: "TestTable",
type: "table",
schema: {
name: {
type: FieldType.STRING,
name: "name",
visible: true,
width: 80,
constraints: {
type: "string",
},
},
description: {
type: FieldType.STRING,
name: "description",
visible: true,
width: 200,
constraints: {
type: "string",
},
},
id: {
type: FieldType.NUMBER,
name: "id",
visible: true,
constraints: {
type: "number",
},
},
hiddenField: {
type: FieldType.STRING,
name: "hiddenField",
visible: false,
constraints: {
type: "string",
},
},
},
})
})
afterEach(() => {
@ -253,6 +292,8 @@ describe("/tables", () => {
name: {
type: "string",
name: "name",
visible: true,
width: 80,
constraints: {
type: "string",
},
@ -260,6 +301,24 @@ describe("/tables", () => {
description: {
type: "string",
name: "description",
visible: true,
width: 200,
constraints: {
type: "string",
},
},
id: {
type: "number",
name: "id",
visible: true,
constraints: {
type: "number",
},
},
hiddenField: {
type: "string",
name: "hiddenField",
visible: false,
constraints: {
type: "string",
},
@ -273,10 +332,17 @@ describe("/tables", () => {
})
it("should fetch the default schema if not overriden", async () => {
const tableId = config.table!._id!
const tableId = testTable._id!
const views = [
await config.api.viewV2.create({ tableId }),
await config.api.viewV2.create({ tableId, columns: ["name"] }),
await config.api.viewV2.create({
tableId,
columns: {
name: { visible: true },
id: { visible: true },
description: { visible: false },
},
}),
]
const res = await config.api.table.fetch()
@ -292,6 +358,8 @@ describe("/tables", () => {
name: {
type: "string",
name: "name",
visible: true,
width: 80,
constraints: {
type: "string",
},
@ -299,6 +367,24 @@ describe("/tables", () => {
description: {
type: "string",
name: "description",
visible: true,
width: 200,
constraints: {
type: "string",
},
},
id: {
type: "number",
name: "id",
visible: true,
constraints: {
type: "number",
},
},
hiddenField: {
type: "string",
name: "hiddenField",
visible: false,
constraints: {
type: "string",
},
@ -311,10 +397,20 @@ describe("/tables", () => {
name: {
type: "string",
name: "name",
visible: true,
width: 80,
constraints: {
type: "string",
},
},
id: {
type: "number",
name: "id",
visible: true,
constraints: {
type: "number",
},
},
},
},
},
@ -327,7 +423,7 @@ describe("/tables", () => {
const tableId = config.table!._id!
const view = await config.api.viewV2.create({
tableId,
columns: ["unnexisting", "name"],
columns: { unnexisting: { visible: true }, name: { visible: true } },
})
const res = await config.api.table.fetch()
@ -343,6 +439,8 @@ describe("/tables", () => {
name: {
type: "string",
name: "name",
visible: true,
width: 80,
constraints: {
type: "string",
},

View file

@ -111,75 +111,4 @@ describe("/v2/views", () => {
expect(await getPersistedView()).toBeUndefined()
})
})
// describe("getSchema", () => {
// beforeAll(async () => {
// await config.createTable(priceTable())
// })
// it("returns table schema if no columns are defined", async () => {
// const view = await config.api.viewV2.create()
// const result = await config.api.viewV2.getSchema(view.id)
// expect(result).toEqual({
// schema: {
// Price: {
// type: "number",
// name: "Price",
// constraints: {},
// },
// Currency: {
// type: "string",
// name: "Currency",
// constraints: {},
// },
// ItemId: {
// type: "string",
// name: "ItemId",
// constraints: {
// type: "string",
// },
// },
// },
// })
// })
// it("respects view column definition if exists", async () => {
// const view = await config.api.viewV2.create({
// columns: ["Price", "ItemId"],
// })
// const result = await config.api.viewV2.getSchema(view.id)
// expect(result).toEqual({
// schema: {
// Price: {
// type: "number",
// name: "Price",
// constraints: {},
// },
// ItemId: {
// type: "string",
// name: "ItemId",
// constraints: {
// type: "string",
// },
// },
// },
// })
// })
// it("respects view column definition if exists", async () => {
// const view = await config.api.viewV2.create({
// columns: ["Price", "innexistingColumn"],
// })
// const result = await config.api.viewV2.getSchema(view.id)
// expect(result).toEqual({
// schema: {
// Price: {
// type: "number",
// name: "Price",
// constraints: {},
// },
// },
// })
// })
// })
})