get full type on prisma client

An alternate method (my preferrence) to get the “full” type, or the model with all of its possible relations, is to use the GetPayload version of your model type. The type is a generic that can receive the same object you pass to your query.

import { PrismaClient, Prisma } from "@prisma/client";

const prisma = new PrismaClient();

type UserWithCars = Prisma.UserGetPayload<{
  include: {
    cars: true;
  }
}>

const usersWithCars = await prisma.user.findMany({
  include: {
    cars: true,
  }
});

As your query grows more complex, you may want to abstract it and ensure that it is strongly typed.

import { Prisma, PrismaClient } from "@prisma/client";

const prisma = new PrismaClient();

const userInclude = Prisma.validator<Prisma.UserInclude>()({
  cars: true,
});

type UserWithCars = Prisma.UserGetPayload<{
  include: typeof userInclude;
}>;

const usersWithCars = await prisma.user.findMany({
  include: userInclude,
});

Leave a Comment