question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Decimal methods not supported

See original GitHub issue

Bug description

According to the docs, Decimal comes from the library Decimal.js. However, the methods from Decimal.js produce an error.

export const FormatPrice = (input: Decimal | null) => {
    const valueString = input === null ? "--" : input.toFixed(2);
    return valueString;
};

This produces an unhandled runtime error: input.toFixed is not a function.

Is prisma’s Decimal a complete implementation of Decimal.js or some sort of subset?

How to reproduce

In a project that has a Decimal data type, create a function to format Decimal values:

export const FormatPrice = (input: Decimal | null) => {
    const valueString = input === null ? "--" : input.toFixed(2);
    return valueString;
};

The function will not compile.

Expected behavior

No response

Prisma information

Environment & setup

  • OS: Ubuntu, Windows
  • Database: MySQL
  • Node.js version: 16.13.0

Prisma Version

prisma                  : 4.0.0
@prisma/client          : 4.0.0
Current platform        : windows
Query Engine (Node-API) : libquery-engine da41d2bb3406da22087b849f0e911199ba4fbf11 (at node_modules\.pnpm\@prisma+engines@3.16.0-49.da41d2bb3406da22087b849f0e911199ba4fbf11\node_modules\@prisma\engines\query_engine-windows.dll.node)
Migration Engine        : migration-engine-cli da41d2bb3406da22087b849f0e911199ba4fbf11 (at node_modules\.pnpm\@prisma+engines@3.16.0-49.da41d2bb3406da22087b849f0e911199ba4fbf11\node_modules\@prisma\engines\migration-engine-windows.exe)
Introspection Engine    : introspection-core da41d2bb3406da22087b849f0e911199ba4fbf11 (at node_modules\.pnpm\@prisma+engines@3.16.0-49.da41d2bb3406da22087b849f0e911199ba4fbf11\node_modules\@prisma\engines\introspection-engine-windows.exe)
Format Binary           : prisma-fmt da41d2bb3406da22087b849f0e911199ba4fbf11 (at node_modules\.pnpm\@prisma+engines@3.16.0-49.da41d2bb3406da22087b849f0e911199ba4fbf11\node_modules\@prisma\engines\prisma-fmt-windows.exe)
Default Engines Hash    : da41d2bb3406da22087b849f0e911199ba4fbf11
Studio                  : 0.465.0
Preview Features        : referentialIntegrity

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
SevInfcommented, Aug 1, 2022

@ChristianIvicevic @grahampcharles I don’t think this problem has anything to do with prisma. JSON.parse(JSON.stringify()) loop is where decimal instance gets lost. Decimal is not a thing in JSON, when you do stringify it is converted to string. JSON.parse then does not know anything about Decimal as well and parses value as regular string. You’ll have to manually convert decimal values to decimal instances after JSON.parse, there is no way around it.

Since it does not seem to be Prisma issue, I’ll close it for now. Feel free to reopen if you manage to reproduce it without JSON.parse(JSON.stringify()) part.

0reactions
grahampcharlescommented, Aug 4, 2022

Hm. Well first, there’s this: Today I learned that in Next.js, prisma objects must cross the getSSP -> component divide. (A similar thing would happen in vanilla React, I think?) It’s not readily clear that that step is going to involve serialization. So first off, I’d say that needs to be documented.

Second, it would seem that nearly everyone who uses prisma with React is going to run into a JSON serialization problem unless they’re only using primitive data types – and since prisma makes use of Decimal instead of number, that includes anyone using any kind of floating-point number. What does everyone else do in this situation? I ran into the problem on a date field and found that The Internet said to just put a parse(stringify()) around the object, but clearly that’s a bad idea.

Anyway, this is clearly a quirk of interaction between prisma (among other packages!) and React. Given that, yes, I think a helper function to fix the problem (an adapted superjson?) would be lovely. At least, some documentation of the issue would be a really good start.

Read more comments on GitHub >

github_iconTop Results From Across the Web

"conversion from method to Decimal is not supported ...
So, I'm trying to calculate the coffee price of the parameters mentioned below. But, every time I try to call the coffeeprice method,...
Read more >
Decimal fixed point and floating point arithmetic
The decimal module was designed to support “without prejudice, both exact unrounded decimal arithmetic (sometimes called fixed-point arithmetic) and rounded ...
Read more >
Decimal Support for MongoDB · Issue #12637 · prisma ...
The problem is not in Prisma Client itself, but in the MongoDB Rust driver we are using under the hood - it can...
Read more >
Floating-point numeric types - C# reference
Learn about the built-in C# floating-point types: float, double, and decimal.
Read more >
decimal.js API - GitHub Pages
A new Decimal constructor is created by calling the clone method of an already ... The number of digits of value is not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found