W
Wasp-langβ€’12mo ago
Dayne

[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
Dayne
DayneOPβ€’12mo ago
I just went with Float for now and it works. Open to suggestions though if others use something for this purpose.
Vinny (@Wasp)
Vinny (@Wasp)β€’12mo ago
Oh good to know. Never tried myself
Zeko369
Zeko369β€’12mo ago
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
Dayne
DayneOPβ€’12mo ago
oh! Thanks. πŸ™Œ I can do that. I wasn't sure of the best approach.
Zeko369
Zeko369β€’12mo ago
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://...
KYAN1TE
KYAN1TEβ€’11mo ago
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.
Zeko369
Zeko369β€’11mo ago
Yeah, for that usecase Decimal would be way better than float / hacky int conversion
Filip
Filipβ€’11mo ago
@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!
Zeko369
Zeko369β€’11mo ago
@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,...
Filip
Filipβ€’11mo ago
Nice. Thanks!
Zeko369
Zeko369β€’11mo ago
Also this should be doable in Userland πŸ€” , since SuperJSON is a singleton instance that could be used from anywhere
Filip
Filipβ€’11mo ago
Possibly, but I think we stripped class-related stuff out of the types in Wasp
Zeko369
Zeko369β€’11mo ago
GitHub
GitHub - Zeko369/wasp-decimal-example
Contribute to Zeko369/wasp-decimal-example development by creating an account on GitHub.
Zeko369
Zeko369β€’11mo ago
On the superJSON side it's quite easy
Want results from more Discord servers?
Add your server