Decimal numbers are always returned as strings, not numbers
Database decimal numbers E.g.decimal(19.2) are always return as strings rather than numbers. Even when numbers are small. E.g the number 123 is returned as "123". I've tried various combinations of supportBigNumbers and bigNumberStrings as per your documentation but nothing seems to work
This is documented: https://github.com/sidorares/node-mysql2/tree/master/documentation#known-incompatibilities-with-node-mysql
My reasoning is that even small numbers when represented as float potentially serialized to string differently after string -> float -> string conversion so I keep initial string representation. It's up to you how to convert them.
What is the reason you have DECIMAL type in your db design @FredOleary ?
Thanks for the quick response. I've inherited the database so have no choice in the DECIMAL choice however I do need to have numbers returned to the app consumers be returned as numbers, not strings. My work around is to use parseFloat() but really prefer not to since other developers certainly won't be expecting this. (I wasn't) I'll check out the 'decimalNumbers' you provided. (BTW your readme doc on "supportBigNumbers" and 'bigNumberStrings ' does indicate that some combination of these will allow numbers to be returned
if you feel parts of documentation are misleading or confusing PR with fix is definitely welcome :)
( noticed almost identical discussion in #237 , linking it here)
One work around you can have is to use typeCast parameter like this:
var mysql = require("mysql");
var pool = mysql.createPool({
user: "...",
// ....
typeCast: function (field, next) {
if (field.type == "DECIMAL") {
var value = field.string();
return (value === null) ? null : Number(value);
}
return next();
}
});
Thanks... My bad on the documentation. I ended up on the wrong github from a search... Your documentation is quite clear on this
Why was that design approved? I couldn't imagine case when having decimal from mysql as string is ok. Of course, in some situations maybe, but that's quitly uncommon. Why won't you change the default setting for DECIMAL to return as number?
The reasoning is that DECIMAL type carries some special semantics and we want to preserve it. If someone who designed schema choose DECIMAL over regular number there might was a reason behind that
Also, the driver mostly just reflects what is sent at protocol level. In binary protocol ( prepared statements ) regular numbers are sent using IEEE 754 4 or 8 byte float but with DECIMAL number is sent as string
Oh, understood. So the real question is why mysql nodejs driver giveaways decimal as number by default. Ok. I hope origins of decimal type are important enough for being added to readme.
Thanks for quick response)
Wow I switched from mysql to mysql2 here, this seriously caught me out. This was a bad design decision.
Ran into this issue during mysql->mysql2 with a BIGINT column as well..
if you feel parts of documentation are misleading or confusing PR with fix is definitely welcome :)
( noticed almost identical discussion in #237 , linking it here)
One work around you can have is to use
typeCastparameter like this:var mysql = require("mysql"); var pool = mysql.createPool({ user: "...", // .... typeCast: function (field, next) { if (field.type == "DECIMAL") { var value = field.string(); return (value === null) ? null : Number(value); } return next(); } });
Thanks for the solution. For MariaDB 10.4.26 I've had to use field.type == "NEWDECIMAL"
like in documentation { decimalNumbers: true }
this is save myday, i got error on production server and parsing everything haha
down the rabbit hole.