Closed jonasacres closed 12 years ago
I think this is a normal precision problem when dealing with big integers (64bit) in node. Maybe this link helps understanding the problem:
http://stackoverflow.com/questions/2983206/bitwise-and-in-javascript-with-a-64-bit-integer
Yikes. That would certainly explain it. Thanks!
Yeah, we had an issue on this before as well. In 95% of the cases people don't need the big-num support, so we default to returning numeric types as integers. You can and should override the type-parser for the oid of the type of column (numeric oid = 1700
) to return the number as a string instead of an integer. The type converter functions are given a string value as input so you can just return the input. In the test I set the type converter to always return the string 'yes.' Not sure why I did it that way, but it should still work as an example for you:
https://github.com/brianc/node-postgres/blob/master/test/integration/client/huge-numeric-tests.js#L3
Thanks @brianc. What is the best way to do this? I see that I can pass in a custom config object to pg.Client.query, but I'd rather set it at a higher level so that I don't need to pass it in every time. Is that possible?
@mjijackson from the link I posted in my last comment there's an example. Basically, this:
var types = require(__dirname + '/../../../lib/types');
//1231 = numericOID
types.setTypeParser(1700, function(){
return 'yes';
})
types.setTypeParser(1700, 'binary', function(){
return 'yes';
})
@brianc Sorry, I should have been more specific. My question is how do you actually require the types module from user code? As far as I can see I would need to do a require('pg/lib/types')
in order to access it, which is ok, but it feels like something that could break in the future. All node modules that I've used have had a single entry point on which everything else hangs, exactly like the native
module hangs on the pg
module in this package. Could we also hang the types
module on it as well?
Edit: Also, I'm not 100% sure that require('pg/lib/types')
is the blessed way to do a require. Seems to me that this kind of require used to not be allowed in node at all...
No worries. I believe require('module/subpath')
is an a-okay way to bring in sub-files of a module, but if you would rather you could do: var path = require('path'); require(path.join(require.resolve('pg'), 'lib/types'))).
or something like that.
You're right on though w/ saying it could/should be hung off the main module require as an object instead of a separate file. I'll leave this open until I can get to that.
Also, you're right that require('pg/lib/types')
didn't use to be allowed, which might be one reason why so many modules hang all their objects off the main require point including this one. :smile:
For anyone else that comes across this thread, the following worked for me:
var pg = require('pg');
pg.types.setTypeParser(20, String);
This tells node-postgres to parse all bigints that come back from the database as strings, so you won't lose digits when parseInt
tries to parse bigints as JavaScript integers.
Thanks @brianc!
Hi!
I'm admittedly new to Node.js and Postgres, so perhaps there is an environment issue that I am simply unaware of. Further, I'm a stranger to the project and am not sure if this is the appropriate place to file something like this. If I'm putting this in the wrong spot, my apologies, and I'd be grateful if you could direct me to where I ought to go.
I'm working with a dataset that uses bigints. It seems like there's an issue where node-postgres loses the lowest two bits when the MSB of a bigint is set. Here's an example:
So far, so good. Let's try it in node.js using node-postgres:
Notice that for the small values, the least significant bits are fine; but for the big values, the two LSBs are always 0. I see the same behavior happen in other tables, using both select *, as well as select column_name, and INSERT...RETURNING. I'm using node-postgres v0.8.1.
For the sake of completeness, I tried again using Python and psycopg2:
The output here matches the correct values, suggesting that this issue is specific to node-postgres. Any guidance would be tremendously appreciated. Thanks!
P.S.: Here's a summary of all the software versions I'm using.
For completeness' sake: