Fix contains search on multi-user column.
This commit is contained in:
parent
323a855d81
commit
11f49c95dc
|
@ -246,7 +246,11 @@ class InternalBuilder {
|
||||||
return `[${value.join(",")}]`
|
return `[${value.join(",")}]`
|
||||||
}
|
}
|
||||||
if (this.client === SqlClient.POSTGRES) {
|
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 wrap = any ? "" : "'"
|
||||||
const op = any ? "\\?| array" : "@>"
|
const op = any ? "\\?| array" : "@>"
|
||||||
const fieldNames = key.split(/\./g)
|
const fieldNames = key.split(/\./g)
|
||||||
|
@ -261,7 +265,11 @@ 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: Array<any>) => {
|
iterate(mode, (key: string, value: 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
|
||||||
|
@ -270,7 +278,11 @@ class InternalBuilder {
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
const andOr = mode === filters?.containsAny ? " OR " : " AND "
|
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 = ""
|
let statement = ""
|
||||||
for (let i in value) {
|
for (let i in value) {
|
||||||
if (typeof value[i] === "string") {
|
if (typeof value[i] === "string") {
|
||||||
|
|
|
@ -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 () => {
|
it("fails to find nonexistent row", async () => {
|
||||||
await expectQuery({ contains: { users: ["us_none"] } }).toFindNothing()
|
await expectQuery({ contains: { users: ["us_none"] } }).toFindNothing()
|
||||||
})
|
})
|
||||||
|
|
|
@ -54,7 +54,7 @@ export interface SearchFilters {
|
||||||
[key: string]: any[]
|
[key: string]: any[]
|
||||||
}
|
}
|
||||||
[SearchFilterOperator.CONTAINS]?: {
|
[SearchFilterOperator.CONTAINS]?: {
|
||||||
[key: string]: any[]
|
[key: string]: any[] | any
|
||||||
}
|
}
|
||||||
[SearchFilterOperator.NOT_CONTAINS]?: {
|
[SearchFilterOperator.NOT_CONTAINS]?: {
|
||||||
[key: string]: any[]
|
[key: string]: any[]
|
||||||
|
|
Loading…
Reference in New Issue