planetscale/database-js

json_object converts bigints to numbers

robinjonsson opened this issue · 2 comments

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 } },
]

Possible workaround:

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

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.

#90

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