Solve onEmptyFilter in a nicer way.

This commit is contained in:
Sam Rose 2024-04-11 10:11:03 +01:00
parent 5d2d84135c
commit 672025e176
No known key found for this signature in database
3 changed files with 12 additions and 38 deletions

View File

@ -17,11 +17,9 @@ import {
Row,
Table,
UserCtx,
EmptyFilterOption,
} from "@budibase/types"
import sdk from "../../../sdk"
import * as utils from "./utils"
import { dataFilters } from "@budibase/shared-core"
import {
inputProcessing,
outputProcessing,
@ -33,17 +31,6 @@ export async function handleRequest<T extends Operation>(
tableId: string,
opts?: RunConfig
): Promise<ExternalRequestReturnType<T>> {
// make sure the filters are cleaned up, no empty strings for equals, fuzzy or string
if (opts && opts.filters) {
opts.filters = sdk.rows.removeEmptyFilters(opts.filters)
}
if (
!dataFilters.hasFilters(opts?.filters) &&
opts?.filters?.onEmptyFilter === EmptyFilterOption.RETURN_NONE
) {
return [] as any
}
return new ExternalRequest<T>(operation, tableId, opts?.datasource).run(
opts || {}
)

View File

@ -22,7 +22,6 @@ import {
SortDirection,
SqlQueryBinding,
Table,
EmptyFilterOption,
} from "@budibase/types"
import environment from "../../environment"
@ -244,7 +243,6 @@ class InternalBuilder {
return query
}
filters = parseFilters(filters)
let noFilters = true
// if all or specified in filters, then everything is an or
const allOr = filters.allOr
if (filters.oneOf) {
@ -252,7 +250,6 @@ class InternalBuilder {
const fnc = allOr ? "orWhereIn" : "whereIn"
query = query[fnc](key, Array.isArray(array) ? array : [array])
})
noFilters = false
}
if (filters.string) {
iterate(filters.string, (key, value) => {
@ -268,11 +265,9 @@ class InternalBuilder {
])
}
})
noFilters = false
}
if (filters.fuzzy) {
iterate(filters.fuzzy, like)
noFilters = false
}
if (filters.range) {
iterate(filters.range, (key, value) => {
@ -305,59 +300,39 @@ class InternalBuilder {
query = query[fnc](key, "<", value.high)
}
})
noFilters = false
}
if (filters.equal) {
iterate(filters.equal, (key, value) => {
const fnc = allOr ? "orWhere" : "where"
query = query[fnc]({ [key]: value })
})
// Somewhere above us in the stack adds `{ type: "row" }` to the `equal`
// key before we get here, so we need to still consider it empty when
// that's the case.
const equalEmpty =
Object.keys(filters.equal).length === 1 && filters.equal.type === "row"
if (!equalEmpty) {
noFilters = false
}
}
if (filters.notEqual) {
iterate(filters.notEqual, (key, value) => {
const fnc = allOr ? "orWhereNot" : "whereNot"
query = query[fnc]({ [key]: value })
})
noFilters = false
}
if (filters.empty) {
iterate(filters.empty, key => {
const fnc = allOr ? "orWhereNull" : "whereNull"
query = query[fnc](key)
})
noFilters = false
}
if (filters.notEmpty) {
iterate(filters.notEmpty, key => {
const fnc = allOr ? "orWhereNotNull" : "whereNotNull"
query = query[fnc](key)
})
noFilters = false
}
if (filters.contains) {
contains(filters.contains)
noFilters = false
}
if (filters.notContains) {
contains(filters.notContains)
noFilters = false
}
if (filters.containsAny) {
contains(filters.containsAny, true)
noFilters = false
}
if (noFilters && filters.onEmptyFilter === EmptyFilterOption.RETURN_NONE) {
query = query.whereRaw("1=0")
}
return query

View File

@ -1,4 +1,5 @@
import {
EmptyFilterOption,
Row,
RowSearchParams,
SearchFilters,
@ -11,6 +12,7 @@ import { NoEmptyFilterStrings } from "../../../constants"
import * as sqs from "./search/sqs"
import env from "../../../environment"
import { ExportRowsParams, ExportRowsResult } from "./search/types"
import { dataFilters } from "@budibase/shared-core"
export { isValidFilter } from "../../../integrations/utils"
@ -60,6 +62,16 @@ export async function search(
options: RowSearchParams
): Promise<SearchResponse<Row>> {
const isExternalTable = isExternalTableID(options.tableId)
options.query = removeEmptyFilters(options.query)
if (
!dataFilters.hasFilters(options.query) &&
options.query.onEmptyFilter === EmptyFilterOption.RETURN_NONE
) {
return {
rows: [],
}
}
if (isExternalTable) {
return external.search(options)
} else if (env.SQS_SEARCH_ENABLE) {