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, Row,
Table, Table,
UserCtx, UserCtx,
EmptyFilterOption,
} from "@budibase/types" } from "@budibase/types"
import sdk from "../../../sdk" import sdk from "../../../sdk"
import * as utils from "./utils" import * as utils from "./utils"
import { dataFilters } from "@budibase/shared-core"
import { import {
inputProcessing, inputProcessing,
outputProcessing, outputProcessing,
@ -33,17 +31,6 @@ export async function handleRequest<T extends Operation>(
tableId: string, tableId: string,
opts?: RunConfig opts?: RunConfig
): Promise<ExternalRequestReturnType<T>> { ): 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( return new ExternalRequest<T>(operation, tableId, opts?.datasource).run(
opts || {} opts || {}
) )

View File

@ -22,7 +22,6 @@ import {
SortDirection, SortDirection,
SqlQueryBinding, SqlQueryBinding,
Table, Table,
EmptyFilterOption,
} from "@budibase/types" } from "@budibase/types"
import environment from "../../environment" import environment from "../../environment"
@ -244,7 +243,6 @@ class InternalBuilder {
return query return query
} }
filters = parseFilters(filters) filters = parseFilters(filters)
let noFilters = true
// if all or specified in filters, then everything is an or // if all or specified in filters, then everything is an or
const allOr = filters.allOr const allOr = filters.allOr
if (filters.oneOf) { if (filters.oneOf) {
@ -252,7 +250,6 @@ class InternalBuilder {
const fnc = allOr ? "orWhereIn" : "whereIn" const fnc = allOr ? "orWhereIn" : "whereIn"
query = query[fnc](key, Array.isArray(array) ? array : [array]) query = query[fnc](key, Array.isArray(array) ? array : [array])
}) })
noFilters = false
} }
if (filters.string) { if (filters.string) {
iterate(filters.string, (key, value) => { iterate(filters.string, (key, value) => {
@ -268,11 +265,9 @@ class InternalBuilder {
]) ])
} }
}) })
noFilters = false
} }
if (filters.fuzzy) { if (filters.fuzzy) {
iterate(filters.fuzzy, like) iterate(filters.fuzzy, like)
noFilters = false
} }
if (filters.range) { if (filters.range) {
iterate(filters.range, (key, value) => { iterate(filters.range, (key, value) => {
@ -305,59 +300,39 @@ class InternalBuilder {
query = query[fnc](key, "<", value.high) query = query[fnc](key, "<", value.high)
} }
}) })
noFilters = false
} }
if (filters.equal) { if (filters.equal) {
iterate(filters.equal, (key, value) => { iterate(filters.equal, (key, value) => {
const fnc = allOr ? "orWhere" : "where" const fnc = allOr ? "orWhere" : "where"
query = query[fnc]({ [key]: value }) 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) { if (filters.notEqual) {
iterate(filters.notEqual, (key, value) => { iterate(filters.notEqual, (key, value) => {
const fnc = allOr ? "orWhereNot" : "whereNot" const fnc = allOr ? "orWhereNot" : "whereNot"
query = query[fnc]({ [key]: value }) query = query[fnc]({ [key]: value })
}) })
noFilters = false
} }
if (filters.empty) { if (filters.empty) {
iterate(filters.empty, key => { iterate(filters.empty, key => {
const fnc = allOr ? "orWhereNull" : "whereNull" const fnc = allOr ? "orWhereNull" : "whereNull"
query = query[fnc](key) query = query[fnc](key)
}) })
noFilters = false
} }
if (filters.notEmpty) { if (filters.notEmpty) {
iterate(filters.notEmpty, key => { iterate(filters.notEmpty, key => {
const fnc = allOr ? "orWhereNotNull" : "whereNotNull" const fnc = allOr ? "orWhereNotNull" : "whereNotNull"
query = query[fnc](key) query = query[fnc](key)
}) })
noFilters = false
} }
if (filters.contains) { if (filters.contains) {
contains(filters.contains) contains(filters.contains)
noFilters = false
} }
if (filters.notContains) { if (filters.notContains) {
contains(filters.notContains) contains(filters.notContains)
noFilters = false
} }
if (filters.containsAny) { if (filters.containsAny) {
contains(filters.containsAny, true) contains(filters.containsAny, true)
noFilters = false
}
if (noFilters && filters.onEmptyFilter === EmptyFilterOption.RETURN_NONE) {
query = query.whereRaw("1=0")
} }
return query return query

View File

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