Skip to content

Commit 4958e80

Browse files
committedApr 7, 2020
Treat postgres bigint as string by default
1 parent 2e01c2b commit 4958e80

File tree

4 files changed

+23
-22
lines changed

4 files changed

+23
-22
lines changed
 

‎README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -493,11 +493,19 @@ prexit(async () => {
493493

494494
`Number` in javascript is only able to represent 2<sup>53</sup>-1 safely which means that types in PostgreSQLs like `bigint` and `numeric` won't fit into `Number`.
495495

496-
Since Node.js v10.4 we can use [`BigInt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt) to match the PostgreSQL type `bigint`, so Postgres.js will use BigInt if running on v10.4 or later. For older versions `bigint` will be returned as a string.
496+
Since Node.js v10.4 we can use [`BigInt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt) to match the PostgreSQL type `bigint` which is returned for eg. `count(*)`. Unfortunately it doesn't work with `JSON.stringify` out of the box, so Postgres.js will return it as a string.
497497

498-
There is currently no way to handle `numeric / decimal` in a native way in Javascript, so these and similar will be returned as `string`.
498+
If you want to use `BigInt` you can add this custom type:
499499

500-
You can of course handle types like these using [custom types](#types) if you want to.
500+
```js
501+
const sql = postgres({
502+
types: {
503+
bigint: postgres.BigInt
504+
}
505+
})
506+
```
507+
508+
There is currently no way to handle `numeric / decimal` in a native way in Javascript, so these and similar will be returned as `string`. You can also handle types like these using [custom types](#types) if you want to.
501509

502510
## The Connection Pool
503511

‎lib/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,13 @@ function notTagged() {
3131
Object.assign(Postgres, {
3232
toPascal,
3333
toCamel,
34-
toKebab
34+
toKebab,
35+
BigInt: {
36+
to: 20,
37+
from: [20],
38+
parse: x => BigInt(x), // eslint-disable-line
39+
serialize: x => x.toString()
40+
}
3541
})
3642

3743
const originCache = new Map()

‎lib/types.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const char = (acc, [k, v]) => (acc[k.charCodeAt(0)] = v, acc)
22
const entries = o => Object.keys(o).map(x => [x, o[x]])
33

44
// These were the fastest ways to do it in Node.js v12.11.1 (add tests to revise if this changes)
5-
const types = module.exports.types = Object.assign({
5+
const types = module.exports.types = {
66
string: {
77
to: 25,
88
from: null, // defaults to string
@@ -38,16 +38,7 @@ const types = module.exports.types = Object.assign({
3838
serialize: x => '\\x' + x.toString('hex'),
3939
parse: x => Buffer.from(x.slice(2), 'hex')
4040
}
41-
},
42-
typeof BigInt === 'undefined' ? {} : {
43-
bigint: {
44-
to: 20,
45-
from: [20],
46-
parse: data => BigInt(data), // eslint-disable-line
47-
serialize: bigint => bigint.toString()
48-
}
49-
}
50-
)
41+
}
5142

5243
const defaultHandlers = typeHandlers(types)
5344

‎tests/index.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ t('Transaction succeeds on caught savepoint', async() => {
198198
await sql`insert into test values(3)`
199199
})
200200

201-
return [typeof BigInt === 'undefined' ? '2' : 2n, (await sql`select count(1) from test`)[0].count, await sql`drop table test`]
201+
return ['2', (await sql`select count(1) from test`)[0].count, await sql`drop table test`]
202202
})
203203

204204
t('Savepoint returns Result', async() => {
@@ -875,8 +875,8 @@ t('Debug works', async() => {
875875
return ['select 1', result]
876876
})
877877

878-
t('bigint is returned as BigInt', async() => [
879-
'bigint',
878+
t('bigint is returned as String', async() => [
879+
'string',
880880
typeof (await sql`select 9223372036854777 as x`)[0].x
881881
])
882882

@@ -890,10 +890,6 @@ t('numeric is returned as string', async() => [
890890
typeof (await sql`select 1.2 as x`)[0].x
891891
])
892892

893-
function get() {
894-
return sql`selec 1`
895-
}
896-
897893
t('Async stack trace', async() => {
898894
return [
899895
parseInt(new Error().stack.split('\n')[1].split(':')[1]) + 1,

0 commit comments

Comments
 (0)