From d93c99b947e6ad6c60437d59dfd0de8e95ac5c7e Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 25 Jul 2023 16:12:04 +0200 Subject: [PATCH] Return data when schema is defined --- .../backend-core/src/db/couch/constants.ts | 8 +++++++ packages/backend-core/src/db/couch/index.ts | 1 + .../server/src/api/routes/tests/row.spec.ts | 22 ++++++++++++++++--- packages/server/src/sdk/app/rows/search.ts | 8 +------ .../src/sdk/app/rows/search/internal.ts | 8 +++++++ 5 files changed, 37 insertions(+), 10 deletions(-) create mode 100644 packages/backend-core/src/db/couch/constants.ts diff --git a/packages/backend-core/src/db/couch/constants.ts b/packages/backend-core/src/db/couch/constants.ts new file mode 100644 index 0000000000..0b13f8f7b3 --- /dev/null +++ b/packages/backend-core/src/db/couch/constants.ts @@ -0,0 +1,8 @@ +export const CONSTANT_INTERNAL_ROW_COLS = [ + "_id", + "_rev", + "type", + "createdAt", + "updatedAt", + "tableId", +] as const diff --git a/packages/backend-core/src/db/couch/index.ts b/packages/backend-core/src/db/couch/index.ts index c731d20d6c..e7dec00d0e 100644 --- a/packages/backend-core/src/db/couch/index.ts +++ b/packages/backend-core/src/db/couch/index.ts @@ -2,3 +2,4 @@ export * from "./connections" export * from "./DatabaseImpl" export * from "./utils" export { init, getPouch, getPouchDB, closePouchDB } from "./pouchDB" +export * from "./constants" diff --git a/packages/server/src/api/routes/tests/row.spec.ts b/packages/server/src/api/routes/tests/row.spec.ts index d7279148c7..7ce9c12bac 100644 --- a/packages/server/src/api/routes/tests/row.spec.ts +++ b/packages/server/src/api/routes/tests/row.spec.ts @@ -5,7 +5,7 @@ tk.freeze(timestamp) import { outputProcessing } from "../../../utilities/rowProcessor" import * as setup from "./utilities" 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 { 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 rows = [] for (let i = 0; i < 10; i++) { @@ -987,9 +987,25 @@ describe("/rows", () => { }) 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).toEqual( - expect.arrayContaining(rows.map(r => ({ name: r.name }))) + expect.arrayContaining( + rows.map(r => ({ + ...anyRowAttributes, + name: r.name, + })) + ) ) }) diff --git a/packages/server/src/sdk/app/rows/search.ts b/packages/server/src/sdk/app/rows/search.ts index 4937460686..a523d828f3 100644 --- a/packages/server/src/sdk/app/rows/search.ts +++ b/packages/server/src/sdk/app/rows/search.ts @@ -3,7 +3,6 @@ import { isExternalTable } from "../../../integrations/utils" import * as internal from "./search/internal" import * as external from "./search/external" import { Format } from "../../../api/controllers/view/exporters" -import _ from "lodash" export interface SearchParams { tableId: string @@ -37,12 +36,7 @@ export async function search(options: SearchParams): Promise<{ hasNextPage?: boolean bookmark?: number | null }> { - const result = await pickApi(options.tableId).search(options) - - if (options.fields) { - result.rows = result.rows.map((r: any) => _.pick(r, options.fields!)) - } - return result + return pickApi(options.tableId).search(options) } export interface ExportRowsParams { diff --git a/packages/server/src/sdk/app/rows/search/internal.ts b/packages/server/src/sdk/app/rows/search/internal.ts index fb0e35b650..5a29541705 100644 --- a/packages/server/src/sdk/app/rows/search/internal.ts +++ b/packages/server/src/sdk/app/rows/search/internal.ts @@ -1,5 +1,6 @@ import { context, + db, SearchParams as InternalSearchParams, } from "@budibase/backend-core" import env from "../../../../environment" @@ -28,6 +29,7 @@ import { } from "../../../../api/controllers/view/utils" import sdk from "../../../../sdk" import { ExportRowsParams, ExportRowsResult, SearchParams } from "../search" +import pick from "lodash/pick" export async function search(options: SearchParams) { const { tableId } = options @@ -72,6 +74,12 @@ export async function search(options: SearchParams) { response.rows = await getGlobalUsersFromMetadata(response.rows) } 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) }