-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
default number type parsing #339
Comments
Your post is well thought out, well researched, and I appreciate the time you put into it. I removed I think the best way to move forward would be to build a test suite that demonstrates all of these types being parsed properly per your suggestions and also demonstrates the edge cases where parsing as another type (say parsing a I'm not sure everyone will agree on any way to do it, and like you mentioned the type parsers are exposed for the user, but I would definitely like to remove as many "wtfs" as possible from a standard node-postgres install. What do you think? |
Based on the issues I referenced, most of the "complaining" is specific to Regardless, I can create a if (num instanceof PgBigInt) {
// use favorite big integer library
} |
I really appreciate the time you put into this and the outcome. Thank you a million. |
No problemo. :) |
I would like to reopen the discussion because I strongly disagree with the current implementation.
Relevant issues: #107, #266, #271, #296, #301
By default, I think any PG type which maps directly to a Javascript type should be converted to that type. If there isn't a direct mapping, then simply returning a string is fine. Of course, the user can override the type parser if they have different needs. This is how node-mysql handles type parsing which I believe to be correct.
What is the problem? Right now all decimal numbers are returned as strings, and 64-bit integers are parsed as integers. Some of the former have a direct mapping to a Javascript type, and the latter loses precision.
The Postgres docs specify that
real
anddouble precision
are "implementations of IEEE Standard 754 for Binary Floating-Point Arithmetic (single and double precision, respectively)". Javascript only has one number type: IEEE Standard 754 double precision. All Javascript numbers whether they are integers or decimals are converted to IEEE Standard 754 double precision.This means that
parseFloat()
can safely be used forreal
anddouble precision
. Using strings is confusing to the user since it is not expected and requires them to do more work by overriding the type parser without any real benefit.Javascript number representation causes a problem for
numeric/decimal
,bigint
,bigserial
since there could be a loss of precision. This is a case where a string is appropriate. All other PG number types can be represented properly with a Javascript number without a loss of precision.Below shows what I think to be the correct, default parsing.
The text was updated successfully, but these errors were encountered: