-
Notifications
You must be signed in to change notification settings - Fork 217
BigInt
Node.js
v10.4 made support for type BigInt official, and pg-promise
v9.3 started supporting it.
This means that explicit type conversion from strings into something else by the client is no longer necessary (see this popular question) in either directions.
In order to make the driver feed PostreSQL 64-bit types (such as BIGINT
and BIGSERIAL
) as BigInt, rather than strings, you need to set the corresponding type parser:
pgp.pg.types.setTypeParser(20, BigInt); // Type Id 20 = BIGINT | BIGSERIAL
test:
await db.one('SELECT 123::bigint as value'); //=> {value: 123n}
And if you are planning to execute queries that return an array of BigInt-s, you will need a separate type parser for that:
// 1016 = Type Id for arrays of BigInt values
const parseBigIntArray = pgp.pg.types.getTypeParser(1016);
pgp.pg.types.setTypeParser(1016, a => parseBigIntArray(a).map(BigInt));
test:
await db.one('SELECT ARRAY[1, 2]::bigint[] as value'); //=> {value: [1n, 2n]}
Query-formatting engine in pg-promise
has been upgraded to support BigInt everywhere.
It means you can now supply BigInt as a formatting value for any query, and it is supported by all Formatting Filters.
example:
pgp.as.format('${this:csv}', {
small: 123_456_789_012_345_678,
large: 123_456_789_012_345_678n
});
//=> 123456789012345680,123456789012345678
The same numerical above is rounded to 53 bits for number
and to 64 bits for BigInt.
pg-promise