P
Prisma•3d ago
JuanErnesto

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.
2 Replies
JuanErnesto
JuanErnestoOP•3d ago
The AI helped:
It appears that creating a computed field using a related field is currently not directly supported in Prisma's result extensions. However, I can provide some information on the current state and limitations of computed fields in Prisma.
As of now, Prisma's result extensions allow you to add computed fields to query results, but these are limited to scalar fields of the model itself. The documentation Result: Add custom fields and methods to query results states:
"You can only create computed fields that are based on scalar fields."
This means that directly using related fields in a computed field is not possible with the current implementation.
There is an open issue on GitHub discussing this limitation: Support relations in result extensions. The issue proposes allowing relation fields in the needs block of result extensions, but this feature is not yet implemented.
For now, you might need to consider alternative approaches, such as:
Fetching the related data separately and computing the field in your application code.
Using a database view if your database supports it.
Exploring other Prisma Client extensions or middleware that might allow for more complex computations.
It's worth noting that this limitation is recognized by the Prisma team, and there are ongoing discussions about how to support computed fields based on relations in the future. You may want to follow the GitHub issue mentioned above for updates on this feature.
I'm sorry I couldn't provide a direct solution to your need. The current limitations of Prisma's computed fields don't allow for using related fields in the computation.
It appears that creating a computed field using a related field is currently not directly supported in Prisma's result extensions. However, I can provide some information on the current state and limitations of computed fields in Prisma.
As of now, Prisma's result extensions allow you to add computed fields to query results, but these are limited to scalar fields of the model itself. The documentation Result: Add custom fields and methods to query results states:
"You can only create computed fields that are based on scalar fields."
This means that directly using related fields in a computed field is not possible with the current implementation.
There is an open issue on GitHub discussing this limitation: Support relations in result extensions. The issue proposes allowing relation fields in the needs block of result extensions, but this feature is not yet implemented.
For now, you might need to consider alternative approaches, such as:
Fetching the related data separately and computing the field in your application code.
Using a database view if your database supports it.
Exploring other Prisma Client extensions or middleware that might allow for more complex computations.
It's worth noting that this limitation is recognized by the Prisma team, and there are ongoing discussions about how to support computed fields based on relations in the future. You may want to follow the GitHub issue mentioned above for updates on this feature.
I'm sorry I couldn't provide a direct solution to your need. The current limitations of Prisma's computed fields don't allow for using related fields in the computation.
Nurul
Nurul•2d ago
Hey @JuanErnesto 👋 I am glad to hear that the AI response was helpful. Here's the feature request: https://github.com/prisma/prisma/issues/20091 And this workaround (which is not completely typesafe) https://github.com/prisma/prisma/issues/20091#issuecomment-1824063594
GitHub
Support relations in result extensions · Issue #20091 · prisma/pr...
Problem At the moment, only scalar fields are allowed to be specified in needs block of result extension. This is by design — since we include computed fields into the default selection, we can&#39...
Want results from more Discord servers?
Add your server