PrismaP
Prisma5mo ago
4 replies
Adam

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.

type UserPosts = Prisma.userGetPayload<{
    select: {
        id: true,
        updatedAt: true,
        posts: true,
    },
}>;

type UserPosts = Prisma.userGetPayload<{
    select: {
        id: true,
        updatedAt: true,
        posts: { 
            select: null, // Ideally, this would make all fields optional
        },
    },
}>;

type UserPosts = Prisma.userGetPayload<{
    select: {
        id: true,
        updatedAt: true,
        posts: {
            select: { id: true },
        },
    },
}>;


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 unfortunately
Was this page helpful?