Skip to content

Commit

Permalink
fix(client): Correctly serialize Decimal.js in an input (#13215)
Browse files Browse the repository at this point in the history
Fix #13213
  • Loading branch information
SevInf committed May 6, 2022
1 parent 7cfb0e1 commit c3a1de5
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 4 deletions.
32 changes: 32 additions & 0 deletions packages/client/src/__tests__/isObject.test.ts
@@ -0,0 +1,32 @@
import Decimal from 'decimal.js'

import { isObject } from '../runtime/utils/isObject'

test('is true for proper object', () => {
expect(isObject({})).toBe(true)
})

test('is false for null', () => {
expect(isObject(null)).toBe(false)
})

test('is false for primitive types', () => {
expect(isObject(1)).toBe(false)
expect(isObject(undefined)).toBe(false)
expect(isObject('hello')).toBe(false)
expect(isObject(true)).toBe(false)
expect(isObject(BigInt('10'))).toBe(false)
expect(isObject(Symbol())).toBe(false)
})

test('is false for Dates', () => {
expect(isObject(new Date('2022-05-06T00:00:00Z'))).toBe(false)
})

test('is false for Decimal.js instance', () => {
expect(isObject(new Decimal('12.34'))).toBe(false)
})

test('is false for Buffer', () => {
expect(isObject(Buffer.from('hello', 'utf8'))).toBe(false)
})
66 changes: 66 additions & 0 deletions packages/client/src/__tests__/query/decimal.test.ts
@@ -0,0 +1,66 @@
import { Decimal } from 'decimal.js'

import { getDMMF } from '../../generation/getDMMF'
import { DMMFClass, makeDocument } from '../../runtime'

const datamodel = /* Prisma */ `
datasource my_db {
provider = "postgres"
url = env("POSTGRES_URL")
}
model User {
id Int @id
money Decimal
}
`

let dmmf
beforeAll(async () => {
dmmf = new DMMFClass(await getDMMF({ datamodel }))
})

test('allows to pass it decimal instance', () => {
const document = makeDocument({
dmmf,
rootTypeName: 'query',
rootField: 'findManyUser',
select: { where: { money: new Decimal('123456789.12334') } },
})

expect(document.toString()).toMatchInlineSnapshot(`
query {
findManyUser(where: {
money: 123456789.12334
}) {
id
money
}
}
`)
})

test('allows to pass it decimal array', () => {
const document = makeDocument({
dmmf,
rootTypeName: 'query',
rootField: 'findManyUser',
select: { where: { money: { in: [new Decimal('12.34'), new Decimal('56.78')] } } },
})

expect(document.toString()).toMatchInlineSnapshot(`
query {
findManyUser(where: {
money: {
in: [
12.34,
56.78
]
}
}) {
id
money
}
}
`)
})
10 changes: 6 additions & 4 deletions packages/client/src/runtime/utils/isObject.ts
Expand Up @@ -5,11 +5,13 @@
*/
const notReallyObjects = {
'[object Date]': true,
'[object BitInt]': true,
'[object Uint8Array]': true, // for Buffers
'[object Function]': true, // for Decimal
'[object Decimal]': true, // for Decimal
}

export function isObject(value: any): boolean {
return value && typeof value === 'object' && !notReallyObjects[Object.prototype.toString.call(value)]
export function isObject(value: unknown): boolean {
if (!value) {
return false
}
return typeof value === 'object' && !notReallyObjects[Object.prototype.toString.call(value)]
}

0 comments on commit c3a1de5

Please sign in to comment.