planetscale / database-js

A Fetch API-compatible PlanetScale database driver
https://planetscale.com/docs/tutorials/planetscale-serverless-driver
Apache License 2.0
1.17k stars 35 forks source link

json_object converts bigints to numbers #152

Closed robinjonsson closed 11 months ago

robinjonsson commented 11 months ago

Hi,

When you use json_object, bigints are converted to numbers and not strings. Is that the intended behavior?

Example:

Schema:

CREATE TABLE users (
  id bigint AUTO_INCREMENT,
  PRIMARY KEY (id)
)

Query:

const results = await conn.execute(
  `SELECT json_object('id', id) as user FROM users`
);
console.log(results.rows);

Output:

[
  { user: { id: 1 } },
]
robinjonsson commented 11 months ago

Possible workaround:

const results = await conn.execute(
  `SELECT json_object('id', CAST(id as CHAR)) as user FROM users`
);
mattrobenolt commented 11 months ago

I'm not exactly sure where something is being lost in translation here, but in JSON, there is no "bigint". JSON has "numbers" which are a float64.

In our driver, we had some internal discussions about supporting bigint types natively when they are expressed over the wire, and we chose to express them as just JavaScript numbers.

https://github.com/planetscale/database-js/pull/90

But this is why int64 typically is represented as a string in JavaScript since you lose precision outside of the BigInt type.