diff --git a/packages/server/src/api/controllers/table/index.ts b/packages/server/src/api/controllers/table/index.ts index 42bfedda8b..ab6c819a63 100644 --- a/packages/server/src/api/controllers/table/index.ts +++ b/packages/server/src/api/controllers/table/index.ts @@ -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) { ctx.body = response } +function enrichSchema( + tableSchema: TableSchema, + viewOverrides: Record +) { + 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 } diff --git a/packages/server/src/api/routes/tests/table.spec.ts b/packages/server/src/api/routes/tests/table.spec.ts index bd36b35c1e..297bbc79a5 100644 --- a/packages/server/src/api/routes/tests/table.spec.ts +++ b/packages/server/src/api/routes/tests/table.spec.ts @@ -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", }, diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index 22c12fcccf..c7e7698f75 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -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: {}, - // }, - // }, - // }) - // }) - // }) })