Honor schema on view search

This commit is contained in:
Adria Navarro 2023-07-21 18:30:17 +02:00
parent c58b145afd
commit 2186b0407a
2 changed files with 33 additions and 1 deletions

View File

@ -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<void, SearchResponse>) {
}
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<void, SearchResponse>) {
datasourceId: view.tableId,
}
)
const { columns } = view
if (columns) {
rows = rows.map(r => _.pick(r, columns))
}
ctx.body = { rows }
}
export async function validate(ctx: Ctx) {

View File

@ -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 })))
)
})
})
})