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

Handle date type #91

Closed
Biktop opened this issue Mar 1, 2016 · 2 comments
Closed

Handle date type #91

Biktop opened this issue Mar 1, 2016 · 2 comments

Comments

@Biktop
Copy link

Biktop commented Mar 1, 2016

Thanks for excellent ORM.

I came across with next issue: I use date type in my postgre table and it seems that value is written correctly but when I read value from object I get something like
expire_at: Sat Feb 27 2016 19:00:00 GMT-0500 (EST)

Should I handle date type somehow especially?

@koskimas
Copy link
Collaborator

koskimas commented Mar 2, 2016

Objection.js doesn't do any type mapping automatically. Especially the javascript Date type sucks so hard that I never use it 😄

You have at least two options here:

Option 1: Add a mapper to the db client

For postgres pg client you can do something like this:

var types = require('pg').types
var TIMESTAMPTZ_OID = 1184
var TIMESTAMP_OID = 1114
var parseFn = function(val) {
   return val === null ? null : new Date(val)
}
types.setTypeParser(TIMESTAMPTZ_OID, parseFn)
types.setTypeParser(TIMESTAMP_OID, parseFn)

Option 2: Add mapping to objection model:

class MyBaseModel extends objection.Model {
  ...
  $parseDatabaseJson(json) {
    json = super.$parseDatabaseJson(json);
    json.expire_at = json.expire_at && new Date(json.expire_at);
    return json;
  }
  ...
}

Or if you use jsonSchema you can write a more generic approach like this:

class MyBaseModel extends objection.Model {
  ...
  $parseDatabaseJson(json) {
    json = super.$parseDatabaseJson(json);
    _.each(this.constructor.jsonSchema.properties, (schema, prop) => {
      if (schema.format === 'date') {
        json[prop] = json[prop] && new Date(json[prop]);
      }
    })
    return json;
  }
  ...
}

@Biktop
Copy link
Author

Biktop commented Mar 2, 2016

Thank you very much. I like the first option.
I've fixed problem by replacing Date type to moment something like this

const DATE_OID = 1082;
const types = require('pg').types;
types.setTypeParser(DATE_OID, val => val == null ? null : moment(val));

Additionally I found interesting discussing related similar problem.

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