Working with decimal in NextJS
Has anyone found a reasonable way to work with Decimal in NextJS?
"Only plain objects can be passed to Client Components from Server Components. Decimal objects are not supported."
Every time we add a Decimal to our schema, we end up breaking a bunch of server components. The only option seems to be manually serializing and deserializing everything, which isn't great.
Is there a way to tell NextJS or Prisma to serialize our Decimals as strings when sending to and from the client? Or are there other recommendations on how to work with decimals?
4 Replies
Hi @martink
This is an issue that has been encountered by most users. One user suggested this workaround in this issue .
GitHub
Option to disable Decimal.js Generation? · Issue #6049 · prisma/pri...
Problem I have few columns in my db that have Decimal values, my GraphQL Server usually returns whatever responses that Prisma returns (using Float in GraphQL for Decimals). Recently I upgraded to ...
Ah thanks - I tried that (and a custom .$extends) and it seems to work, but I still get errors:
Only plain objects can be passed to Client Components from Server Components. Decimal objects are not supported.
or Only plain objects can be passed to Client Components from Server Components. Objects with symbol properties like nodejs.util.inspect.custom are not supported.
respectivelyah found more: https://github.com/prisma/prisma/issues/20627
GitHub
Computed fields (via Client Extensions) are producing non-POJOs · I...
Problem If I'm using computed fields in SvelteKit - objects can't be serialized by devalue() because they are not passing POJO check. E.g. const prismax = prisma.$extends({ result: { user: ...
It's looking to me like the solution is really just not to use Decimal and use a fixed int type instead? e.g. "cents" or whatever precision I need?