Honor fields on views

This commit is contained in:
Adria Navarro 2024-08-28 19:05:14 +02:00
parent 1bc1db9b29
commit 383aad7265
5 changed files with 47 additions and 16 deletions

View File

@ -71,8 +71,11 @@ export async function searchView(
}) })
const searchOptions: RequiredKeys<SearchViewRowRequest> & const searchOptions: RequiredKeys<SearchViewRowRequest> &
RequiredKeys<Pick<RowSearchParams, "tableId" | "query" | "fields">> = { RequiredKeys<
Pick<RowSearchParams, "tableId" | "viewId" | "query" | "fields">
> = {
tableId: view.tableId, tableId: view.tableId,
viewId: view.id,
query: enrichedQuery, query: enrichedQuery,
fields: viewFields, fields: viewFields,
...getSortOptions(body, view), ...getSortOptions(body, view),

View File

@ -17,6 +17,7 @@ import {
FieldType, FieldType,
LinkDocumentValue, LinkDocumentValue,
Row, Row,
SquashTableFields,
Table, Table,
TableSchema, TableSchema,
} from "@budibase/types" } 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. * 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 table The table from which the rows originated.
* @param enriched The pre-enriched rows (full docs) which are to be squashed. * @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. * @returns The rows after having their links squashed to only contain the ID and primary display.
*/ */
export async function squashLinks<T = Row[] | Row>( export async function squashLinks<T = Row[] | Row>(
table: Table, table: Table,
enriched: T enriched: T,
squashFields?: SquashTableFields
): Promise<T> { ): Promise<T> {
// will populate this as we find them // will populate this as we find them
const linkedTables = [table] const linkedTables = [table]
@ -270,17 +273,11 @@ export async function squashLinks<T = Row[] | Row>(
const obj: any = { _id: link._id } const obj: any = { _id: link._id }
obj.primaryDisplay = getPrimaryDisplayValue(link, linkedTable) obj.primaryDisplay = getPrimaryDisplayValue(link, linkedTable)
// TODO if (squashFields && squashFields[column]) {
// const allowRelationshipSchemas = await features.flags.isEnabled( for (const relField of squashFields[column].visibleFieldNames) {
// FeatureFlag.ENRICHED_RELATIONSHIPS obj[relField] = link[relField]
// ) }
// if (schema.schema && allowRelationshipSchemas) { }
// for (const relField of Object.entries(schema.schema)
// .filter(([_, field]) => field.visible !== false)
// .map(([fieldKey]) => fieldKey)) {
// obj[relField] = link[relField]
// }
// }
newLinks.push(obj) newLinks.push(obj)
} }

View File

@ -86,16 +86,21 @@ export async function search(
options.query = removeInvalidFilters(options.query, queriableFields) options.query = removeInvalidFilters(options.query, queriableFields)
} }
let outputRowOptions
if (options.viewId) {
outputRowOptions = sdk.views.outputRowOptions(table, options.viewId)
}
let result: SearchResponse<Row> let result: SearchResponse<Row>
if (isExternalTable) { if (isExternalTable) {
span?.addTags({ searchType: "external" }) span?.addTags({ searchType: "external" })
result = await external.search(options, table) result = await external.search(options, table, outputRowOptions)
} else if (dbCore.isSqsEnabledForTenant()) { } else if (dbCore.isSqsEnabledForTenant()) {
span?.addTags({ searchType: "sqs" }) span?.addTags({ searchType: "sqs" })
result = await internal.sqs.search(options, table) result = await internal.sqs.search(options, table, { outputRowOptions })
} else { } else {
span?.addTags({ searchType: "lucene" }) span?.addTags({ searchType: "lucene" })
result = await internal.lucene.search(options, table) result = await internal.lucene.search(options, table, outputRowOptions)
} }
span?.addTags({ span?.addTags({

View File

@ -1,7 +1,9 @@
import { import {
FieldType, FieldType,
OutputRowOptions,
RelationSchemaField, RelationSchemaField,
RenameColumn, RenameColumn,
SquashTableFields,
Table, Table,
TableSchema, TableSchema,
View, View,
@ -250,3 +252,26 @@ export function syncSchema(
return view 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 }
}

View File

@ -5,6 +5,7 @@ import { WithRequired } from "../shared"
export interface SearchParams { export interface SearchParams {
tableId?: string tableId?: string
viewId?: string
query?: SearchFilters query?: SearchFilters
paginate?: boolean paginate?: boolean
bookmark?: string | number bookmark?: string | number