-
-
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
Treatment of Dates #285
Comments
node-postgres does its best to parse various datatypes from postgres back into their JavaScript counterpart. The parseFloat thing was bad since it could lead to actual data loss (and you would be non the wiser). As far as parsing back into a date, I think this is the 99% good case. Since parsing into various JavaScript types does not work in all cases the type parsing is exposed as a part of the postgres API (with tests to enforce this) and allows anyone to override a particular type parser. In your case, if you want to treat dates from postgres as strings it's as easy as this: require('pg').types.setTypeParser(1082, function(val) { return val; }); Here are the default type parser configurations: https://github.com/brianc/node-postgres/blob/master/lib/textParsers.js#L166 I plan on documenting this more clearly in the wiki soon. |
This is fantastic. Thank you for the prompt and thorough explanation! |
🎸 💃 |
If anyone else is needs this, the above example didn't work quite as expected. I ended up using:
The key being the String(), which I gleaned from https://github.com/brianc/node-postgres/blob/master/lib/types/index.js#L11 |
With 1.0 on the horizon due to the wonderful new treatment of numeric types (#271) I'd like to ask about how and why node-postgres treats dates as javascript objects. It may be I am simply doing things wrong or missing a setting.
Here is my use case:
The first two steps function acceptably, however the fetching of a row returns a javascript date object. This means that if I'm storing only the date, I need to have metadata surrounding the column type, and utilize it to transform the javascript date back into a string.
This seems silly considering it's then being transformed from string -> javascript date -> string.
So, the questions:
pg.defaults.parseDate = false
parameter, which defaults to true?If code must be written, I could create a pull request before Friday to hopefully sneak it into 1.0.
Please advise (and thanks for the great library!)
The text was updated successfully, but these errors were encountered: