Skip to content

Commit df81cba

Browse files
authored
LDS: Improve querying dates (#808)
* LDS: Improve querying dates Can query from Date Object / String comparison * fix Date
1 parent 342a5ac commit df81cba

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

integration/test/ParseLocalDatastoreTest.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2590,6 +2590,45 @@ function runTest(controller) {
25902590
});
25912591
});
25922592

2593+
it(`${controller.name} can query from date`, async () => {
2594+
const now = new Date();
2595+
const obj = new TestObject({ dateField: now });
2596+
await obj.save();
2597+
await obj.pin();
2598+
2599+
let q = new Parse.Query(TestObject);
2600+
q.equalTo('dateField', now);
2601+
q.fromLocalDatastore();
2602+
let objects = await q.find();
2603+
assert.equal(objects.length, 1);
2604+
2605+
const future = new Date(now.getTime() + 1000);
2606+
q = new Parse.Query(TestObject);
2607+
q.lessThan('dateField', future);
2608+
q.fromLocalDatastore();
2609+
objects = await q.find();
2610+
assert.equal(objects.length, 1);
2611+
2612+
q = new Parse.Query(TestObject);
2613+
q.lessThanOrEqualTo('dateField', now);
2614+
q.fromLocalDatastore();
2615+
objects = await q.find();
2616+
assert.equal(objects.length, 1);
2617+
2618+
const past = new Date(now.getTime() - 1000);
2619+
q = new Parse.Query(TestObject);
2620+
q.greaterThan('dateField', past);
2621+
q.fromLocalDatastore();
2622+
objects = await q.find();
2623+
assert.equal(objects.length, 1);
2624+
2625+
q = new Parse.Query(TestObject);
2626+
q.greaterThanOrEqualTo('dateField', now);
2627+
q.fromLocalDatastore();
2628+
objects = await q.find();
2629+
assert.equal(objects.length, 1);
2630+
});
2631+
25932632
it(`${controller.name} supports withinPolygon`, async () => {
25942633
const sacramento = new TestObject();
25952634
sacramento.set('location', new Parse.GeoPoint(38.52, -121.50));

src/OfflineQuery.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,12 @@ function matchesKeyConstraints(className, object, objects, key, constraints) {
137137
if (compareTo.__type) {
138138
compareTo = decode(compareTo);
139139
}
140-
if (toString.call(compareTo) === '[object Date]') {
141-
object[key] = new Date(object[key]);
140+
// Compare Date Object or Date String
141+
if (toString.call(compareTo) === '[object Date]' ||
142+
(typeof compareTo === 'string' &&
143+
new Date(compareTo) !== 'Invalid Date' &&
144+
!isNaN(new Date(compareTo)))) {
145+
object[key] = new Date(object[key].iso ? object[key].iso : object[key]);
142146
}
143147
switch (condition) {
144148
case '$lt':

0 commit comments

Comments
 (0)