diff --git a/packages/client/src/__tests__/isObject.test.ts b/packages/client/src/__tests__/isObject.test.ts new file mode 100644 index 000000000000..965a371d8790 --- /dev/null +++ b/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) +}) diff --git a/packages/client/src/__tests__/query/decimal.test.ts b/packages/client/src/__tests__/query/decimal.test.ts new file mode 100644 index 000000000000..890cd7b62242 --- /dev/null +++ b/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 + } + } + `) +}) diff --git a/packages/client/src/runtime/utils/isObject.ts b/packages/client/src/runtime/utils/isObject.ts index 0de236ce812f..d283efa3112c 100644 --- a/packages/client/src/runtime/utils/isObject.ts +++ b/packages/client/src/runtime/utils/isObject.ts @@ -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)] }