You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
DECIMAL values are returned differently in node-mysql and node-mysql2. In node-mysql they are returned as type Number while in node-mysql2 they are returned as type String, which seems wrong to me.
Minimal test case:
varmysql=require('mysql'),mysql2=require('mysql2'),connection;connection=mysql.createConnection(JSON.parse(process.env.MYSQL_CONFIG));connection2=mysql2.createConnection(JSON.parse(process.env.MYSQL_CONFIG));connection.query('CREATE TABLE decimal_table (d DECIMAL(4,2))',function(){connection.query('INSERT INTO decimal_table VALUES (0.1)',function(){varquery=connection.query('SELECT * FROM decimal_table LIMIT 1');varquery2=connection2.query('SELECT * FROM decimal_table LIMIT 1');query.on('result',function(r){console.log('node-mysql',r.d,typeofr.d);connection.end();});query2.on('result',function(r){console.log('node-mysql2',r.d,typeofr.d);connection2.end();});});});
hi, this is intended ( but I'm open for discussion if that should be that way )
DECIMAL values are stored and transmitted as strings, and often used where exact precision is required ( accounting etc ), so I assume in that case some precision arithmetic library would be used ( Bignumber, bn.js etc ) and data would be passed as string to that library
Is this incompatibility causing any particular problems in your code?
@jaredjenkins if absolutely critical we could have decimalAsNumber flag to force conversion to number (at a cost of losing precision), but I'd rather avoid adding more flags
I see for I'm assuming this isn't the behavior for double or float? I am
dealing with a legacy codebase - not exactly clear why the previous dev
chose Decimal but it seems rather overkill.
Understood - I think we can comfortably move to a Float, but this is a breaking change from mysql -> mysql2 and Sequelize >= v4 won't work properly because of it. I am working on a PR with Sequelize. Can probably figure out how to make this configurable on their end.
Activity
sidorares commentedon Dec 15, 2015
hi, this is intended ( but I'm open for discussion if that should be that way )
DECIMAL values are stored and transmitted as strings, and often used where exact precision is required ( accounting etc ), so I assume in that case some precision arithmetic library would be used ( Bignumber, bn.js etc ) and data would be passed as string to that library
Is this incompatibility causing any particular problems in your code?
mlarcher commentedon Sep 24, 2016
Is there way to apply the same library transformation to all decimal fields before they reach the consuming code ?
sushantdhiman commentedon Sep 24, 2016
You can use
typeCastoption as used in test heremlarcher commentedon Sep 24, 2016
thanks, looks like precisely what we need :)
jaredjenkins commentedon Mar 31, 2017
Related issue for Sequelize: sequelize/sequelize#7465
sidorares commentedon Mar 31, 2017
@jaredjenkins if absolutely critical we could have
decimalAsNumberflag to force conversion to number (at a cost of losing precision), but I'd rather avoid adding more flagsjaredjenkins commentedon Mar 31, 2017
sidorares commentedon Mar 31, 2017
To me Decimal is very special, I'd suggest to update schema to use floats or ints depending on semantic if no real need for DECIMAL type
normal int/float types returned as JS Number
node-mysql2/lib/compile_text_parser.js
Line 118 in 9995606
floats:
node-mysql2/lib/packets/packet.js
Lines 582 to 625 in 9995606
ints:
node-mysql2/lib/packets/packet.js
Lines 383 to 489 in 9995606
jaredjenkins commentedon Mar 31, 2017
Understood - I think we can comfortably move to a Float, but this is a breaking change from mysql -> mysql2 and Sequelize >= v4 won't work properly because of it. I am working on a PR with Sequelize. Can probably figure out how to make this configurable on their end.
jaredjenkins commentedon Mar 31, 2017
So I take that back - we definitely want DECIMAL, but still think this is fine for the client (Sequelize) to resolve.