From 2186b0407af82c34f81dbce7748e232b1fd18502 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Fri, 21 Jul 2023 18:30:17 +0200 Subject: [PATCH] Honor schema on view search --- .../server/src/api/controllers/row/index.ts | 10 +++++++- .../server/src/api/routes/tests/row.spec.ts | 24 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/packages/server/src/api/controllers/row/index.ts b/packages/server/src/api/controllers/row/index.ts index 8ba2f4b0ba..0b17a35772 100644 --- a/packages/server/src/api/controllers/row/index.ts +++ b/packages/server/src/api/controllers/row/index.ts @@ -8,6 +8,7 @@ import { gridSocket } from "../../../websockets" import sdk from "../../../sdk" import * as exporters from "../view/exporters" import { apiFileReturn } from "../../../utilities/fileSystem" +import _ from "lodash" function pickApi(tableId: any) { if (isExternalTable(tableId)) { @@ -155,7 +156,7 @@ export async function searchView(ctx: Ctx) { } ctx.status = 200 - ctx.body = await quotas.addQuery( + let { rows } = await quotas.addQuery( () => sdk.rows.search({ tableId: view.tableId, @@ -168,6 +169,13 @@ export async function searchView(ctx: Ctx) { datasourceId: view.tableId, } ) + + const { columns } = view + if (columns) { + rows = rows.map(r => _.pick(r, columns)) + } + + ctx.body = { rows } } export async function validate(ctx: Ctx) { diff --git a/packages/server/src/api/routes/tests/row.spec.ts b/packages/server/src/api/routes/tests/row.spec.ts index 2890085c88..ba0afd1fcf 100644 --- a/packages/server/src/api/routes/tests/row.spec.ts +++ b/packages/server/src/api/routes/tests/row.spec.ts @@ -841,5 +841,29 @@ describe("/rows", () => { rows: expected.map(name => expect.objectContaining({ name })), }) }) + + it("when schema is defined, no other columns are returnd", async () => { + const table = await config.createTable(userTable()) + const rows = [] + for (let i = 0; i < 10; i++) { + rows.push( + await config.createRow({ + tableId: table._id, + name: generator.name(), + age: generator.age(), + }) + ) + } + + const createViewResponse = await config.api.viewV2.create({ + columns: ["name"], + }) + const response = await config.api.viewV2.search(createViewResponse.id) + + expect(response.body.rows).toHaveLength(10) + expect(response.body.rows).toEqual( + expect.arrayContaining(rows.map(r => ({ name: r.name }))) + ) + }) }) })