Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: deal with bigint types in objects stored as jsonb #2947

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ales-albert-kilbergr
Copy link

@ales-albert-kilbergr ales-albert-kilbergr commented Apr 1, 2023

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
Copy link
Collaborator

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
Copy link
Author

ales-albert-kilbergr commented Apr 4, 2023

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 GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants