Don't clean relationship query
This commit is contained in:
parent
08b0b6af19
commit
1a88d9f89b
|
@ -6,7 +6,6 @@ import {
|
||||||
SearchResponse,
|
SearchResponse,
|
||||||
SortOrder,
|
SortOrder,
|
||||||
Table,
|
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"
|
||||||
|
@ -92,7 +91,7 @@ export async function search(
|
||||||
|
|
||||||
options.query = removeInvalidFilters(
|
options.query = removeInvalidFilters(
|
||||||
options.query,
|
options.query,
|
||||||
await getQueriableFields(options.fields, table.schema)
|
await getQueriableFields(options.fields, table)
|
||||||
)
|
)
|
||||||
|
|
||||||
let result: SearchResponse<Row>
|
let result: SearchResponse<Row>
|
||||||
|
@ -140,29 +139,35 @@ export async function fetchView(
|
||||||
|
|
||||||
async function getQueriableFields(
|
async function getQueriableFields(
|
||||||
fields: string[],
|
fields: string[],
|
||||||
schema: TableSchema
|
table: Table
|
||||||
): Promise<string[]> {
|
): Promise<string[]> {
|
||||||
const handledTables: Record<string, Table> = {}
|
const handledTables = new Set<string>([table._id!])
|
||||||
const extractTableFields = async (
|
const extractTableFields = async (
|
||||||
fromField: string,
|
table: Table,
|
||||||
tableId: string
|
allowedFields: string[]
|
||||||
): Promise<string[]> => {
|
): Promise<string[]> => {
|
||||||
const result = []
|
const result = []
|
||||||
if (handledTables[tableId]) {
|
for (const field of Object.keys(table.schema).filter(f =>
|
||||||
return []
|
allowedFields.includes(f)
|
||||||
}
|
)) {
|
||||||
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]
|
const subSchema = table.schema[field]
|
||||||
if (subSchema.type === FieldType.LINK) {
|
if (subSchema.type === FieldType.LINK) {
|
||||||
result.push(
|
if (handledTables.has(`${table._id}_${subSchema.tableId}`)) {
|
||||||
...(await extractTableFields(formattedColumn, subSchema.tableId))
|
// avoid circular loops
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
handledTables.add(`${subSchema.tableId}_${table._id}`)
|
||||||
|
const relatedTable = await sdk.tables.getTable(subSchema.tableId)
|
||||||
|
const relatedFields = await extractTableFields(
|
||||||
|
relatedTable,
|
||||||
|
Object.keys(relatedTable.schema)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
result.push(...relatedFields.map(f => `${subSchema.name}.${f}`))
|
||||||
|
// should be able to filter by relationship using table name
|
||||||
|
result.push(...relatedFields.map(f => `${relatedTable.name}.${f}`))
|
||||||
} else {
|
} else {
|
||||||
result.push(formattedColumn)
|
result.push(field)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
|
@ -172,13 +177,7 @@ async function getQueriableFields(
|
||||||
"_id", // Querying by _id is always allowed, even if it's never part of the schema
|
"_id", // Querying by _id is always allowed, even if it's never part of the schema
|
||||||
]
|
]
|
||||||
|
|
||||||
for (const field of fields) {
|
result.push(...(await extractTableFields(table, fields)))
|
||||||
if (schema[field].type === FieldType.LINK) {
|
|
||||||
result.push(...(await extractTableFields(field, schema[field].tableId)))
|
|
||||||
} else {
|
|
||||||
result.push(field)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue