Hey. Let's assume I have a sales model like so - ...
# orm-help
k
Hey. Let's assume I have a sales model like so -
model Sale {
id        String            @id @default(uuid())
createdAt DateTime          @default(now())
updatedAt DateTime          @updatedAt
name      String
date      String
time      String
revenue   Int
amount    Int
invoice   String
}
After I fetch using
prisma.sale.findMany({})
I want to calculate the commission against some percentage and add an addition commission field. How would I add the type of the additional field to the client so that I can consume it there? Would it be possible to do something like -
type Sale = Prisma.SaleGetPayload<{
include: {
commission: float
}
}>
The reason why the commission field isn't in the sales model in the first place is because the commission percentage is not persistant and might change depending on other factors. So it needs to be calculated before it gets passed further. I hope that makes sense. Any help is appreciated
1
j
Could give me full data model of relationship? it will help me to figure out and try to find solution 😄
k
There's no relationship. Only the sales model 😅
The
sum
calculates based on existing data. What I'm trying to do is to first fetch the data on the server. (I'm using Next.js API) then calculate the commission based on the revenue like so for example.
commission = revenue * 0.05
then I would add this commission as a field of the data but I'm afraid Prisma might not know the type as it's originally generated like so -
model Sale {
id        String            @id @default(uuid())
createdAt DateTime          @default(now())
updatedAt DateTime          @updatedAt
name      String
date      String
time      String
revenue   Int
amount    Int
invoice   String
}
But I want to pass it to the client like so -
model Sale {
id        String            @id @default(uuid())
createdAt DateTime          @default(now())
updatedAt DateTime          @updatedAt
name      String
date      String
time      String
revenue   Int
amount    Int
invoice   String
commission Float
}
j
so I think you should create a function to calculate it as you said there is not relation
But IMO you should have relation at some points it will help you alot
k
Hmm, how would a relation be of help in this case? I'm not storing the commission. I'm only calculating it after i have fetched the sales.
But I get your point. Thank you for your help
j
you can use
_sum
directly.
IMO you your solution is very simple
but if you need Prisma support this please create feature request on github issue 😄
k
Okay 😀