Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for decimal.js. Support for Prisma.Decimal. #158

Conversation

michaeljonsampson
Copy link

Allows using Prisma Decimal type in getServerSideProps.
This fixes #152

@Skn0tt
Copy link
Member

Skn0tt commented Nov 1, 2021

Hi @michaeljonsampson! Thanks for opening this PR. When you opened the issue, I somehow thought that Decimal was a built-in type like BigInt. It's apparently a proposal in Stage 1: https://github.com/tc39/proposal-decimal

To keep bundle size down, SuperJSON needs to stay as lean as possible. I've adapted your changes into #159, which adds a test for Decimal.js and mentions how to integrate in the README.

@michaeljonsampson
Copy link
Author

@Skn0tt wasn't my issue but I ran into the same problem an came across it. Didn't know about registerCustom. That covers my use case. Tested #159 and I can still serialize Prisma.Decimal. Thanks.

@michaelhays
Copy link

michaelhays commented Jan 18, 2022

Just a heads up for anyone using superjson with tRPC Data Transformers, make sure to put the Decimal.js registerCustom recipe in both your client and router.

I'd recommend making a utils/superjson.ts file and importing superjson from there, to keep them in sync.

@michealroberts
Copy link

Hi @michaelhays ... do you have a minimal reproduction of your superjson transformer with decimal.js support? I'd love to use it one of our projects ...

@michaelhays
Copy link

michaelhays commented Mar 16, 2023

Sure, here are the relevant files for a Next.js project:

src/utils/superjson.ts
import Decimal from 'decimal.js'
import superjson from 'superjson'

superjson.registerCustom<Decimal, string>(
  {
    isApplicable: (v): v is Decimal => Decimal.isDecimal(v),
    serialize: (v) => v.toJSON(),
    deserialize: (v) => new Decimal(v),
  },
  'decimal.js',
)

export default superjson
src/utils/trpc.ts
import type { AppRouter } from '@/server/appRouter'
import { createTRPCNext } from '@trpc/next'
import superjson from './superjson'

export const trpc = createTRPCNext<AppRouter>({
  config() {
    return {
      transformer: superjson,
      links: [...],
    }
  },
})
src/server/trpc.ts
import { initTRPC, TRPCError } from '@trpc/server'
import superjson from '../utils/superjson'
import type { Context } from './context'

const t = initTRPC.context<Context>().create({ transformer: superjson })

export const publicProcedure = t.procedure

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

issue with Next.js Decimal data type (data fetched from prisma/postgres)
4 participants