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
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:
Accept JSON data into an API w/ISO dates, or date times.
Save the date/datetime/time into appropriate Postgres typed field.
At a future point in time, retrieve the date/datetime/time out of the system and return it over JSON.
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:
Is there something I'm missing that allow me to treat dates solely as strings as things stand right now?
If no to above, would you be willing to accept a pull request which allows this functionality to exist, perhaps through a 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.
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:
Activity
brianc commentedon Feb 27, 2013
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:
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.
robraux commentedon Feb 27, 2013
This is fantastic. Thank you for the prompt and thorough explanation!
brianc commentedon Feb 27, 2013
🎸 💃
robraux commentedon Apr 5, 2013
If anyone else is needs this, the above example didn't work quite as expected.
I ended up using:
pg.types.setTypeParser(1082, 'text', function(val) { return String(val) })The key being the String(), which I gleaned from https://github.com/brianc/node-postgres/blob/master/lib/types/index.js#L11