Skip to content

Commit

Permalink
LDS: Improve querying dates (#808)
Browse files Browse the repository at this point in the history
* LDS: Improve querying dates

Can query from Date Object / String comparison

* fix Date
  • Loading branch information
dplewis authored Apr 30, 2019
1 parent 342a5ac commit df81cba
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
39 changes: 39 additions & 0 deletions integration/test/ParseLocalDatastoreTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2590,6 +2590,45 @@ function runTest(controller) {
});
});

it(`${controller.name} can query from date`, async () => {
const now = new Date();
const obj = new TestObject({ dateField: now });
await obj.save();
await obj.pin();

let q = new Parse.Query(TestObject);
q.equalTo('dateField', now);
q.fromLocalDatastore();
let objects = await q.find();
assert.equal(objects.length, 1);

const future = new Date(now.getTime() + 1000);
q = new Parse.Query(TestObject);
q.lessThan('dateField', future);
q.fromLocalDatastore();
objects = await q.find();
assert.equal(objects.length, 1);

q = new Parse.Query(TestObject);
q.lessThanOrEqualTo('dateField', now);
q.fromLocalDatastore();
objects = await q.find();
assert.equal(objects.length, 1);

const past = new Date(now.getTime() - 1000);
q = new Parse.Query(TestObject);
q.greaterThan('dateField', past);
q.fromLocalDatastore();
objects = await q.find();
assert.equal(objects.length, 1);

q = new Parse.Query(TestObject);
q.greaterThanOrEqualTo('dateField', now);
q.fromLocalDatastore();
objects = await q.find();
assert.equal(objects.length, 1);
});

it(`${controller.name} supports withinPolygon`, async () => {
const sacramento = new TestObject();
sacramento.set('location', new Parse.GeoPoint(38.52, -121.50));
Expand Down
8 changes: 6 additions & 2 deletions src/OfflineQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,12 @@ function matchesKeyConstraints(className, object, objects, key, constraints) {
if (compareTo.__type) {
compareTo = decode(compareTo);
}
if (toString.call(compareTo) === '[object Date]') {
object[key] = new Date(object[key]);
// Compare Date Object or Date String
if (toString.call(compareTo) === '[object Date]' ||
(typeof compareTo === 'string' &&
new Date(compareTo) !== 'Invalid Date' &&
!isNaN(new Date(compareTo)))) {
object[key] = new Date(object[key].iso ? object[key].iso : object[key]);
}
switch (condition) {
case '$lt':
Expand Down

0 comments on commit df81cba

Please sign in to comment.