Skip to content
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

Closed
robraux opened this issue Feb 27, 2013 · 4 comments
Closed

Treatment of Dates #285

robraux opened this issue Feb 27, 2013 · 4 comments

Comments

@robraux
Copy link
Contributor

robraux commented Feb 27, 2013

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.

Please advise (and thanks for the great library!)

@brianc
Copy link
Owner

brianc commented 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:

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.

@robraux
Copy link
Contributor Author

robraux commented Feb 27, 2013

This is fantastic. Thank you for the prompt and thorough explanation!

@robraux robraux closed this as completed Feb 27, 2013
@brianc
Copy link
Owner

brianc commented Feb 27, 2013

🎸 💃

@robraux
Copy link
Contributor Author

robraux commented 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants