Skip to content

Commit 2a5904b

Browse files
committed
Move "No two geopoints" logic into mongo adapter
1 parent a727e1c commit 2a5904b

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
@@ -185,21 +185,45 @@ class MongoSchemaCollection {
185185
return this._collection.upsertOne(_mongoSchemaQueryFromNameQuery(name, query), update);
186186
}
187187

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

205229
get transform() {

src/Adapters/Storage/Mongo/MongoTransform.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -348,23 +348,20 @@ function CannotTransform() {}
348348
// Raises an error if this cannot possibly be valid REST format.
349349
// Returns CannotTransform if it's just not an atom, or if force is
350350
// true, throws an error.
351-
function transformAtom(atom, force, options) {
352-
options = options || {};
353-
var inArray = options.inArray;
354-
var inObject = options.inObject;
351+
function transformAtom(atom, force, {
352+
inArray,
353+
inObject,
354+
} = {}) {
355355
switch(typeof atom) {
356356
case 'string':
357357
case 'number':
358358
case 'boolean':
359359
return atom;
360-
361360
case 'undefined':
362361
return atom;
363362
case 'symbol':
364363
case 'function':
365-
throw new Parse.Error(Parse.Error.INVALID_JSON,
366-
'cannot transform value: ' + atom);
367-
364+
throw new Parse.Error(Parse.Error.INVALID_JSON, `cannot transform value: ${atom}`);
368365
case 'object':
369366
if (atom instanceof Date) {
370367
// Technically dates are not rest format, but, it seems pretty
@@ -379,7 +376,7 @@ function transformAtom(atom, force, options) {
379376
// TODO: check validity harder for the __type-defined types
380377
if (atom.__type == 'Pointer') {
381378
if (!inArray && !inObject) {
382-
return atom.className + '$' + atom.objectId;
379+
return `${atom.className}$${atom.objectId}`;
383380
}
384381
return {
385382
__type: 'Pointer',
@@ -404,15 +401,13 @@ function transformAtom(atom, force, options) {
404401
}
405402

406403
if (force) {
407-
throw new Parse.Error(Parse.Error.INVALID_JSON,
408-
'bad atom: ' + atom);
404+
throw new Parse.Error(Parse.Error.INVALID_JSON, `bad atom: ${atom}`);
409405
}
410406
return CannotTransform;
411407

412408
default:
413409
// I don't think typeof can ever let us get here
414-
throw new Parse.Error(Parse.Error.INTERNAL_SERVER_ERROR,
415-
'really did not expect value: ' + atom);
410+
throw new Parse.Error(Parse.Error.INTERNAL_SERVER_ERROR, `really did not expect value: ${atom}`);
416411
}
417412
}
418413

src/Schema.js

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

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

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

0 commit comments

Comments
 (0)