Skip to content
Vitaly Tomilov edited this page Oct 7, 2019 · 43 revisions

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.

Receiving Data

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]}
Clone this wiki locally