DT
Drizzle Team•3mo ago
Dari

Using function in "and" instead of dynamic query

Hey i was wondering if this would work or if there would be any downsides
await db.query.myTable.findFirst({
where: and(
eq(myTable.identifier, params.identifier),
eq(myTable.userId, params.userId),
this.optionalAnd()
),
});

function optionalAnd() {
// some complex synchronous condition which will result in true or false
const doNothing = false; // simplified
if (doNothing) {
return undefined;
}

return and(
eq(myTable.someField, "someValue"),
eq(myTable.someOtherField, "someOtherValue")
);
}
await db.query.myTable.findFirst({
where: and(
eq(myTable.identifier, params.identifier),
eq(myTable.userId, params.userId),
this.optionalAnd()
),
});

function optionalAnd() {
// some complex synchronous condition which will result in true or false
const doNothing = false; // simplified
if (doNothing) {
return undefined;
}

return and(
eq(myTable.someField, "someValue"),
eq(myTable.someOtherField, "someOtherValue")
);
}
This is a simplified example. I prefered writing it like this instead of a dynamic query because i prefer its readability. But I'm not sure if I might run into issues with this. For now the code seems to work. Any suggestions? Im using turso as my db (sqlite aka libsql to be more precise). Thanks for your help 🙂
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server