Skip to content

Commit

Permalink
feat: localDatastore support for unsorted distance queries (#1570)
Browse files Browse the repository at this point in the history
  • Loading branch information
dplewis authored Oct 11, 2022
1 parent 4864d54 commit ea3e75f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
20 changes: 20 additions & 0 deletions integration/test/ParseLocalDatastoreTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2735,6 +2735,26 @@ function runTest(controller) {
assert.equal(objects.length, 1);
});

it(`${controller.name} supports withinKilometers`, async () => {
const object = new TestObject();
const firstPoint = new Parse.GeoPoint({ latitude: 40.0, longitude: -30.0 });
object.set({ location: firstPoint });
await object.save();
await object.pin();

const sorted = false;
const query = new Parse.Query(TestObject);
query.withinKilometers(
'location',
new Parse.GeoPoint({ latitude: 40.0, longitude: -30.0 }),
2,
sorted
);
query.fromLocalDatastore();
const results = await query.find();
assert.equal(results.length, 1);
});

it(`${controller.name} supports withinPolygon`, async () => {
const sacramento = new TestObject();
sacramento.set('location', new Parse.GeoPoint(38.52, -121.5));
Expand Down
22 changes: 19 additions & 3 deletions src/OfflineQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -513,9 +513,25 @@ function matchesKeyConstraints(className, object, objects, key, constraints) {
return true;
}
case '$geoWithin': {
const points = compareTo.$polygon.map(geoPoint => [geoPoint.latitude, geoPoint.longitude]);
const polygon = new ParsePolygon(points);
return polygon.containsPoint(object[key]);
if (compareTo.$polygon) {
const points = compareTo.$polygon.map(geoPoint => [
geoPoint.latitude,
geoPoint.longitude,
]);
const polygon = new ParsePolygon(points);
return polygon.containsPoint(object[key]);
}
if (compareTo.$centerSphere) {
const [WGS84Point, maxDistance] = compareTo.$centerSphere;
const centerPoint = new ParseGeoPoint({
latitude: WGS84Point[1],
longitude: WGS84Point[0],
});
const point = new ParseGeoPoint(object[key]);
const distance = point.radiansTo(centerPoint);
return distance <= maxDistance;
}
break;
}
case '$geoIntersects': {
const polygon = new ParsePolygon(object[key].coordinates);
Expand Down
9 changes: 9 additions & 0 deletions src/__tests__/OfflineQuery-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,15 @@ describe('OfflineQuery', () => {
expect(matchesQuery(q.className, pt, [], q)).toBe(true);
});

it('matches $centerSphere queries', () => {
const pt = new ParseObject('Checkin');
pt.set('location', new ParseGeoPoint(40, 40));

const q = new ParseQuery('Checkin');
q.withinRadians('location', new ParseGeoPoint(30, 30), 0.3, false);
expect(matchesQuery(q.className, pt, [], q)).toBe(true);
});

it('matches $within queries', () => {
const caltrainStation = new ParseObject('Checkin');
caltrainStation
Expand Down

0 comments on commit ea3e75f

Please sign in to comment.