Skip to content
This repository has been archived by the owner on Mar 4, 2019. It is now read-only.

Commit

Permalink
fix: date casting in documents should use timestamptz (fixes #563)
Browse files Browse the repository at this point in the history
* explicitly using is not comparison

* fix for timestamp casting in docs
  • Loading branch information
eduardomourar authored and dmfay committed Mar 10, 2018
1 parent f9684ac commit dd9e16b
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/where.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ exports.docPredicate = function (result, condition, value, conditions) {
} else if(_.isDate(value)) {
result.params.push(value);
result.predicates.push(
util.format("(body ->> '%s')::timestamp %s $%d",
util.format("(body ->> '%s')::timestamptz %s $%d",
condition.field,
condition.operator,
result.params.length + result.offset)
Expand Down
2 changes: 1 addition & 1 deletion test/db/schemata/default.sql
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ values (1, 'Power Age'),

insert into myschema.docs(body)
values('{"title":"A Document","price":22,"description":"lorem ipsum etc","is_good":true,"created_at":"2015-03-04T09:43:41.643Z"}'),
('{"title":"Another Document","price":18,"description":"Macaroni and Cheese","is_good":true,"created_at":"2015-03-04T09:43:41.643Z"}'),
('{"title":"Another Document","price":18,"description":"Macaroni and Cheese","is_good":true,"created_at":"2015-03-04T06:43:41.643-03:00"}'),
('{"title":"Starsky and Hutch","price":6,"description":"Two buddies fighting crime","is_good":false,"created_at":"1977-03-04T09:43:41.643Z","studios": [{"name" : "Warner"}, {"name" : "Universal"}]}');

create or replace function all_products()
Expand Down
10 changes: 9 additions & 1 deletion test/document_query_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,20 @@ describe('Document queries', function () {
});

it('works properly with dates', function (done) {
db.docs.findDoc({"created_at <" : new Date(1980, 1,1)}, function(err,docs){
db.docs.findDoc({"created_at <" : new Date(1980, 1, 1)}, function(err,docs){
assert.ifError(err);
assert.equal(docs.length, 1);
done();
});
});

it('works properly with timestamp including time zone ', function (done) {
db.docs.findDoc({"created_at >" : new Date("2015-03-04T09:00:00.000Z")}, function(err,docs){
assert.ifError(err);
assert.equal(docs.length, 2);
done();
});
});
});

describe('Querying with options', function () {
Expand Down
2 changes: 1 addition & 1 deletion test/where_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ describe('WHERE clause generation', function () {
var condition = {field: 'field', operator: '<>'};
var result = where.docPredicate({params: [], predicates: [], offset: 0}, condition, date, {'field <>': date});
assert.equal(result.predicates.length, 1);
assert.equal(result.predicates[0], '(body ->> \'field\')::timestamp <> $1');
assert.equal(result.predicates[0], '(body ->> \'field\')::timestamptz <> $1');
assert.equal(result.params.length, 1);
assert.equal(result.params[0], date);
});
Expand Down

0 comments on commit dd9e16b

Please sign in to comment.