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

Support JavaScript BigInt primitive for MySQL BIGINT type

Open yshrsmz opened this issue 1 year ago • 3 comments

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt

Instead of treating MySQL BIGINT as String, It would be great if we could use JavaScript BigInt primitive.

yshrsmz avatar Oct 13 '24 09:10 yshrsmz

This can fail silently by downcasting a bigint returned from the database into a JS number, causing data loss if the same value is later saved, for example SELECT followed by UPDATE.

In TypeScript, when casting the rows to types, the downcast is not obvious. Luckily this was caught using integration tests.

erikschul avatar Jan 30 '25 15:01 erikschul

The connection can be configured to return strings, but why not use native types? @sidorares


  /**
   * When dealing with big numbers (BIGINT and DECIMAL columns) in the database, you should enable this option
   * (Default: false)
   */
  supportBigNumbers?: boolean;

  /**
   * Enabling both supportBigNumbers and bigNumberStrings forces big numbers (BIGINT and DECIMAL columns) to be
   * always returned as JavaScript String objects (Default: false). Enabling supportBigNumbers but leaving
   * bigNumberStrings disabled will return big numbers as String objects only when they cannot be accurately
   * represented with [JavaScript Number objects](https://262.ecma-international.org/5.1/#sec-8.5)
   * (which happens when they exceed the [-2^53, +2^53] range), otherwise they will be returned as Number objects.
   * This option is ignored if supportBigNumbers is disabled.
   */
  bigNumberStrings?: boolean;

erikschul avatar Jan 30 '25 15:01 erikschul

any news on that? BigInt is supported since Node 10.5, i think it's time to use it

dzek69 avatar Apr 03 '25 20:04 dzek69