Select statement with dynamic fields
Hello I'm writing out this because I don't get how I'm supposed to pass a subset of a given table's fields to a
select()
statement.
I would like to do something like this:
I would like to call that function like this:
If second parameter is ommited then i get all columns (with the default ['*']
or something like that)
I tried several approches, and it seems like i need to use SelectedFields
but I don't understand how to do so. I would assume that the secont type argument is typeof userTable
but cannot find what to put as first one.1 Reply
Alright, I solved half of the issue, i was importing
SelectedFields
from the wrong package (drizzle-orm
, instead of the one from my driver).
Now it looks like this:
I need to figure out how to correctly type the output now because i'm getting the following for the query result array:
For some reason, I understood where the typing was lost, apparently, giving an object to select
instead of declaring it directly in the function call makes typescript behaving differently.
I'm not expert enough in this language to figure out a better solution than casting the output as what it should be but anyways...
Now i can use this like:
And getting both "type-safety" on the result and auto-completion for column names to get