node-postgres icon indicating copy to clipboard operation
node-postgres copied to clipboard

How to detect when at attempt is made to serialize BigInt to JSON?

Open gajus opened this issue 2 years ago • 0 comments

For context, I want to throw an error when an attempt is made to serialize BigInt to a JSON.

https://github.com/gajus/slonik/issues/515

The reason being that handling values in such a way may produce unexpected results:

test.only('serializes BigInt to JSON', async (t) => {
  const pool = await createPool(t.context.dsn, {
    PgPool,
  });

  const result = await pool.oneFirst(sql.unsafe`
    SELECT json_object_agg('foo', ${BigInt(
      1_000_000_000_000_000_001n,
    )}::bigint)
  `);

  t.deepEqual(result, {
    foo: 1_000_000_000_000_000_000,
  });

  await pool.end();
});

I would therefore rather require that the end user cast the value to ::text or whatever else.

I am trying to figure out which part of the node-postgres API I could use to tap into this happening?

gajus avatar Oct 06 '23 14:10 gajus