P
Prisma•6mo ago
fivewen

How to create Prisma Client's generated types with nested models?

Hi guys, I am currently trying to define generate model types using Prisma.validator to include relations. I've followed this documentation: https://www.prisma.io/docs/orm/prisma-client/type-safety/operating-against-partial-structures-of-model-types#problem-using-variations-of-the-generated-model-type However, I am confused how I can set up with model with nested models. For example, I have a Post that has UserSocial Model. In addition, UserSocial has 1 to 1 relationship with User model. I want to generate a PostWithUserSocial but also has the nested User model included. Does anyone know how I can achieve this?
Operating against partial structures of your model types | Prisma D...
This page documents various scenarios for using the generated types from the Prisma namespace
2 Replies
Nurul
Nurul•6mo ago
Hey @fivewen 👋 Assuming your models looks like this:
model Post {
id String @id @default(uuid())
title String
content String?
userSocial UserSocial? @relation(fields: [userSocialId], references: [id])
userSocialId String?
}

model UserSocial {
id String @id @default(uuid())
userId String @unique
user User @relation(fields: [userId], references: [id])
Post Post[]
}

model User {
id String @id @default(uuid())
username String @unique
email String @unique
UserSocial UserSocial?
}
model Post {
id String @id @default(uuid())
title String
content String?
userSocial UserSocial? @relation(fields: [userSocialId], references: [id])
userSocialId String?
}

model UserSocial {
id String @id @default(uuid())
userId String @unique
user User @relation(fields: [userId], references: [id])
Post Post[]
}

model User {
id String @id @default(uuid())
username String @unique
email String @unique
UserSocial UserSocial?
}
You can do something like this to generate the nested type:
import { PrismaClient, Prisma } from "@prisma/client";

const prisma = new PrismaClient({
log: ["query"],
});

async function main() {

const postWithUserSocialAndUser = Prisma.validator<Prisma.PostDefaultArgs>()({
include: {
userSocial: {
include: {
user: true,
},
},
},
});

type PostWithUserSocialAndUser = Prisma.PostGetPayload<
typeof postWithUserSocialAndUser
>;
}

main()
.catch((e) => {
throw e;
})
.finally(async () => {
await prisma.$disconnect();
});
import { PrismaClient, Prisma } from "@prisma/client";

const prisma = new PrismaClient({
log: ["query"],
});

async function main() {

const postWithUserSocialAndUser = Prisma.validator<Prisma.PostDefaultArgs>()({
include: {
userSocial: {
include: {
user: true,
},
},
},
});

type PostWithUserSocialAndUser = Prisma.PostGetPayload<
typeof postWithUserSocialAndUser
>;
}

main()
.catch((e) => {
throw e;
})
.finally(async () => {
await prisma.$disconnect();
});
fivewen
fivewenOP•6mo ago
Worked like a charm. That's intuitive, thanks! One more question, how should I nest when it has a self-relationship? For example, I want to implement a comment type where comment has replies mapped which is a list of comments. So far, this is what I have:
const commentWithRelations = Prisma.validator<Prisma.CommentDefaultArgs>()({
include: {
replies : true,
author: {
include : {
user: true
}
}
},
})
const commentWithRelations = Prisma.validator<Prisma.CommentDefaultArgs>()({
include: {
replies : true,
author: {
include : {
user: true
}
}
},
})
Want results from more Discord servers?
Add your server