[PRISMA] Adding method with param in result object

Hi, I have seen the documentation about adding method to the result object. I would like to add a method with parameter, NOT static method in result object. Is that possible? https://www.prisma.io/docs/concepts/components/prisma-client/client-extensions/result#add-a-custom-method-to-the-result-object
const user = await prisma.user.findUniqueOrThrow({ where: { id: someId } })
user.generateDiscount(discountPercent)
const user = await prisma.user.findUniqueOrThrow({ where: { id: someId } })
user.generateDiscount(discountPercent)
3 Replies
Finn
Finn2y ago
Does this method interact with the dB at all? I'm hesitant to say this is a good pattern tbh. My brain would just make a generateDiscount util function that takes a user and a discountPercent
Mir
MirOP2y ago
No. It will interact with some of the result value. That's one solution for sure. But it's a big app, so a method would help maintaining and keep track of it.
Finn
Finn2y ago
Idk my fp lovers will disagree. If you want it though what's not working about the approach linked on the docs above? Does it not allow a second argument or somthing? The other option would be to create a method that take a user and mounts the fucntion, so in effect your query result really is a dto like
const hydrateUser = (user: RawUser): User => {
user.someThing = () => ({})
user.otherThing = () => ({})
return user;
}
const hydrateUser = (user: RawUser): User => {
user.someThing = () => ({})
user.otherThing = () => ({})
return user;
}
const user = prsima....
return hydrateUser(user)
const user = prsima....
return hydrateUser(user)
Want results from more Discord servers?
Add your server