blitz-js / blitz

⚡️ The Missing Fullstack Toolkit for Next.js
https://Blitzjs.com
MIT License
13.68k stars 798 forks source link

Register Prisma.Decimal with Superjson #2491

Closed dbachrach closed 1 year ago

dbachrach commented 3 years ago

What do you want and why?

I noticed that if you use the type Decimal in your Prisma schema that the Prisma client uses Decimal.js instead of just a raw JS number. If you return a Decimal through a blitz query though the value on the client side is not reserialized back into a Decimal.js class. I can register this class in _app.tsx to get superjson to handle this case:

import SuperJson from "superjson"
import { Prisma } from "db"
SuperJson.registerClass(Prisma.Decimal, { identifier: "DecimalJS" })

Should we do this automatically in Blitz since this is an unexpected gotcha right now? We would probably want to be clever about it though. If you don't ever return a Decimal in a query, then we would be adding the decimal.js library to your client bundle unnecessarily.

flybayer commented 3 years ago

Thanks for opening this!

Probably the best we could do for now is register it automatically if you have Decimal in your schema.

phoenixeliot commented 2 years ago

FWIW I'm also finding that sum(...) $queryRaw queries across Int (@db.TinyInt) columns are also returning Decimals, so I don't think it needs to be in the schema for it to show up.