Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(client): Correctly serialize Decimal.js in an input (#13215)
Fix #13213
- Loading branch information
Showing
3 changed files
with
104 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} | ||
`) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters