Skip to content

Commit

Permalink
Merge pull request #284 from JohnyDays/patch-1
Browse files Browse the repository at this point in the history
Support unix timestamp dates - PR by @JohnyDays
  • Loading branch information
neumino committed Jul 12, 2015
2 parents 7ddbc15 + dbdaefa commit 03790d8
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,11 @@ Document.prototype.__makeSavableCopy = function(doc, schema, options, model, r)

// model is an instance of a Model (for the top level fields), or undefined
var result, key, keys, nextSchema, copyFlag;
if (type.isDate(schema) && (typeof doc === 'string')) {
if (type.isDate(schema) && (typeof doc === 'string' || typeof doc === 'number')) {
var numericDate = parseInt(doc, 10);
if(!isNaN(numericDate)) {
doc = numericDate;
}
return new Date(doc); // Use r.ISO8601 and not `new Date()` to keep timezone
}
else if (type.isPoint(schema)) {
Expand Down
6 changes: 5 additions & 1 deletion lib/type/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ TypeDate.prototype.validate = function(date, prefix, options) {
// TOIMPROVE -- we currently just check if it's a term from the driver
// We suppose for now that this is enough and we don't throw an error
}
else if (typeof date === 'string') {
else if (typeof date === 'string' || typeof date === 'number') {
var numericDate = parseInt(date, 10);
if(!isNaN(numericDate)){
date = numericDate;
}
jsDate = new Date(date);
if (jsDate.getTime() !== jsDate.getTime()) {
if (options.enforce_type === "strict") {
Expand Down
20 changes: 20 additions & 0 deletions test/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,26 @@ describe('save', function() {
done()
}).error(done)
});
it('Date as number should be coerced to ReQL dates', function(done){
var Model = thinky.createModel(modelNames[0], {
id: String,
date: Date
})
var t = new Model({
id: util.s8(),
date: Date.now()
});

t.save().then(function(result) {
assert(t.date instanceof Date)

return Model.get(t.id).execute({timeFormat: "raw"})
}).then(function(result) {
assert.equal(Object.prototype.toString.call(result.date), "[object Object]");
assert.equal(result.date.$reql_type$, "TIME");
done()
}).error(done)
});
it('Points as array should be coerced to ReQL points', function(done){
var Model = thinky.createModel(modelNames[0], {
id: String,
Expand Down
8 changes: 8 additions & 0 deletions test/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,14 @@ describe('Chainable types', function(){
var doc = new Model({ id: null })
doc.validate();
});
it('Date - number', function(){
var name = util.s8();
var Model = thinky.createModel(name,
{id: type.date()},
{init: false})
var doc = new Model({ id: Date.now() })
doc.validate();
});
it('Date - basic - wrong type', function(){
var name = util.s8();
var Model = thinky.createModel(name,
Expand Down

0 comments on commit 03790d8

Please sign in to comment.