P
Prisma•2mo ago
TJ

How to derive a type from types generated by Prisma?

Hi, The following TypeScript code snippet is working where I'm able to derive a type X from the query result from Prisma.
const rows = await prisma.role_users.findMany({
select: {
users: {
select: {
id: true,
email: true,
},
},
roles: {
select: {
members: {
select: {
groups: {
select: {
id: true,
name: true,
},
},
},
},
},
},
},
});

type X = (typeof rows)[number]["users"] & {
roles: (typeof rows)[number]["roles"]["members"][number]["groups"][];
};
const rows = await prisma.role_users.findMany({
select: {
users: {
select: {
id: true,
email: true,
},
},
roles: {
select: {
members: {
select: {
groups: {
select: {
id: true,
name: true,
},
},
},
},
},
},
},
});

type X = (typeof rows)[number]["users"] & {
roles: (typeof rows)[number]["roles"]["members"][number]["groups"][];
};
May I know how to create the type outside of the function instead of defining the type X on runtime in the function? Something like deriving the type from the database table types generated by Prisma and etc?
1 Reply
Nurul
Nurul•2mo ago
Hello @TJ 👋 Have you seen this section of our docs? https://www.prisma.io/docs/orm/prisma-client/type-safety/operating-against-partial-structures-of-model-types I believe this should be helpful
Operating against partial structures of your model types | Prisma D...
This page documents various scenarios for using the generated types from the Prisma namespace
Want results from more Discord servers?
Add your server