Skip to content

Commit 2a199a0

Browse files
committed
fix(lib): parse int8 as string
Because JS only has float64 numbers
1 parent 61a59ce commit 2a199a0

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/lib/db.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@ import { types, Pool, PoolConfig } from 'pg'
22
import { parse as parseArray } from 'postgres-array'
33
import { PostgresMetaResult } from './types'
44

5-
types.setTypeParser(types.builtins.INT8, parseInt)
5+
types.setTypeParser(types.builtins.INT8, (x) => {
6+
const asNumber = Number(x)
7+
if (Number.isSafeInteger(asNumber)) {
8+
return asNumber
9+
} else {
10+
return x
11+
}
12+
})
613
types.setTypeParser(types.builtins.DATE, (x) => x)
714
types.setTypeParser(types.builtins.TIMESTAMP, (x) => x)
815
types.setTypeParser(types.builtins.TIMESTAMPTZ, (x) => x)

test/lib/query.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,3 +491,22 @@ test('formatter', async () => {
491491
}
492492
`)
493493
})
494+
495+
test('very big number', async () => {
496+
const res = await pgMeta.query(
497+
`SELECT ${Number.MAX_SAFE_INTEGER + 1}::int8, ARRAY[${Number.MIN_SAFE_INTEGER - 1}::int8]`
498+
)
499+
expect(res).toMatchInlineSnapshot(`
500+
Object {
501+
"data": Array [
502+
Object {
503+
"array": Array [
504+
"-9007199254740992",
505+
],
506+
"int8": "9007199254740992",
507+
},
508+
],
509+
"error": null,
510+
}
511+
`)
512+
})

0 commit comments

Comments
 (0)