[SOLVED] Using Decimal type with Prisma
I am trying to store dollar amounts and it seems
Decimal
is the type to go with. However, when I use it wasp it crashing on Type 'Decimal' is not assignable to type 'SuperJSONValue'.
I am not sure if this is a wasp or maybe the superJson package. Any suggestions or a different type to use for this? π€14 Replies
I just went with
Float
for now and it works. Open to suggestions though if others use something for this purpose.Oh good to know. Never tried myself
Not really the exact answer you want to hear, but you should never be storing currency amounts in decimals/floats, since you could end up with rounding errors and money rounding errors -> BAD
You should be just storing it in cents (so 1000, for 10,00), you'll still end up with rounding errors if you're doing converions but you'd have that problem either way
oh! Thanks. π I can do that. I wasn't sure of the best approach.
If you have 7min, take a look why https://www.youtube.com/watch?v=fhwEFZ34c7g
PlanetScale
YouTube
Storing money in MySQL (the right way)
π Learn more about PlanetScale at https://planetscale.com/youtube.
ββββββββββββββββββ
01:15 Creating the money table
02:27 The problem with floating points
03:12 Remaking the table with decimals
04:14 Storing money as integers
05:12 Remaking the table with integers
ββββββββββββββββββ
π¬ Follow PlanetScale on social media
β’ Twitter: https://...
Just ran in to this issue, but with the intention of using Decimal to store latitude & longitude values.
Not a big deal for my use case, however where precision is required, decimal would be preferred. Just noting this here as it seems the above has circumvented the lack of Decimal types with the whole money use case.
Yeah, for that usecase Decimal would be way better than float / hacky int conversion
@Dayne This problem comes down to Wasp's Superjson serialization and JavaScript's built-in types.
JavaScript doesn't have a built-in highly precise decimal type, so you need to somehow convert Prisma's Decimal to a value you can send over the network and deserialize on the frontend.
Normally, this would only include valid JSON values, but Wasp uses Superjson which gives you a couple of extra types you can serialize (e.g., sets and dates).
Still, it doesn't know (yet) how to serialize and deserialize a Decimal object.
I'll definitely create an issue and look into it. Thanks for reporting!
@Filip just a note on that -> https://github.com/blitz-js/superjson#recipes
GitHub
GitHub - blitz-js/superjson: Safely serialize JavaScript expression...
Safely serialize JavaScript expressions to a superset of JSON, which includes Dates, BigInts, and more. - GitHub - blitz-js/superjson: Safely serialize JavaScript expressions to a superset of JSON,...
Nice. Thanks!
Also this should be doable in Userland π€ , since SuperJSON is a singleton instance that could be used from anywhere
Possibly, but I think we stripped class-related stuff out of the types in Wasp
GitHub
GitHub - Zeko369/wasp-decimal-example
Contribute to Zeko369/wasp-decimal-example development by creating an account on GitHub.
On the superJSON side it's quite easy