Respond to PR feedback.

This commit is contained in:
Sam Rose 2024-07-01 11:41:21 +01:00
parent 11f49c95dc
commit 6eb37df92d
No known key found for this signature in database
3 changed files with 31 additions and 16 deletions

View File

@ -246,11 +246,7 @@ class InternalBuilder {
return `[${value.join(",")}]` return `[${value.join(",")}]`
} }
if (this.client === SqlClient.POSTGRES) { if (this.client === SqlClient.POSTGRES) {
iterate(mode, (key: string, value: any) => { iterate(mode, (key: string, value: Array<any>) => {
if (!Array.isArray(value)) {
value = [value]
}
const wrap = any ? "" : "'" const wrap = any ? "" : "'"
const op = any ? "\\?| array" : "@>" const op = any ? "\\?| array" : "@>"
const fieldNames = key.split(/\./g) const fieldNames = key.split(/\./g)
@ -265,11 +261,7 @@ class InternalBuilder {
}) })
} else if (this.client === SqlClient.MY_SQL) { } else if (this.client === SqlClient.MY_SQL) {
const jsonFnc = any ? "JSON_OVERLAPS" : "JSON_CONTAINS" const jsonFnc = any ? "JSON_OVERLAPS" : "JSON_CONTAINS"
iterate(mode, (key: string, value: any) => { iterate(mode, (key: string, value: Array<any>) => {
if (!Array.isArray(value)) {
value = [value]
}
query = query[rawFnc]( query = query[rawFnc](
`${not}COALESCE(${jsonFnc}(${key}, '${stringifyArray( `${not}COALESCE(${jsonFnc}(${key}, '${stringifyArray(
value value
@ -278,11 +270,7 @@ class InternalBuilder {
}) })
} else { } else {
const andOr = mode === filters?.containsAny ? " OR " : " AND " const andOr = mode === filters?.containsAny ? " OR " : " AND "
iterate(mode, (key: string, value: any) => { iterate(mode, (key: string, value: Array<any>) => {
if (!Array.isArray(value)) {
value = [value]
}
let statement = "" let statement = ""
for (let i in value) { for (let i in value) {
if (typeof value[i] === "string") { if (typeof value[i] === "string") {

View File

@ -2,6 +2,7 @@ import {
EmptyFilterOption, EmptyFilterOption,
Row, Row,
RowSearchParams, RowSearchParams,
SearchFilterOperator,
SearchFilters, SearchFilters,
SearchResponse, SearchResponse,
SortOrder, SortOrder,
@ -65,11 +66,37 @@ export function removeEmptyFilters(filters: SearchFilters) {
return filters return filters
} }
// The frontend can send single values for array fields sometimes, so to handle
// this we convert them to arrays at the controller level so that nothing below
// this has to worry about the non-array values.
function fixupFilterArrays(filters: SearchFilters) {
const arrayFields = [
SearchFilterOperator.ONE_OF,
SearchFilterOperator.CONTAINS,
SearchFilterOperator.NOT_CONTAINS,
SearchFilterOperator.CONTAINS_ANY,
]
for (const searchField of arrayFields) {
const field = filters[searchField]
if (field == null) {
continue
}
for (const key of Object.keys(field)) {
if (!Array.isArray(field[key])) {
field[key] = [field[key]]
}
}
}
return filters
}
export async function search( 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 || {}) options.query = removeEmptyFilters(options.query || {})
options.query = fixupFilterArrays(options.query)
if ( if (
!dataFilters.hasFilters(options.query) && !dataFilters.hasFilters(options.query) &&
options.query.onEmptyFilter === EmptyFilterOption.RETURN_NONE options.query.onEmptyFilter === EmptyFilterOption.RETURN_NONE

View File

@ -54,7 +54,7 @@ export interface SearchFilters {
[key: string]: any[] [key: string]: any[]
} }
[SearchFilterOperator.CONTAINS]?: { [SearchFilterOperator.CONTAINS]?: {
[key: string]: any[] | any [key: string]: any[]
} }
[SearchFilterOperator.NOT_CONTAINS]?: { [SearchFilterOperator.NOT_CONTAINS]?: {
[key: string]: any[] [key: string]: any[]