This is a read-only copy of the MariaDB Knowledgebase generated on 2024-11-15. For the latest, interactive version please visit https://mariadb.com/kb/.

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.

see https://github.com/mariadb-corporation/mariadb-connector-nodejs/blob/master/documentation/promise-api.md#migrating-from-2x-or-mysqlmysql2-to-3x

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

Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.