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,
});
Answer from joematune on Stack OverflowAn 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,
});
You can use the Prisma Validator API along with some typescript features to generate types for your query.
For the findMany example you mentioned
import { Prisma } from '@prisma/client'
// 1. Define a User type that includes the "cars" relation.
const userWithCars = Prisma.validator<Prisma.UserArgs>()({
include: { cars: true },
})
// 2: This type will include many users and all their cars
type UserWithCars = Prisma.UserGetPayload<typeof userWithCars>[]
If you simply want to automatically infer the return type of a prisma query wrapped in a function, you can use PromiseReturnType.
For example:
import { Prisma } from '@prisma/client'
async function getUsersWithCars() {
const users = await prisma.user.findMany({ include: { cars: true } });
return users;
}
type UsersWithCars = Prisma.PromiseReturnType<typeof getUsersWithCars>
You can read more about this in the Operating against partial structures of your model types concept guide in the Prisma docs.
TypeScript: way to reference model type by name ? (`Prisma.*Delegate`)
How To Get The Type of a Model?
How do I get the model names in a type form? (Prisma) - Stack Overflow
how to convert prisma schema to typescript types ?
For example if I have this simple schema
model User { id String @id @default(uuid()) email String @unique }
I want to have this user type:
type User ={ id : string;
email: string; }
You get error because modelName could be any string, while it can only receive "user" or "participant" (according to schema.prisma model).
So to get list of your model you can get it with Prisma type
/*
file schema.prisma models:
model User {
id Int @id @default(autoincrement())
name String
password String
job String @default("")
location String @default("")
phone String @default("")
email String
}
model Participant {
id Int @id @default(autoincrement())
userID Int
groupID Int
}
*/
import { Prisma, PrismaClient } from "@prisma/client";
type PrismaOption = PrismaClient<
Prisma.PrismaClientOptions,
never,
Prisma.RejectOnNotFound | Prisma.RejectPerOperation | undefined
>
// this will return "user" | "participant"
export type PrismaModel = keyof Omit<
PrismaOption,
| "$connect"
| "$disconnect"
| "$executeRaw"
| "$executeRawUnsafe"
| "$on"
| "$queryRaw"
| "$queryRawUnsafe"
| "$transaction"
| "$use"
>
function loadModel(modelName: PrismaModel) { // modelName type will be "user" | "participant"
const prisma = new PrismaClient();
return prisma[modelName]
}
loadModel("user")
There is an open feature request for this to be natively supported in Prisma, if you want to add to it: https://github.com/prisma/prisma/issues/5273
