JuanErnesto
JuanErnesto
PPrisma
Created by JuanErnesto on 12/19/2024 in #help-and-questions
Help creating computed fields
Hi! im trying to do an extension of PrismaClient so I can add a computed field. each pageItem has a relation of many-to-many with products, and each product have a type. I wish to generate a computed field named "isMerch" that its true when all the products it contain are of type "MERCH". Here is my approach, far from being usefull...
export const prismaClient = new PrismaClient()
.$extends({
result: {
pageItem: {
isMerch: {
needs: { productRelations: true },
compute(pageItem){
return pageItem.products.reduce((prev, curr) => prev || curr.type=='MERCH', true);
}
},
}
}
});
export const prismaClient = new PrismaClient()
.$extends({
result: {
pageItem: {
isMerch: {
needs: { productRelations: true },
compute(pageItem){
return pageItem.products.reduce((prev, curr) => prev || curr.type=='MERCH', true);
}
},
}
}
});
The first issue I encounter is on the needs field, how can I use relations? PD: I restarted to work with prisma after a whole year, i'm a little rusty in a lot of aspects.
4 replies