How to infer an aggregated column with `filterWhere` clause as nullable?
Greetings.
I am joining from another table using
leftJoin
, then grouping rows by id, and finally using jsonAgg(jsonBuildObject(...))
with a filterWhere
method chained on top of this expression as seen in the demo.
The problem is that the inferred type by kysely is incorrect, because it thinks the json aggregate function will always be an array, however, if there are no matching rows from the other table it will be NULL because of the filter where clause.
I know I can use the .$castTo<{...}>()
helper and manually set the type, but that can become problematic, especially when dealing with larger objects.
Is there any workaround for this problem? I thought filterWhere
would be smart enough to make the column nullable, or maybe I'm not smart enough myself and I'm missing something 🙂
https://kyse.link/COF71Solution:Jump to solution
Hey 👋
This is a bug.
I'm not sure there's an easy/performant way of solving this....
2 Replies
Solution
Hey 👋
This is a bug.
I'm not sure there's an easy/performant way of solving this.
We don't have an easy workaround. Should just
$castTo
.
maybe extract to a helper function that re-use it's return type inside $castTo
somehow to DRY it up.All right, thank you @Igal I will figure something out, most likely with
$castTo
or maybe even restructure my query. I was also thinking, if $nonNull
is a helper, shouldn't there be a $nullable
helper as well, not sure if that's a good idea or not haha 🙂
But anyway, cheers!