Closed alrightsure closed 2 years ago
Another note, I have also tried the following:
async function selectPerson(gender: "male" | "female" | "other", firstName?: string | null) {
const query = db.selectFrom("person").where("gender", "=", gender);
if (firstName) {
query.where("first_name", "=", firstName);
}
return await query.execute();
}
selectPerson("male", "charles");
However, the second where clause is completely ignored from the query, even when firstName is passed. It seems any part of the query must be added to the initial chain.
updating as open as I closed the issue by mistake.
Unfortunately it's impossible to alert typescript of the checks. This is a perfect place for !
:
.if(firstName !== null && firstName !== undefined, qb => qb.where("first_name", "=", firstName!))
Everything is immutable in kysely, so you need to assign the results of operations back:
async function selectPerson(gender: "male" | "female" | "other", firstName?: string | null) {
let query = db.selectFrom("person").where("gender", "=", gender);
if (firstName) {
query = query.where("first_name", "=", firstName);
}
return await query.execute();
}
selectPerson("male", "charles");
Given the following interfaces:
The following function should allow the variable 'firstName' to be used in the where clause
However, the built in '.if()' function kysely does not alert typescript that 'firstName' is a string rather than null or undefined.
After playing with kysely for a bit, there doesn't seem to be a good way to do this other than have the entire query written twice with an external if check. Please let me know if I am misguided or doing something wrong.
Would love to use this library on a larger sized project at work, but there are a lot of instances where we need to check for optional props being passed to the calling function.