Fix contains search on multi-user column.

This commit is contained in:
Sam Rose 2024-07-01 09:47:58 +01:00
parent 323a855d81
commit 11f49c95dc
No known key found for this signature in database
3 changed files with 25 additions and 4 deletions

View File

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

View File

@ -1938,6 +1938,15 @@ describe.each([
])
})
it("successfully finds a row searching with a string", async () => {
await expectQuery({
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()
})

View File

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