Return data when schema is defined

This commit is contained in:
Adria Navarro 2023-07-25 16:12:04 +02:00
parent a7f3836eb9
commit d93c99b947
5 changed files with 37 additions and 10 deletions

View File

@ -0,0 +1,8 @@
export const CONSTANT_INTERNAL_ROW_COLS = [
"_id",
"_rev",
"type",
"createdAt",
"updatedAt",
"tableId",
] as const

View File

@ -2,3 +2,4 @@ export * from "./connections"
export * from "./DatabaseImpl" export * from "./DatabaseImpl"
export * from "./utils" export * from "./utils"
export { init, getPouch, getPouchDB, closePouchDB } from "./pouchDB" export { init, getPouch, getPouchDB, closePouchDB } from "./pouchDB"
export * from "./constants"

View File

@ -5,7 +5,7 @@ tk.freeze(timestamp)
import { outputProcessing } from "../../../utilities/rowProcessor" import { outputProcessing } from "../../../utilities/rowProcessor"
import * as setup from "./utilities" import * as setup from "./utilities"
const { basicRow } = setup.structures const { basicRow } = setup.structures
import { context, tenancy } from "@budibase/backend-core" import { context, db, tenancy } from "@budibase/backend-core"
import { quotas } from "@budibase/pro" import { quotas } from "@budibase/pro"
import { import {
QuotaUsageType, QuotaUsageType,
@ -969,7 +969,7 @@ describe("/rows", () => {
} }
) )
it("when schema is defined, no other columns are returned", async () => { it("when schema is defined, defined columns and row attributes are returned", async () => {
const table = await config.createTable(userTable()) const table = await config.createTable(userTable())
const rows = [] const rows = []
for (let i = 0; i < 10; i++) { for (let i = 0; i < 10; i++) {
@ -987,9 +987,25 @@ describe("/rows", () => {
}) })
const response = await config.api.viewV2.search(createViewResponse.id) const response = await config.api.viewV2.search(createViewResponse.id)
const anyRowAttributes: {
[K in (typeof db.CONSTANT_INTERNAL_ROW_COLS)[number]]: any
} = {
tableId: expect.anything(),
type: expect.anything(),
_id: expect.anything(),
_rev: expect.anything(),
createdAt: expect.anything(),
updatedAt: expect.anything(),
}
expect(response.body.rows).toHaveLength(10) expect(response.body.rows).toHaveLength(10)
expect(response.body.rows).toEqual( expect(response.body.rows).toEqual(
expect.arrayContaining(rows.map(r => ({ name: r.name }))) expect.arrayContaining(
rows.map(r => ({
...anyRowAttributes,
name: r.name,
}))
)
) )
}) })

View File

@ -3,7 +3,6 @@ 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
@ -37,12 +36,7 @@ export async function search(options: SearchParams): Promise<{
hasNextPage?: boolean hasNextPage?: boolean
bookmark?: number | null bookmark?: number | null
}> { }> {
const result = await pickApi(options.tableId).search(options) return 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 {

View File

@ -1,5 +1,6 @@
import { import {
context, context,
db,
SearchParams as InternalSearchParams, SearchParams as InternalSearchParams,
} from "@budibase/backend-core" } from "@budibase/backend-core"
import env from "../../../../environment" import env from "../../../../environment"
@ -28,6 +29,7 @@ import {
} from "../../../../api/controllers/view/utils" } from "../../../../api/controllers/view/utils"
import sdk from "../../../../sdk" import sdk from "../../../../sdk"
import { ExportRowsParams, ExportRowsResult, SearchParams } from "../search" import { ExportRowsParams, ExportRowsResult, SearchParams } from "../search"
import pick from "lodash/pick"
export async function search(options: SearchParams) { export async function search(options: SearchParams) {
const { tableId } = options const { tableId } = options
@ -72,6 +74,12 @@ export async function search(options: SearchParams) {
response.rows = await getGlobalUsersFromMetadata(response.rows) response.rows = await getGlobalUsersFromMetadata(response.rows)
} }
table = table || (await sdk.tables.getTable(tableId)) table = table || (await sdk.tables.getTable(tableId))
if (options.fields) {
const fields = [...options.fields, ...db.CONSTANT_INTERNAL_ROW_COLS]
response.rows = response.rows.map((r: any) => pick(r, fields))
}
response.rows = await outputProcessing(table, response.rows) response.rows = await outputProcessing(table, response.rows)
} }