Skip to content

Commit f27431c

Browse files
committed
Remove private Schema API usage from SchemasRouter.
1 parent 981d22f commit f27431c

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

src/Routers/SchemasRouter.js

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function getAllSchemas(req) {
1818
return req.config.database.adaptiveCollection('_SCHEMA')
1919
.then(collection => collection.find({}))
2020
.then(schemas => schemas.map(Schema.mongoSchemaToSchemaAPIResponse))
21-
.then(schemas => ({ response: { results: schemas }}));
21+
.then(schemas => ({ response: { results: schemas } }));
2222
}
2323

2424
function getOneSchema(req) {
@@ -65,7 +65,7 @@ function modifySchema(req) {
6565
throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME, `Class ${req.params.className} does not exist.`);
6666
}
6767

68-
let existingFields = Object.assign(schema.data[className], {_id: className});
68+
let existingFields = Object.assign(schema.data[className], { _id: className });
6969
Object.keys(submittedFields).forEach(name => {
7070
let field = submittedFields[name];
7171
if (existingFields[name] && field.__op !== 'Delete') {
@@ -83,24 +83,27 @@ function modifySchema(req) {
8383
}
8484

8585
// Finally we have checked to make sure the request is valid and we can start deleting fields.
86-
// Do all deletions first, then a single save to _SCHEMA collection to handle all additions.
87-
let deletionPromises = [];
88-
Object.keys(submittedFields).forEach(submittedFieldName => {
89-
if (submittedFields[submittedFieldName].__op === 'Delete') {
90-
let promise = schema.deleteField(submittedFieldName, className, req.config.database);
91-
deletionPromises.push(promise);
86+
// Do all deletions first, then add fields to avoid duplicate geopoint error.
87+
let deletePromises = [];
88+
let insertedFields = [];
89+
Object.keys(submittedFields).forEach(fieldName => {
90+
if (submittedFields[fieldName].__op === 'Delete') {
91+
const promise = schema.deleteField(fieldName, className, req.config.database);
92+
deletePromises.push(promise);
93+
} else {
94+
insertedFields.push(fieldName);
9295
}
9396
});
94-
95-
return Promise.all(deletionPromises)
96-
.then(() => new Promise((resolve, reject) => {
97-
schema.collection.update({_id: className}, mongoObject.result, {w: 1}, (err, docs) => {
98-
if (err) {
99-
reject(err);
100-
}
101-
resolve({ response: Schema.mongoSchemaToSchemaAPIResponse(mongoObject.result)});
102-
})
103-
}));
97+
return Promise.all(deletePromises) // Delete Everything
98+
.then(() => schema.reloadData()) // Reload our Schema, so we have all the new values
99+
.then(() => {
100+
let promises = insertedFields.map(fieldName => {
101+
const mongoType = mongoObject.result[fieldName];
102+
return schema.validateField(className, fieldName, mongoType);
103+
});
104+
return Promise.all(promises);
105+
})
106+
.then(() => ({ response: Schema.mongoSchemaToSchemaAPIResponse(mongoObject.result) }));
104107
});
105108
}
106109

@@ -140,7 +143,7 @@ function deleteSchema(req) {
140143
// We've dropped the collection now, so delete the item from _SCHEMA
141144
// and clear the _Join collections
142145
return req.config.database.adaptiveCollection('_SCHEMA')
143-
.then(coll => coll.findOneAndDelete({_id: req.params.className}))
146+
.then(coll => coll.findOneAndDelete({ _id: req.params.className }))
144147
.then(document => {
145148
if (document === null) {
146149
//tried to delete non-existent class

0 commit comments

Comments
 (0)