From 383aad72659792dc4c9b5b89dc548439be89aa22 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Wed, 28 Aug 2024 19:05:14 +0200 Subject: [PATCH] Honor fields on views --- .../server/src/api/controllers/row/views.ts | 5 +++- packages/server/src/db/linkedRows/index.ts | 21 +++++++--------- packages/server/src/sdk/app/rows/search.ts | 11 +++++--- packages/server/src/sdk/app/views/index.ts | 25 +++++++++++++++++++ packages/types/src/sdk/row.ts | 1 + 5 files changed, 47 insertions(+), 16 deletions(-) diff --git a/packages/server/src/api/controllers/row/views.ts b/packages/server/src/api/controllers/row/views.ts index 0b2bb8c203..c5e227740d 100644 --- a/packages/server/src/api/controllers/row/views.ts +++ b/packages/server/src/api/controllers/row/views.ts @@ -71,8 +71,11 @@ export async function searchView( }) const searchOptions: RequiredKeys & - RequiredKeys> = { + RequiredKeys< + Pick + > = { tableId: view.tableId, + viewId: view.id, query: enrichedQuery, fields: viewFields, ...getSortOptions(body, view), diff --git a/packages/server/src/db/linkedRows/index.ts b/packages/server/src/db/linkedRows/index.ts index fe1d534f9f..54acb1f8d6 100644 --- a/packages/server/src/db/linkedRows/index.ts +++ b/packages/server/src/db/linkedRows/index.ts @@ -17,6 +17,7 @@ import { FieldType, LinkDocumentValue, Row, + SquashTableFields, Table, TableSchema, } from "@budibase/types" @@ -245,11 +246,13 @@ function getPrimaryDisplayValue(row: Row, table?: Table) { * This function will take the given enriched rows and squash the links to only contain the primary display field. * @param table The table from which the rows originated. * @param enriched The pre-enriched rows (full docs) which are to be squashed. + * @param squashFields Per link column (key) define which columns are allowed while squashing. * @returns The rows after having their links squashed to only contain the ID and primary display. */ export async function squashLinks( table: Table, - enriched: T + enriched: T, + squashFields?: SquashTableFields ): Promise { // will populate this as we find them const linkedTables = [table] @@ -270,17 +273,11 @@ export async function squashLinks( const obj: any = { _id: link._id } obj.primaryDisplay = getPrimaryDisplayValue(link, linkedTable) - // TODO - // const allowRelationshipSchemas = await features.flags.isEnabled( - // FeatureFlag.ENRICHED_RELATIONSHIPS - // ) - // if (schema.schema && allowRelationshipSchemas) { - // for (const relField of Object.entries(schema.schema) - // .filter(([_, field]) => field.visible !== false) - // .map(([fieldKey]) => fieldKey)) { - // obj[relField] = link[relField] - // } - // } + if (squashFields && squashFields[column]) { + for (const relField of squashFields[column].visibleFieldNames) { + obj[relField] = link[relField] + } + } newLinks.push(obj) } diff --git a/packages/server/src/sdk/app/rows/search.ts b/packages/server/src/sdk/app/rows/search.ts index 482621630c..cedad3b16b 100644 --- a/packages/server/src/sdk/app/rows/search.ts +++ b/packages/server/src/sdk/app/rows/search.ts @@ -86,16 +86,21 @@ export async function search( options.query = removeInvalidFilters(options.query, queriableFields) } + let outputRowOptions + if (options.viewId) { + outputRowOptions = sdk.views.outputRowOptions(table, options.viewId) + } + let result: SearchResponse if (isExternalTable) { span?.addTags({ searchType: "external" }) - result = await external.search(options, table) + result = await external.search(options, table, outputRowOptions) } else if (dbCore.isSqsEnabledForTenant()) { span?.addTags({ searchType: "sqs" }) - result = await internal.sqs.search(options, table) + result = await internal.sqs.search(options, table, { outputRowOptions }) } else { span?.addTags({ searchType: "lucene" }) - result = await internal.lucene.search(options, table) + result = await internal.lucene.search(options, table, outputRowOptions) } span?.addTags({ diff --git a/packages/server/src/sdk/app/views/index.ts b/packages/server/src/sdk/app/views/index.ts index 493104d252..c3cf58f21e 100644 --- a/packages/server/src/sdk/app/views/index.ts +++ b/packages/server/src/sdk/app/views/index.ts @@ -1,7 +1,9 @@ import { FieldType, + OutputRowOptions, RelationSchemaField, RenameColumn, + SquashTableFields, Table, TableSchema, View, @@ -250,3 +252,26 @@ export function syncSchema( return view } + +export function outputRowOptions( + table: Table, + viewId: string +): OutputRowOptions { + const view = Object.values(table.views || {}).find( + (v): v is ViewV2 => sdk.views.isV2(v) && v.id === viewId + ) + const viewSchema = view?.schema || {} + + const squashFields: SquashTableFields = {} + for (const key of Object.keys(viewSchema)) { + if (viewSchema[key].columns) { + squashFields[key] = { + visibleFieldNames: Object.entries(viewSchema[key].columns) + .filter(([_, c]) => c.visible !== false) + .map(([columnName]) => columnName), + } + } + } + + return { squashNestedFields: squashFields } +} diff --git a/packages/types/src/sdk/row.ts b/packages/types/src/sdk/row.ts index 2790ceb994..fe51bd850d 100644 --- a/packages/types/src/sdk/row.ts +++ b/packages/types/src/sdk/row.ts @@ -5,6 +5,7 @@ import { WithRequired } from "../shared" export interface SearchParams { tableId?: string + viewId?: string query?: SearchFilters paginate?: boolean bookmark?: string | number