Conversation

ales-albert-kilbergr

The pg library fails to serialize query values in case when an object for jsonb column contains bigint.

The failed error message is as follows because JSON.stringify does not know how to deal with bigint.

TypeError: Do not know how to serialize a BigInt

Reproducible example could look like this:

const queryConfig = `INSERT INTO my_table (id, data) VALUES ($1, $2)`;
const values = ['someId', { someValue: BigInt(1) }]
await pgClient.query(queryConfig, values);

Currently a pg library fails when processing objects for jsonb columns which contains bigints because JSON.stringify by default does not know how to deal with it.
@charmander

This introduces overhead to every JSON encoding in order to accommodate one specific type. I would suggest moving the JSON.stringify step to user code when you need to stringify a BigInt; I don’t think it belongs in pg.

Also, I wouldn’t expect BigInt to be serialized as a JSON string.

@ales-albert-kilbergr

Also, I wouldn’t expect BigInt to be serialized as a JSON string.

A big int can exceed a range for number in javascript therefor there is not much other options how to serialize it in JSON than a string currently.

I would suggest moving the JSON.stringify step to user code when you need to stringify a BigInt

That would be a good option.

I don’t think it belongs in pg.

BigInt is standartized javascript primitive type. Why shouldn`t be possible to pass it as a property for JSONB object?

Sign up for free to join this conversation on . Already have an account? Sign in to comment
None yet
None yet

Successfully merging this pull request may close these issues.