Is there a way to import a type from a Prisma Model on Client

I know that we have inferRouterInputs and inferRouterOutputs, but I dont feel that is what I need. TRPC provides a way to generate a type from model? or the right only way is literally the output?
13 Replies
whatplan
whatplan14mo ago
Can you rephrase your question? I dont understand what your actually asking a prisma model?
kevinka
kevinka14mo ago
oh jeez I forgot about mention Prisma - sorry. but yes!
whatplan
whatplan14mo ago
import { type nameOfModel } from @prisma/client
import { type nameOfModel } from @prisma/client
kevinka
kevinka14mo ago
that's literally it! thank you brodher
whatplan
whatplan14mo ago
t3ggLove
kevinka
kevinka14mo ago
enjoying the contact fella, do you know well prisma? I want to create a query that connects to an another model from a many to many and I'm struggling
kevinka
kevinka14mo ago
this is my models and I want to query an item and include the category it has by searching on the many to many ItemCategory
whatplan
whatplan14mo ago
await prisma.item.findUnqiue({
where: {
id: id,
},
include: {
categories: {
include: {
category: true,
},
},
},
})
await prisma.item.findUnqiue({
where: {
id: id,
},
include: {
categories: {
include: {
category: true,
},
},
},
})
would return
{
id: string,
name: string,
//...
categories: (ItemCategory & Category)[]
}
{
id: string,
name: string,
//...
categories: (ItemCategory & Category)[]
}
whatplan
whatplan14mo ago
Prisma
Relation queries (Concepts)
Prisma Client provides convenient queries for working with relations, such as a fluent API, nested writes (transactions), nested reads and relation filters.
kevinka
kevinka14mo ago
ohh boy, I need to start to explain better my questions lol thank you! it's almost that Ive found this article from Prisma Docs before but what I really want is almost that; by doing that I will get all the register I have from the model ItemCategory that's my control table. what I really need is a list of the category itself! The final result I would like to have would be something { id: string, name: string, //... categories: { id: string, name: string, // values from the Category model } } and btw including this category on the Item query it have stopped to retrieve the rest of the item data, it seems they all are set to false. thinkies
kevinka
kevinka14mo ago
I can workaround my need with that but I still think there's a way to make it in one query
whatplan
whatplan14mo ago
I still dont know exactly what you want to be honest I think you want the item + all the categories for that item Im pretty sure the first thing I wrote (what im replying to) works as well but this is basically what you just wrote out
const item = await ctx.prisma.item.findFirst({
where: {
id: input.id,
},
})
const categories = await prisma.itemcatergory.findMany({
where: {
categoryId: input.id,
},
include: {
category: true,
},
})

return {
...item,
categories: categories.map(x => x.category)
}
const item = await ctx.prisma.item.findFirst({
where: {
id: input.id,
},
})
const categories = await prisma.itemcatergory.findMany({
where: {
categoryId: input.id,
},
include: {
category: true,
},
})

return {
...item,
categories: categories.map(x => x.category)
}
kevinka
kevinka14mo ago
its because the first query (the one you wrote first) doesnt provide the category itself and the ids of them