Eddie Jaoude
Eddie Jaoude
PPrisma
Created by Eddie Jaoude on 10/24/2024 in #help-and-questions
Custom method in a model, gives a "not found" error
I am trying to create a custom method in my model, but when I try to use it, it says not a function. I am confused by the docs that I quoted below as the works/not works are the same code?
In the above example, prisma.user.signUp works, but prisma.user.signUp does not, because the original prisma is not modified.
https://www.prisma.io/docs/orm/prisma-client/client-extensions/model#example Here is my usage in my NextJS project: Prisma client with custom method
new PrismaClient().$extends({
model: {
utm: {
async totalClicks(utmId: string) {
const utmStats = await prisma.utmStats.findMany({
where: { utmId },
});
return utmStats.reduce((a, b) => a + b.clicks, 0) || 0;
},
},
},
});
new PrismaClient().$extends({
model: {
utm: {
async totalClicks(utmId: string) {
const utmStats = await prisma.utmStats.findMany({
where: { utmId },
});
return utmStats.reduce((a, b) => a + b.clicks, 0) || 0;
},
},
},
});
Usage
const utm = await prisma.utm.findUnique({ ... });
const totalClicks = await utm.totalClicks(utm.id); // ERROR: Property 'totalClicks' does not exist
const utm = await prisma.utm.findUnique({ ... });
const totalClicks = await utm.totalClicks(utm.id); // ERROR: Property 'totalClicks' does not exist
I tried "generate" also, but no change. Any help appreciated
2 replies