-
Notifications
You must be signed in to change notification settings - Fork 640
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
Comments
Objection.js doesn't do any type mapping automatically. Especially the javascript You have at least two options here: Option 1: Add a mapper to the db client For postgres 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 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;
}
...
} |
Thank you very much. I like the first option.
Additionally I found interesting discussing related similar problem. |
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?
The text was updated successfully, but these errors were encountered: