Support outputSquash fields on searchs

This commit is contained in:
Adria Navarro 2024-08-28 18:58:01 +02:00
parent 73ec0bbf09
commit 1bc1db9b29
5 changed files with 31 additions and 7 deletions

View File

@ -1,6 +1,7 @@
import { import {
IncludeRelationship, IncludeRelationship,
Operation, Operation,
OutputRowOptions,
PaginationJson, PaginationJson,
Row, Row,
RowSearchParams, RowSearchParams,
@ -60,7 +61,8 @@ function getPaginationAndLimitParameters(
export async function search( export async function search(
options: RowSearchParams, options: RowSearchParams,
table: Table table: Table,
outputRowOptions?: OutputRowOptions
): Promise<SearchResponse<Row>> { ): Promise<SearchResponse<Row>> {
const { tableId } = options const { tableId } = options
const { countRows, paginate, query, ...params } = options const { countRows, paginate, query, ...params } = options
@ -115,6 +117,7 @@ export async function search(
let processed = await outputProcessing(table, rows, { let processed = await outputProcessing(table, rows, {
preserveLinks: true, preserveLinks: true,
squash: true, squash: true,
squashNestedFields: outputRowOptions?.squashNestedFields,
}) })
let hasNextPage = false let hasNextPage = false

View File

@ -2,6 +2,7 @@ import { PROTECTED_INTERNAL_COLUMNS } from "@budibase/shared-core"
import { fullSearch, paginatedSearch } from "../utils" import { fullSearch, paginatedSearch } from "../utils"
import { InternalTables } from "../../../../../db/utils" import { InternalTables } from "../../../../../db/utils"
import { import {
OutputRowOptions,
Row, Row,
RowSearchParams, RowSearchParams,
SearchResponse, SearchResponse,
@ -15,7 +16,8 @@ import pick from "lodash/pick"
export async function search( export async function search(
options: RowSearchParams, options: RowSearchParams,
table: Table table: Table,
outputRowOptions?: OutputRowOptions
): Promise<SearchResponse<Row>> { ): Promise<SearchResponse<Row>> {
const { tableId } = options const { tableId } = options
@ -59,7 +61,9 @@ export async function search(
response.rows = response.rows.map((r: any) => pick(r, fields)) response.rows = response.rows.map((r: any) => pick(r, fields))
} }
response.rows = await outputProcessing(table, response.rows) response.rows = await outputProcessing(table, response.rows, {
squashNestedFields: outputRowOptions?.squashNestedFields,
})
} }
return response return response

View File

@ -4,6 +4,7 @@ import {
FieldType, FieldType,
isLogicalSearchOperator, isLogicalSearchOperator,
Operation, Operation,
OutputRowOptions,
QueryJson, QueryJson,
RelationshipFieldMetadata, RelationshipFieldMetadata,
RelationshipsJson, RelationshipsJson,
@ -283,7 +284,7 @@ function resyncDefinitionsRequired(status: number, message: string) {
export async function search( export async function search(
options: RowSearchParams, options: RowSearchParams,
table: Table, table: Table,
opts?: { retrying?: boolean } opts?: { retrying?: boolean; outputRowOptions?: OutputRowOptions }
): Promise<SearchResponse<Row>> { ): Promise<SearchResponse<Row>> {
let { paginate, query, ...params } = options let { paginate, query, ...params } = options
@ -382,6 +383,7 @@ export async function search(
let finalRows = await outputProcessing(table, processed, { let finalRows = await outputProcessing(table, processed, {
preserveLinks: true, preserveLinks: true,
squash: true, squash: true,
squashNestedFields: opts?.outputRowOptions?.squashNestedFields,
}) })
// check if we need to pick specific rows out // check if we need to pick specific rows out
@ -409,7 +411,10 @@ export async function search(
const msg = typeof err === "string" ? err : err.message const msg = typeof err === "string" ? err : err.message
if (!opts?.retrying && resyncDefinitionsRequired(err.status, msg)) { if (!opts?.retrying && resyncDefinitionsRequired(err.status, msg)) {
await sdk.tables.sqs.syncDefinition() await sdk.tables.sqs.syncDefinition()
return search(options, table, { retrying: true }) return search(options, table, {
retrying: true,
outputRowOptions: opts?.outputRowOptions,
})
} }
// previously the internal table didn't error when a column didn't exist in search // previously the internal table didn't error when a column didn't exist in search
if (err.status === 400 && msg?.match(MISSING_COLUMN_REGEX)) { if (err.status === 400 && msg?.match(MISSING_COLUMN_REGEX)) {

View File

@ -16,6 +16,7 @@ import {
IdentityType, IdentityType,
Row, Row,
RowAttachment, RowAttachment,
SquashTableFields,
Table, Table,
User, User,
} from "@budibase/types" } from "@budibase/types"
@ -247,6 +248,7 @@ export async function outputProcessing<T extends Row[] | Row>(
preserveLinks?: boolean preserveLinks?: boolean
fromRow?: Row fromRow?: Row
skipBBReferences?: boolean skipBBReferences?: boolean
squashNestedFields?: SquashTableFields
} = { } = {
squash: true, squash: true,
preserveLinks: false, preserveLinks: false,
@ -342,8 +344,12 @@ export async function outputProcessing<T extends Row[] | Row>(
// process formulas after the complex types had been processed // process formulas after the complex types had been processed
enriched = await processFormulas(table, enriched, { dynamic: true }) enriched = await processFormulas(table, enriched, { dynamic: true })
if (opts.squash) { if (opts.squash || opts.squashNestedFields) {
enriched = await linkRows.squashLinks(table, enriched) enriched = await linkRows.squashLinks(
table,
enriched,
opts.squashNestedFields
)
} }
// remove null properties to match internal API // remove null properties to match internal API
const isExternal = isExternalTableID(table._id!) const isExternal = isExternalTableID(table._id!)

View File

@ -36,3 +36,9 @@ export enum RowExportFormat {
JSON = "json", JSON = "json",
JSON_WITH_SCHEMA = "jsonWithSchema", JSON_WITH_SCHEMA = "jsonWithSchema",
} }
export interface OutputRowOptions {
squashNestedFields: SquashTableFields
}
export type SquashTableFields = Record<string, { visibleFieldNames: string[] }>