GetPayload: Make all properties optional on related model
I'm struggling to find a clean way to use the the
GetPayload
model helper functions with nested relationships without some level of properties being required. We use helper functions to compute dynamic data points (status', counts, etc).
For a specific helper function, we attempt to compute a status based on the number of entries in a related 1:M relationship. We do not care which fields are included in the array of related objects, just that the array itself exists. I've included several examples below using a standard users->posts relationship to demonstrate the issue. In each case below, all fields are required.
Can someone share insight on how they've properly typed in these situations while using the Prisma Typescript helper functions? I'd rather not create the type by hand for multiple reasons but that seems to be the only way unfortunately3 Replies
Howdy, friend! I'm the Prisma AI Help Bot — fast answers from me, or slow, hand-crafted wisdom from a dev? Choose wisely, adventurer.
Just to confirm, did you had a look at this guide?
https://www.prisma.io/docs/orm/prisma-client/type-safety/operating-against-partial-structures-of-model-types
Do I understand correctly that for your helper you only care that posts is an array, not which fields each post has. But the types Prisma generates make every selected field on posts required. You would like GetPayload to make the properties of the related model optional (or unspecified), ideally with something like select: null?
Yes, I've looked through this guide and attempted a few variations. as for the first soluition, I could absolutely hand roll a solution that manipulates the types from the generated client but it's not a clean solution and adds dev friction.
Do I understand correctly that for your helper you only care that posts is an array, not which fields each post has. But the types Prisma generates make every selected field on posts required. You would like GetPayload to make the properties of the related model optional (or unspecified), ideally with something like select: null?Yes, exactly! This would help streamline those edge cases regardless of how the data was retrieved from the DB