How to do conditional joins with the query builder?
Hi, I'm looking for a way to do conditional join's without working with AnyPgSelect. Thanks in advance.
15 Replies
Are you trying to use the new relational query or the old syntax?
I think the new one.
db.select().from(table).innerJoin
Here is a where clause with conditional params:
All equels with a undefined value are ignored
I think you missed the point. I want to do conditional joins.
I didn’t try it, but you can pass an
and
function instead of eq
where the first condition is the ‘magical’ sql
utility and do something like this:
I mean conditional joins like leftJoin or innerJoin not condition statements part of a single join.
I’m not sure I’m following.. Although my example does have a join, ultimately if
condition
is false the join will have no effect.
Can you share a made-up example of what will be your ideal solution? Or better yet, a raw sql example.I want to do this without specifying all types on my own. This doesn't work because leftJoin and inerJoin return different types.
cc @Dan Kochetov
good question
in this particular case, you can workaround like this:
Hi @Dan Kochetov, tsc does not complain but
innerJoin
literal is not type-safe. Therefore according to my example above, I'd like to work with one concrete join and reassign the result back to query
.why do you think it's not type-safe?
I tested it. I don't have auto completion for the different join types.
yes, you won't have the autocompletion for the method name, but that doesn't mean it's not type safe.
if you enter an incorrect method name, it won't work.
You're right, thx