Merge pull request #11328 from Budibase/BUDI-7189/return_columns_based_on_schema

Honor schema on view search
This commit is contained in:
Adria Navarro 2023-07-24 16:04:24 +01:00 committed by GitHub
commit 626cb612e7
3 changed files with 56 additions and 1 deletions

View File

@ -199,12 +199,25 @@ export async function searchView(ctx: Ctx<void, SearchResponse>) {
ctx.throw(404, `View ${viewId} not found`) ctx.throw(404, `View ${viewId} not found`)
} }
if (view.version !== 2) {
ctx.throw(400, `This method only supports viewsV2`)
}
const table = await sdk.tables.getTable(view?.tableId)
const viewFields =
(view.columns &&
Object.entries(view.columns).length &&
Object.keys(sdk.views.enrichSchema(view, table.schema).schema)) ||
undefined
ctx.status = 200 ctx.status = 200
ctx.body = await quotas.addQuery( ctx.body = await quotas.addQuery(
() => () =>
sdk.rows.search({ sdk.rows.search({
tableId: view.tableId, tableId: view.tableId,
query: view.query || {}, query: view.query || {},
fields: viewFields,
...getSortOptions(ctx, view), ...getSortOptions(ctx, view),
}), }),
{ {

View File

@ -893,5 +893,38 @@ describe("/rows", () => {
}) })
} }
) )
it("when schema is defined, no other columns are returned", 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: { visible: true } },
})
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 })))
)
})
it("views without data can be returned", async () => {
const table = await config.createTable(userTable())
const createViewResponse = await config.api.viewV2.create()
const response = await config.api.viewV2.search(createViewResponse.id)
expect(response.body.rows).toHaveLength(0)
})
}) })
}) })

View File

@ -3,6 +3,7 @@ import { isExternalTable } from "../../../integrations/utils"
import * as internal from "./search/internal" import * as internal from "./search/internal"
import * as external from "./search/external" import * as external from "./search/external"
import { Format } from "../../../api/controllers/view/exporters" import { Format } from "../../../api/controllers/view/exporters"
import _ from "lodash"
export interface SearchParams { export interface SearchParams {
tableId: string tableId: string
@ -15,6 +16,7 @@ export interface SearchParams {
sortType?: SortType sortType?: SortType
version?: string version?: string
disableEscaping?: boolean disableEscaping?: boolean
fields?: string[]
} }
export interface ViewParams { export interface ViewParams {
@ -32,8 +34,15 @@ function pickApi(tableId: any) {
export async function search(options: SearchParams): Promise<{ export async function search(options: SearchParams): Promise<{
rows: any[] rows: any[]
hasNextPage?: boolean
bookmark?: number | null
}> { }> {
return pickApi(options.tableId).search(options) const result = await pickApi(options.tableId).search(options)
if (options.fields) {
result.rows = result.rows.map((r: any) => _.pick(r, options.fields!))
}
return result
} }
export interface ExportRowsParams { export interface ExportRowsParams {