PrismaP
Prisma2y ago
1 reply
Florian

Prisma.validator with arguments

Is this the correct way to use Prisma validator (to reuse parts of a query) if the query depends on dynamic data?

export function getPostDataInclude(loggedInUserId: string | undefined) {
  return Prisma.validator<Prisma.PostInclude>()({
    user: {
      select: {
        username: true,
        displayName: true,
        avatarUrl: true,
        pinnedPostId: true,
      },
    },
    likes: {
      where: {
        userId: loggedInUserId,
      },
      select: {
        userId: true,
      },
    },
    _count: {
      select: {
        likes: true,
      },
    },
  });
}

export type PostData = Prisma.PostGetPayload<{
  include: ReturnType<typeof getPostDataInclude>;
}>;
Was this page helpful?