Fix the coercion issue related to GeoPoint near

This commit is contained in:
Raymond Feng 2013-12-14 09:49:11 -08:00
parent dab99c4ebb
commit fab504b5d9
3 changed files with 29 additions and 6 deletions

View File

@ -396,10 +396,24 @@ DataAccessObject._coerce = function (where) {
if (!DataType) { if (!DataType) {
continue; continue;
} }
if (DataType === geo.GeoPoint) {
// Skip the GeoPoint as the near operator breaks the assumption that
// an operation has only one property
// We should probably fix it based on
// http://docs.mongodb.org/manual/reference/operator/query/near/
// The other option is to make operators start with $
continue;
}
var val = where[p]; var val = where[p];
// Check there is an operator // Check there is an operator
var operator = null; var operator = null;
if ('object' === typeof val && Object.keys(val).length === 1) { if ('object' === typeof val) {
if (Object.keys(val).length !== 1) {
// Skip if there are not only one properties
continue;
}
for (var op in operators) { for (var op in operators) {
if (op in val) { if (op in val) {
val = val[op]; val = val[op];
@ -455,6 +469,11 @@ DataAccessObject.find = function find(params, cb) {
var constr = this; var constr = this;
params = params || {}; params = params || {};
if(params.where) {
params.where = this._coerce(params.where);
}
var fields = params.fields; var fields = params.fields;
var near = params && geo.nearFilter(params.where); var near = params && geo.nearFilter(params.where);
var supportsGeo = !!this.getDataSource().connector.buildNearFilter; var supportsGeo = !!this.getDataSource().connector.buildNearFilter;
@ -465,9 +484,6 @@ DataAccessObject.find = function find(params, cb) {
} }
params = removeUndefined(params); params = removeUndefined(params);
if(params.where) {
params.where = this._coerce(params.where);
}
if(near) { if(near) {
if(supportsGeo) { if(supportsGeo) {
// convert it // convert it

View File

@ -106,8 +106,8 @@ function GeoPoint(data) {
} }
assert(typeof data === 'object', 'must provide a lat and lng object when creating a GeoPoint'); assert(typeof data === 'object', 'must provide a lat and lng object when creating a GeoPoint');
assert(typeof data.lat === 'number', 'lat must be a number when creating a GeoPoint'); assert(typeof data.lat === 'number' && !isNaN(data.lat), 'lat must be a number when creating a GeoPoint');
assert(typeof data.lng === 'number', 'lng must be a number when creating a GeoPoint'); assert(typeof data.lng === 'number' && !isNaN(data.lng), 'lng must be a number when creating a GeoPoint');
assert(data.lng <= 180, 'lng must be <= 180'); assert(data.lng <= 180, 'lng must be <= 180');
assert(data.lng >= -180, 'lng must be >= -180'); assert(data.lng >= -180, 'lng must be >= -180');
assert(data.lat <= 90, 'lat must be <= 90'); assert(data.lat <= 90, 'lat must be <= 90');

View File

@ -582,6 +582,7 @@ describe('DataAccessObject', function () {
age: Number, age: Number,
vip: Boolean, vip: Boolean,
date: Date, date: Date,
location: 'GeoPoint',
scores: [Number] scores: [Number]
}); });
}); });
@ -647,6 +648,12 @@ describe('DataAccessObject', function () {
assert.deepEqual(where, {vip: false}); assert.deepEqual(where, {vip: false});
}); });
it('should skip GeoPoint', function () {
where = model._coerce({location: {near: {lng: 10, lat: 20}, maxDistance: 20}});
assert.deepEqual(where, {location: {near: {lng: 10, lat: 20}, maxDistance: 20}});
});
}); });
describe('Load models from json', function () { describe('Load models from json', function () {