Conditional query failing
I have a query like so:
Why is this happening?
const userId = 'abcdefg'
let selectAll = true; // or false (Condition)
let arr = [];
if (urlCondition1) {
statusFilter.push(eq(table.status, param1));
}
if (urlCondition2) {
statusFilter.push(eq(table.status, param2));
}
if (urlCondition3) {
nameFilter.push(or(ilike(table.name, `%${param3}%`)))
}
// Below produces wrong output
await db.select().from(table).where(
and(
eq(table.userId, userId),
selectAll
? (or(...statusFilter), or(...nameFilter))
: inArray(table.userId, arr)
)
);
// Below produces correct output
await db.select().from(table).where(
and(
eq(table.userId, userId),
or(...statusFilter),
or(...nameFilter))
)
);const userId = 'abcdefg'
let selectAll = true; // or false (Condition)
let arr = [];
if (urlCondition1) {
statusFilter.push(eq(table.status, param1));
}
if (urlCondition2) {
statusFilter.push(eq(table.status, param2));
}
if (urlCondition3) {
nameFilter.push(or(ilike(table.name, `%${param3}%`)))
}
// Below produces wrong output
await db.select().from(table).where(
and(
eq(table.userId, userId),
selectAll
? (or(...statusFilter), or(...nameFilter))
: inArray(table.userId, arr)
)
);
// Below produces correct output
await db.select().from(table).where(
and(
eq(table.userId, userId),
or(...statusFilter),
or(...nameFilter))
)
);Why is this happening?