Unexpected result from mariaDB connector for node in select count
Hi,
what is the reason that mariadb connector for node.js returns [ { num: 2n } ] instead of [ { num: 2 } ] to a query like this:
SELECT count(*) as num FROM tasks
This doesn't happen when using mysql2. This "n" is causing further errors in other libraries that I'm using.
I checked the list of options but I didn't find anyone that might change this behaviour.
Thank you
Answer Answered by Diego Dupin in this comment.
javascript integer are safe until some limit (2^53). Resultset that return integer since 3.0 return a javascript BigInt (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt). Resultset are now safe.
To permit compatibility, options `insertIdAsNumber`, `decimalAsNumber` and `bigIntAsNumber` can be set to return unsafe value.
It can be done at connection level or query level. Example: const conn = await mariadb.createConnection({ host: 'mydb.com', user: 'myUser', password: 'myPwd', bigIntAsNumber: true, decimalAsNumber: true, bigIntAsNumber: true });
or for a specific query: const res = await conn.query({ sql:'SELECT count(*) as num FROM tasks', bigIntAsNumber: true, decimalAsNumber: true, bigIntAsNumber: true});
for this particular example "SELECT count(*) as num FROM tasks" count would never reach 9007199254740991