Fix search relationships
This commit is contained in:
parent
59a164f4df
commit
08b0b6af19
|
@ -1,9 +1,12 @@
|
||||||
import {
|
import {
|
||||||
EmptyFilterOption,
|
EmptyFilterOption,
|
||||||
|
FieldType,
|
||||||
Row,
|
Row,
|
||||||
RowSearchParams,
|
RowSearchParams,
|
||||||
SearchResponse,
|
SearchResponse,
|
||||||
SortOrder,
|
SortOrder,
|
||||||
|
Table,
|
||||||
|
TableSchema,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import { isExternalTableID } from "../../../integrations/utils"
|
import { isExternalTableID } from "../../../integrations/utils"
|
||||||
import * as internal from "./search/internal"
|
import * as internal from "./search/internal"
|
||||||
|
@ -87,10 +90,10 @@ export async function search(
|
||||||
options.fields = visibleTableFields
|
options.fields = visibleTableFields
|
||||||
}
|
}
|
||||||
|
|
||||||
options.query = removeInvalidFilters(options.query, [
|
options.query = removeInvalidFilters(
|
||||||
"_id",
|
options.query,
|
||||||
...options.fields,
|
await getQueriableFields(options.fields, table.schema)
|
||||||
])
|
)
|
||||||
|
|
||||||
let result: SearchResponse<Row>
|
let result: SearchResponse<Row>
|
||||||
if (isExternalTable) {
|
if (isExternalTable) {
|
||||||
|
@ -134,3 +137,48 @@ export async function fetchView(
|
||||||
): Promise<Row[]> {
|
): Promise<Row[]> {
|
||||||
return pickApi(tableId).fetchView(viewName, params)
|
return pickApi(tableId).fetchView(viewName, params)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function getQueriableFields(
|
||||||
|
fields: string[],
|
||||||
|
schema: TableSchema
|
||||||
|
): Promise<string[]> {
|
||||||
|
const handledTables: Record<string, Table> = {}
|
||||||
|
const extractTableFields = async (
|
||||||
|
fromField: string,
|
||||||
|
tableId: string
|
||||||
|
): Promise<string[]> => {
|
||||||
|
const result = []
|
||||||
|
if (handledTables[tableId]) {
|
||||||
|
return []
|
||||||
|
}
|
||||||
|
const table = await sdk.tables.getTable(tableId)
|
||||||
|
handledTables[tableId] = table
|
||||||
|
|
||||||
|
for (const field of Object.keys(table.schema)) {
|
||||||
|
const formattedColumn = `${fromField}.${field}`
|
||||||
|
const subSchema = table.schema[field]
|
||||||
|
if (subSchema.type === FieldType.LINK) {
|
||||||
|
result.push(
|
||||||
|
...(await extractTableFields(formattedColumn, subSchema.tableId))
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
result.push(formattedColumn)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = [
|
||||||
|
"_id", // Querying by _id is always allowed, even if it's never part of the schema
|
||||||
|
]
|
||||||
|
|
||||||
|
for (const field of fields) {
|
||||||
|
if (schema[field].type === FieldType.LINK) {
|
||||||
|
result.push(...(await extractTableFields(field, schema[field].tableId)))
|
||||||
|
} else {
|
||||||
|
result.push(field)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue