Explicitly specifying an object in Prisma with its foreign relationships
I've set up my schema as an approximation of a directory structure: directories (
ADir
s) and documents (ADoc
s). Directories and documents can have a parent directory, which is a foreign key.
I can write queries to retrieve directories and their children (childDirs
, childDocs
) without issue, but if I specify a directory as a state variable in a client component with useState
, the type definition for ADir
that I get from importing @prisma/client
does not have the children as a property.
I've circumvented this issue by extending the type with my own interface, but I feel like it's a messy fix. Any tips? Thanks.
further reference: related libs are just next (using app router) and prisma. No trpc, zod, or superjson packages, but not against them if they are key to a good implementation.5 Replies
I think it's because under the hood Prisma only makes the relations on its own level, so it's a little messy. I had a similar issue, what I did was I extracted the type from the query itself.
Say you have a query
Solution
so then I'd get a type like this
it would get back the typings exactly as the return type of getDir()
it's for a singular item, as getDir returns an array of items
thanks for your help! It's working out well.
No problems, happy to help