A few fixes for logical operators, there was a lot of cleanup that was not occurring as it is supposed to be recursive, this wasn't happening.

This commit is contained in:
mike12345567 2024-08-15 18:35:30 +01:00
parent 20baecc9fb
commit aa1eaa1d3d
4 changed files with 50 additions and 33 deletions

View File

@ -58,8 +58,9 @@ export async function searchView(
}) })
}) })
} else { } else {
const operator = query.allOr ? "$or" : "$and"
query = { query = {
$and: { [operator]: {
conditions: [query, body.query], conditions: [query, body.query],
}, },
} }

View File

@ -139,34 +139,29 @@ function cleanupFilters(
allTables.some(table => table.schema[key]) allTables.some(table => table.schema[key])
const splitter = new dataFilters.ColumnSplitter(allTables) const splitter = new dataFilters.ColumnSplitter(allTables)
for (const filterKey of Object.keys(filters) as (keyof SearchFilters)[]) {
const prefixFilters = (filters: SearchFilters) => { if (!isLogicalSearchOperator(filterKey)) {
for (const filterKey of Object.keys(filters) as (keyof SearchFilters)[]) { const filter = filters[filterKey]!
if (isLogicalSearchOperator(filterKey)) { if (typeof filter !== "object") {
for (const condition of filters[filterKey]!.conditions) { continue
prefixFilters(condition) }
} for (const key of Object.keys(filter)) {
} else { const { numberPrefix, relationshipPrefix, column } = splitter.run(key)
const filter = filters[filterKey]! if (keyInAnyTable(column)) {
if (typeof filter !== "object") { filter[
continue `${numberPrefix || ""}${relationshipPrefix || ""}${mapToUserColumn(
} column
for (const key of Object.keys(filter)) { )}`
const { numberPrefix, relationshipPrefix, column } = splitter.run(key) ] = filter[key]
if (keyInAnyTable(column)) { delete filter[key]
filter[
`${numberPrefix || ""}${
relationshipPrefix || ""
}${mapToUserColumn(column)}`
] = filter[key]
delete filter[key]
}
} }
} }
} }
} }
prefixFilters(filters)
return filters return dataFilters.recurseLogicalOperators(filters, (f: SearchFilters) => {
return cleanupFilters(f, table, allTables)
})
} }
function buildTableMap(tables: Table[]) { function buildTableMap(tables: Table[]) {

View File

@ -12,6 +12,7 @@ import { getSQLClient } from "./utils"
import { cloneDeep } from "lodash" import { cloneDeep } from "lodash"
import datasources from "../datasources" import datasources from "../datasources"
import { BudibaseInternalDB } from "../../../db/utils" import { BudibaseInternalDB } from "../../../db/utils"
import { dataFilters } from "@budibase/shared-core"
type PerformQueryFunction = ( type PerformQueryFunction = (
datasource: Datasource, datasource: Datasource,
@ -199,16 +200,20 @@ export default class AliasTables {
) )
} }
if (json.filters) { if (json.filters) {
for (let [filterKey, filter] of Object.entries(json.filters)) { const aliasFilters = (filters: SearchFilters): SearchFilters => {
if (typeof filter !== "object") { for (let [filterKey, filter] of Object.entries(filters)) {
continue if (typeof filter !== "object") {
continue
}
const aliasedFilters: typeof filter = {}
for (let key of Object.keys(filter)) {
aliasedFilters[this.aliasField(key)] = filter[key]
}
filters[filterKey as keyof SearchFilters] = aliasedFilters
} }
const aliasedFilters: typeof filter = {} return dataFilters.recurseLogicalOperators(filters, aliasFilters)
for (let key of Object.keys(filter)) {
aliasedFilters[this.aliasField(key)] = filter[key]
}
json.filters[filterKey as keyof SearchFilters] = aliasedFilters
} }
json.filters = aliasFilters(json.filters)
} }
if (json.meta?.table) { if (json.meta?.table) {
this.getAlias(json.meta.table.name) this.getAlias(json.meta.table.name)

View File

@ -113,6 +113,20 @@ export const NoEmptyFilterStrings = [
OperatorOptions.In.value, OperatorOptions.In.value,
] as (keyof SearchQueryFields)[] ] as (keyof SearchQueryFields)[]
export function recurseLogicalOperators(
filters: SearchFilters,
fn: (f: SearchFilters) => SearchFilters
) {
for (const logical of Object.values(LogicalOperator)) {
if (filters[logical]) {
filters[logical]!.conditions = filters[logical]!.conditions.map(
condition => fn(condition)
)
}
}
return filters
}
/** /**
* Removes any fields that contain empty strings that would cause inconsistent * Removes any fields that contain empty strings that would cause inconsistent
* behaviour with how backend tables are filtered (no value means no filter). * behaviour with how backend tables are filtered (no value means no filter).
@ -145,6 +159,7 @@ export const cleanupQuery = (query: SearchFilters) => {
} }
} }
} }
query = recurseLogicalOperators(query, cleanupQuery)
return query return query
} }
@ -410,6 +425,7 @@ export function fixupFilterArrays(filters: SearchFilters) {
} }
} }
} }
recurseLogicalOperators(filters, fixupFilterArrays)
return filters return filters
} }