Next.js Discord

Discord Forum

Serialising Decimal from Prisma

Unanswered
Masai Lion posted this in #help-forum
Open in Discord
Masai LionOP
Decimal is the currently recommended type by Prisma for storing price, amount, currency etc. Currently this is proving to be a giant pain when it comes to server-side data fetching. Current methods for handling usually seem to be around either
1. Using array.map() to map a findMany and replace values with Decimal.toString()
2. Using SuperJSON
3. Standard API call

The problem with 1 is that it's a giant PITA when you're dealing with relational data - any nesting leads to nested maps. which is difficult to understand, maintain, debug etc.

The problem with 2 and 3 is that when passing props across the boundary you lose type saftey. You would have to pass the prop as a string or with an API call you're getting a string.

The only other apporach currently i've found is:
1. Using serverActions and superJson to serialise and deserialise. But still has issues with type Safety
2. Moving to Int*100 to store a price in cents. which seems to be leading.

Does anyone else have other suggestions / can see i'm missing something?

0 Replies