From 49eb9df1ef585819f24b9cdfc96edcf194eec84f Mon Sep 17 00:00:00 2001 From: Nikita Lutsenko Date: Mon, 7 Mar 2016 20:56:51 -0800 Subject: [PATCH] Remove private Schema API usage from SchemasRouter. --- src/Routers/SchemasRouter.js | 41 +++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/src/Routers/SchemasRouter.js b/src/Routers/SchemasRouter.js index 0d1a4cf931..74b15285bc 100644 --- a/src/Routers/SchemasRouter.js +++ b/src/Routers/SchemasRouter.js @@ -18,7 +18,7 @@ function getAllSchemas(req) { return req.config.database.adaptiveCollection('_SCHEMA') .then(collection => collection.find({})) .then(schemas => schemas.map(Schema.mongoSchemaToSchemaAPIResponse)) - .then(schemas => ({ response: { results: schemas }})); + .then(schemas => ({ response: { results: schemas } })); } function getOneSchema(req) { @@ -65,7 +65,7 @@ function modifySchema(req) { throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME, `Class ${req.params.className} does not exist.`); } - let existingFields = Object.assign(schema.data[className], {_id: className}); + let existingFields = Object.assign(schema.data[className], { _id: className }); Object.keys(submittedFields).forEach(name => { let field = submittedFields[name]; if (existingFields[name] && field.__op !== 'Delete') { @@ -83,24 +83,27 @@ function modifySchema(req) { } // Finally we have checked to make sure the request is valid and we can start deleting fields. - // Do all deletions first, then a single save to _SCHEMA collection to handle all additions. - let deletionPromises = []; - Object.keys(submittedFields).forEach(submittedFieldName => { - if (submittedFields[submittedFieldName].__op === 'Delete') { - let promise = schema.deleteField(submittedFieldName, className, req.config.database); - deletionPromises.push(promise); + // Do all deletions first, then add fields to avoid duplicate geopoint error. + let deletePromises = []; + let insertedFields = []; + Object.keys(submittedFields).forEach(fieldName => { + if (submittedFields[fieldName].__op === 'Delete') { + const promise = schema.deleteField(fieldName, className, req.config.database); + deletePromises.push(promise); + } else { + insertedFields.push(fieldName); } }); - - return Promise.all(deletionPromises) - .then(() => new Promise((resolve, reject) => { - schema.collection.update({_id: className}, mongoObject.result, {w: 1}, (err, docs) => { - if (err) { - reject(err); - } - resolve({ response: Schema.mongoSchemaToSchemaAPIResponse(mongoObject.result)}); - }) - })); + return Promise.all(deletePromises) // Delete Everything + .then(() => schema.reloadData()) // Reload our Schema, so we have all the new values + .then(() => { + let promises = insertedFields.map(fieldName => { + const mongoType = mongoObject.result[fieldName]; + return schema.validateField(className, fieldName, mongoType); + }); + return Promise.all(promises); + }) + .then(() => ({ response: Schema.mongoSchemaToSchemaAPIResponse(mongoObject.result) })); }); } @@ -140,7 +143,7 @@ function deleteSchema(req) { // We've dropped the collection now, so delete the item from _SCHEMA // and clear the _Join collections return req.config.database.adaptiveCollection('_SCHEMA') - .then(coll => coll.findOneAndDelete({_id: req.params.className})) + .then(coll => coll.findOneAndDelete({ _id: req.params.className })) .then(document => { if (document === null) { //tried to delete non-existent class