InferModel including relations?
Hi,
How do you use InferModel to obtain a type that includes relations?
The results for an executed query has the inferred types but I'm not sure how to infer the type more generally?
Thanks
13 Replies
Bumping this, I'd love to know as well
Hi @danielsharvey @gerty1 Did one of you happen to have found a workaround for this ?
@chronicstone
You can get the returntype of the function where you fetch the data and do the following
@intelligently Good point, thanks
@gerty1 @chronicstone Sorry for the slow response! Similar to @intelligently 's answer:
That's not really a good fitting solution everywhere right? I don't want to write code just so I can define my types correctly... Is there another way? In my case I want to define a function parameter that is the type of a single relation in my table.
The Relations API can output too many types. You can have drizzle give you whatever you want. Depending on the shape you give it, the type will be different. How could you get a type if you don't write it?
Now, what we've done where we need the relational API and we need to export the types, we just create them like so:
InferModel
is a type helper imported from drizzle-orm.👍🏻 I've also used this mechanism in places.
Issue is that this will give you the wrong typehints if you're only selecting certain columns instead of all of them
I guess @Dan Kochetov can help you with a proper type for it
@Andrew Sherman @Dan Kochetov Related question: Is there an official way to get the relation metadata? I'm currently doing the following:
no official way for now, but this should work fine