K
Kysely•11mo ago
decho

Modifying the return type of a query

I have a question, hopefully it's not a silly one. So I have generated types for my DB (via kanel-kysely), one of which is for a materialized view, and as such all columns of that view are generated as nullable. (I think that's just some Postgres weirdness, not related to kysely or kanel). I also have a function that runs a query and selects all from that view, and I am wondering if there is any way to modify that in kysely land so that the returned type has all of these columns as non-nullable. Here is a super basic example: https://kyse.link/?p=s&i=EBBWymS9HA2FBdR6X73o Basically I'm asking if there is a way to change the query so that all nulls from the column types are excluded.
Solution:
Hey 👋 Not a silly one at all. 🙂 You could use .$narrowType to narrow the result type in a type-safe way....
Jump to solution
2 Replies
Solution
Igal
Igal•11mo ago
Hey 👋 Not a silly one at all. 🙂 You could use .$narrowType to narrow the result type in a type-safe way.
decho
decho•11mo ago
oh nice, i will give that a try scanned the docs but couldn't find anything ok yep, that totally worked, thank you very much! going to leave a playground link with 2 solutions in case someone stumbles upon this in the future: https://kyse.link/?p=s&i=GQiHk3ruwxIjsU9RYJfk