Skip to content

Commit d22436c

Browse files
committed
Move "No two geopoints" logic into mongo adapter
1 parent fc1cdd4 commit d22436c

File tree

4 files changed

+51
-39
lines changed

4 files changed

+51
-39
lines changed

spec/ParseGeoPoint.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ describe('Parse.GeoPoint testing', () => {
8585
equal(results.length, 3);
8686
done();
8787
}, (err) => {
88-
console.log(err);
89-
fail();
88+
fail("Couldn't query GeoPoint");
89+
fail(err)
9090
});
9191
});
9292

src/Adapters/Storage/Mongo/MongoSchemaCollection.js

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -184,21 +184,45 @@ class MongoSchemaCollection {
184184
return this._collection.upsertOne(_mongoSchemaQueryFromNameQuery(name, query), update);
185185
}
186186

187-
updateField(className: string, fieldName: string, type: string) {
188-
// We don't have this field. Update the schema.
189-
// Note that we use the $exists guard and $set to avoid race
190-
// conditions in the database. This is important!
191-
let query = {};
192-
query[fieldName] = { '$exists': false };
193-
let update = {};
194-
if (typeof type === 'string') {
195-
type = {
196-
type: type
187+
// Add a field to the schema. If database does not support the field
188+
// type (e.g. mongo doesn't support more than one GeoPoint in a class) reject with an "Incorrect Type"
189+
// Parse error with a desciptive message. If the field already exists, this function must
190+
// not modify the schema, and must reject with an error. Exact error format is TBD. If this function
191+
// is called for a class that doesn't exist, this function must create that class.
192+
193+
// TODO: throw an error if an unsupported field type is passed. Deciding whether a type is supported
194+
// should be the job of the adapter. Some adapters may not support GeoPoint at all. Others may
195+
// Support additional types that Mongo doesn't, like Money, or something.
196+
197+
// TODO: don't spend an extra query on finding the schema if the type we are trying to add isn't a GeoPoint.
198+
addFieldIfNotExists(className: string, fieldName: string, type: string) {
199+
return this.findSchema(className)
200+
.then(schema => {
201+
// The schema exists. Check for existing GeoPoints.
202+
if (type.type === 'GeoPoint') {
203+
// Make sure there are not other geopoint fields
204+
if (Object.keys(schema.fields).some(existingField => schema.fields[existingField].type === 'GeoPoint')) {
205+
return Promise.reject(new Parse.Error(Parse.Error.INCORRECT_TYPE, 'MongoDB only supports one GeoPoint field in a class.'));
206+
}
197207
}
198-
}
199-
update[fieldName] = parseFieldTypeToMongoFieldType(type);
200-
update = {'$set': update};
201-
return this.upsertSchema(className, query, update);
208+
return Promise.resolve();
209+
}, error => {
210+
// If error is undefined, the schema doesn't exist, and we can create the schema with the field.
211+
// If some other error, reject with it.
212+
if (error === undefined) {
213+
return Promise.resolve()
214+
}
215+
throw Promise.reject(error);
216+
})
217+
.then(() => {
218+
// We use $exists and $set to avoid overwriting the field type if it
219+
// already exists. (it could have added inbetween the last query and the update)
220+
return this.upsertSchema(
221+
className,
222+
{ [fieldName]: { '$exists': false } },
223+
{ '$set' : { [fieldName]: parseFieldTypeToMongoFieldType(type) } }
224+
);
225+
});
202226
}
203227
}
204228

src/Schema.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -464,18 +464,11 @@ class Schema {
464464
return Promise.resolve(this);
465465
}
466466

467-
if (type === 'GeoPoint') {
468-
// Make sure there are not other geopoint fields
469-
for (let otherKey in this.data[className]) {
470-
if (this.data[className][otherKey].type === 'GeoPoint') {
471-
throw new Parse.Error(
472-
Parse.Error.INCORRECT_TYPE,
473-
'there can only be one geopoint field in a class');
474-
}
475-
}
467+
if (typeof type === 'string') {
468+
type = { type };
476469
}
477470

478-
return this._collection.updateField(className, fieldName, type).then(() => {
471+
return this._collection.addFieldIfNotExists(className, fieldName, type).then(() => {
479472
// The update succeeded. Reload the schema
480473
return this.reloadData();
481474
}, () => {

src/transform.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -346,23 +346,20 @@ function CannotTransform() {}
346346
// Raises an error if this cannot possibly be valid REST format.
347347
// Returns CannotTransform if it's just not an atom, or if force is
348348
// true, throws an error.
349-
function transformAtom(atom, force, options) {
350-
options = options || {};
351-
var inArray = options.inArray;
352-
var inObject = options.inObject;
349+
function transformAtom(atom, force, {
350+
inArray,
351+
inObject,
352+
} = {}) {
353353
switch(typeof atom) {
354354
case 'string':
355355
case 'number':
356356
case 'boolean':
357357
return atom;
358-
359358
case 'undefined':
360359
return atom;
361360
case 'symbol':
362361
case 'function':
363-
throw new Parse.Error(Parse.Error.INVALID_JSON,
364-
'cannot transform value: ' + atom);
365-
362+
throw new Parse.Error(Parse.Error.INVALID_JSON, `cannot transform value: ${atom}`);
366363
case 'object':
367364
if (atom instanceof Date) {
368365
// Technically dates are not rest format, but, it seems pretty
@@ -377,7 +374,7 @@ function transformAtom(atom, force, options) {
377374
// TODO: check validity harder for the __type-defined types
378375
if (atom.__type == 'Pointer') {
379376
if (!inArray && !inObject) {
380-
return atom.className + '$' + atom.objectId;
377+
return `${atom.className}$${atom.objectId}`;
381378
}
382379
return {
383380
__type: 'Pointer',
@@ -402,15 +399,13 @@ function transformAtom(atom, force, options) {
402399
}
403400

404401
if (force) {
405-
throw new Parse.Error(Parse.Error.INVALID_JSON,
406-
'bad atom: ' + atom);
402+
throw new Parse.Error(Parse.Error.INVALID_JSON, `bad atom: ${atom}`);
407403
}
408404
return CannotTransform;
409405

410406
default:
411407
// I don't think typeof can ever let us get here
412-
throw new Parse.Error(Parse.Error.INTERNAL_SERVER_ERROR,
413-
'really did not expect value: ' + atom);
408+
throw new Parse.Error(Parse.Error.INTERNAL_SERVER_ERROR, `really did not expect value: ${atom}`);
414409
}
415410
}
416411

0 commit comments

Comments
 (0)