add support for geoWithin.centerSphere queries via withJSON (#4825)
* add support for geoWithin.centerSphere queries via withJSON * added test for passing array of lat, lng instead of Parse.GeoPoint * added postgres support * added more tests * improved tests and validation * added more tests
This commit is contained in:
@@ -535,6 +535,30 @@ const buildWhereClause = ({ schema, query, index }): WhereClause => {
|
||||
index += 2;
|
||||
}
|
||||
|
||||
if (fieldValue.$geoWithin && fieldValue.$geoWithin.$centerSphere) {
|
||||
const centerSphere = fieldValue.$geoWithin.$centerSphere;
|
||||
if (!(centerSphere instanceof Array) || centerSphere.length < 2) {
|
||||
throw new Parse.Error(Parse.Error.INVALID_JSON, 'bad $geoWithin value; $centerSphere should be an array of Parse.GeoPoint and distance');
|
||||
}
|
||||
// Get point, convert to geo point if necessary and validate
|
||||
let point = centerSphere[0];
|
||||
if (point instanceof Array && point.length === 2) {
|
||||
point = new Parse.GeoPoint(point[1], point[0]);
|
||||
} else if (!GeoPointCoder.isValidJSON(point)) {
|
||||
throw new Parse.Error(Parse.Error.INVALID_JSON, 'bad $geoWithin value; $centerSphere geo point invalid');
|
||||
}
|
||||
Parse.GeoPoint._validate(point.latitude, point.longitude);
|
||||
// Get distance and validate
|
||||
const distance = centerSphere[1];
|
||||
if(isNaN(distance) || distance < 0) {
|
||||
throw new Parse.Error(Parse.Error.INVALID_JSON, 'bad $geoWithin value; $centerSphere distance invalid');
|
||||
}
|
||||
const distanceInKM = distance * 6371 * 1000;
|
||||
patterns.push(`ST_distance_sphere($${index}:name::geometry, POINT($${index + 1}, $${index + 2})::geometry) <= $${index + 3}`);
|
||||
values.push(fieldName, point.longitude, point.latitude, distanceInKM);
|
||||
index += 4;
|
||||
}
|
||||
|
||||
if (fieldValue.$geoWithin && fieldValue.$geoWithin.$polygon) {
|
||||
const polygon = fieldValue.$geoWithin.$polygon;
|
||||
let points;
|
||||
@@ -1986,4 +2010,13 @@ function literalizeRegexPart(s: string) {
|
||||
);
|
||||
}
|
||||
|
||||
var GeoPointCoder = {
|
||||
isValidJSON(value) {
|
||||
return (typeof value === 'object' &&
|
||||
value !== null &&
|
||||
value.__type === 'GeoPoint'
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
export default PostgresStorageAdapter;
|
||||
|
||||
Reference in New Issue
Block a user