Remove private Schema API usage from SchemasRouter.

This commit is contained in:
Nikita Lutsenko
2016-03-07 20:56:51 -08:00
parent fb5b8fb58f
commit 49eb9df1ef

View File

@@ -18,7 +18,7 @@ function getAllSchemas(req) {
return req.config.database.adaptiveCollection('_SCHEMA') return req.config.database.adaptiveCollection('_SCHEMA')
.then(collection => collection.find({})) .then(collection => collection.find({}))
.then(schemas => schemas.map(Schema.mongoSchemaToSchemaAPIResponse)) .then(schemas => schemas.map(Schema.mongoSchemaToSchemaAPIResponse))
.then(schemas => ({ response: { results: schemas }})); .then(schemas => ({ response: { results: schemas } }));
} }
function getOneSchema(req) { 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.`); 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 => { Object.keys(submittedFields).forEach(name => {
let field = submittedFields[name]; let field = submittedFields[name];
if (existingFields[name] && field.__op !== 'Delete') { 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. // 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. // Do all deletions first, then add fields to avoid duplicate geopoint error.
let deletionPromises = []; let deletePromises = [];
Object.keys(submittedFields).forEach(submittedFieldName => { let insertedFields = [];
if (submittedFields[submittedFieldName].__op === 'Delete') { Object.keys(submittedFields).forEach(fieldName => {
let promise = schema.deleteField(submittedFieldName, className, req.config.database); if (submittedFields[fieldName].__op === 'Delete') {
deletionPromises.push(promise); const promise = schema.deleteField(fieldName, className, req.config.database);
deletePromises.push(promise);
} else {
insertedFields.push(fieldName);
} }
}); });
return Promise.all(deletePromises) // Delete Everything
return Promise.all(deletionPromises) .then(() => schema.reloadData()) // Reload our Schema, so we have all the new values
.then(() => new Promise((resolve, reject) => { .then(() => {
schema.collection.update({_id: className}, mongoObject.result, {w: 1}, (err, docs) => { let promises = insertedFields.map(fieldName => {
if (err) { const mongoType = mongoObject.result[fieldName];
reject(err); return schema.validateField(className, fieldName, mongoType);
} });
resolve({ response: Schema.mongoSchemaToSchemaAPIResponse(mongoObject.result)}); 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 // We've dropped the collection now, so delete the item from _SCHEMA
// and clear the _Join collections // and clear the _Join collections
return req.config.database.adaptiveCollection('_SCHEMA') return req.config.database.adaptiveCollection('_SCHEMA')
.then(coll => coll.findOneAndDelete({_id: req.params.className})) .then(coll => coll.findOneAndDelete({ _id: req.params.className }))
.then(document => { .then(document => {
if (document === null) { if (document === null) {
//tried to delete non-existent class //tried to delete non-existent class