Merge pull request #14059 from Budibase/budi-8404-filtering-with-the-contains-operator-throws-an-error-when
Fix contains search on multi-user column.
This commit is contained in:
commit
9e7b51c7e5
|
@ -1938,6 +1938,17 @@ describe.each([
|
|||
])
|
||||
})
|
||||
|
||||
it("successfully finds a row searching with a string", async () => {
|
||||
await expectQuery({
|
||||
// @ts-expect-error this test specifically goes against the type to
|
||||
// test that we coerce the string to an array.
|
||||
contains: { "1:users": user1._id },
|
||||
}).toContainExactly([
|
||||
{ users: [{ _id: user1._id }] },
|
||||
{ users: [{ _id: user1._id }, { _id: user2._id }] },
|
||||
])
|
||||
})
|
||||
|
||||
it("fails to find nonexistent row", async () => {
|
||||
await expectQuery({ contains: { users: ["us_none"] } }).toFindNothing()
|
||||
})
|
||||
|
|
|
@ -2,6 +2,7 @@ import {
|
|||
EmptyFilterOption,
|
||||
Row,
|
||||
RowSearchParams,
|
||||
SearchFilterOperator,
|
||||
SearchFilters,
|
||||
SearchResponse,
|
||||
SortOrder,
|
||||
|
@ -65,11 +66,37 @@ export function removeEmptyFilters(filters: SearchFilters) {
|
|||
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(
|
||||
options: RowSearchParams
|
||||
): Promise<SearchResponse<Row>> {
|
||||
const isExternalTable = isExternalTableID(options.tableId)
|
||||
options.query = removeEmptyFilters(options.query || {})
|
||||
options.query = fixupFilterArrays(options.query)
|
||||
if (
|
||||
!dataFilters.hasFilters(options.query) &&
|
||||
options.query.onEmptyFilter === EmptyFilterOption.RETURN_NONE
|
||||
|
|
Loading…
Reference in New Issue