Remove direct mongo access from SchemaRouter.modify, Schema.deleteField.
This commit is contained in:
@@ -483,7 +483,7 @@ describe('Schema', () => {
|
|||||||
.then(schema => schema.deleteField('installationId', '_Installation'))
|
.then(schema => schema.deleteField('installationId', '_Installation'))
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
expect(error.code).toEqual(136);
|
expect(error.code).toEqual(136);
|
||||||
expect(error.error).toEqual('field installationId cannot be changed');
|
expect(error.message).toEqual('field installationId cannot be changed');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -493,7 +493,7 @@ describe('Schema', () => {
|
|||||||
.then(schema => schema.deleteField('field', 'NoClass'))
|
.then(schema => schema.deleteField('field', 'NoClass'))
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
expect(error.code).toEqual(Parse.Error.INVALID_CLASS_NAME);
|
expect(error.code).toEqual(Parse.Error.INVALID_CLASS_NAME);
|
||||||
expect(error.error).toEqual('class NoClass does not exist');
|
expect(error.message).toEqual('Class NoClass does not exist.');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -504,7 +504,7 @@ describe('Schema', () => {
|
|||||||
.then(schema => schema.deleteField('missingField', 'HasAllPOD'))
|
.then(schema => schema.deleteField('missingField', 'HasAllPOD'))
|
||||||
.fail(error => {
|
.fail(error => {
|
||||||
expect(error.code).toEqual(255);
|
expect(error.code).toEqual(255);
|
||||||
expect(error.error).toEqual('field missingField does not exist, cannot delete');
|
expect(error.message).toEqual('Field missingField does not exist, cannot delete.');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -523,11 +523,11 @@ describe('Schema', () => {
|
|||||||
config.database.adapter.database.collection('test__Join:aRelation:HasPointersAndRelations', { strict: true }, (err, coll) => {
|
config.database.adapter.database.collection('test__Join:aRelation:HasPointersAndRelations', { strict: true }, (err, coll) => {
|
||||||
expect(err).toEqual(null);
|
expect(err).toEqual(null);
|
||||||
config.database.loadSchema()
|
config.database.loadSchema()
|
||||||
.then(schema => schema.deleteField('aRelation', 'HasPointersAndRelations', config.database.adapter.database, 'test_'))
|
.then(schema => schema.deleteField('aRelation', 'HasPointersAndRelations', config.database))
|
||||||
.then(() => config.database.adapter.database.collection('test__Join:aRelation:HasPointersAndRelations', { strict: true }, (err, coll) => {
|
.then(() => config.database.adapter.database.collection('test__Join:aRelation:HasPointersAndRelations', { strict: true }, (err, coll) => {
|
||||||
expect(err).not.toEqual(null);
|
expect(err).not.toEqual(null);
|
||||||
done();
|
done();
|
||||||
}))
|
}));
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
@@ -538,7 +538,7 @@ describe('Schema', () => {
|
|||||||
var obj2 = hasAllPODobject();
|
var obj2 = hasAllPODobject();
|
||||||
var p = Parse.Object.saveAll([obj1, obj2])
|
var p = Parse.Object.saveAll([obj1, obj2])
|
||||||
.then(() => config.database.loadSchema())
|
.then(() => config.database.loadSchema())
|
||||||
.then(schema => schema.deleteField('aString', 'HasAllPOD', config.database.adapter.database, 'test_'))
|
.then(schema => schema.deleteField('aString', 'HasAllPOD', config.database))
|
||||||
.then(() => new Parse.Query('HasAllPOD').get(obj1.id))
|
.then(() => new Parse.Query('HasAllPOD').get(obj1.id))
|
||||||
.then(obj1Reloaded => {
|
.then(obj1Reloaded => {
|
||||||
expect(obj1Reloaded.get('aString')).toEqual(undefined);
|
expect(obj1Reloaded.get('aString')).toEqual(undefined);
|
||||||
@@ -568,7 +568,7 @@ describe('Schema', () => {
|
|||||||
expect(obj1.get('aPointer').id).toEqual(obj1.id);
|
expect(obj1.get('aPointer').id).toEqual(obj1.id);
|
||||||
})
|
})
|
||||||
.then(() => config.database.loadSchema())
|
.then(() => config.database.loadSchema())
|
||||||
.then(schema => schema.deleteField('aPointer', 'NewClass', config.database.adapter.database, 'test_'))
|
.then(schema => schema.deleteField('aPointer', 'NewClass', config.database))
|
||||||
.then(() => new Parse.Query('NewClass').get(obj1.id))
|
.then(() => new Parse.Query('NewClass').get(obj1.id))
|
||||||
.then(obj1 => {
|
.then(obj1 => {
|
||||||
expect(obj1.get('aPointer')).toEqual(undefined);
|
expect(obj1.get('aPointer')).toEqual(undefined);
|
||||||
|
|||||||
@@ -369,7 +369,7 @@ describe('schemas', () => {
|
|||||||
}, (error, response, body) => {
|
}, (error, response, body) => {
|
||||||
expect(response.statusCode).toEqual(400);
|
expect(response.statusCode).toEqual(400);
|
||||||
expect(body.code).toEqual(Parse.Error.INVALID_CLASS_NAME);
|
expect(body.code).toEqual(Parse.Error.INVALID_CLASS_NAME);
|
||||||
expect(body.error).toEqual('class NoClass does not exist');
|
expect(body.error).toEqual('Class NoClass does not exist.');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -390,7 +390,7 @@ describe('schemas', () => {
|
|||||||
}, (error, response, body) => {
|
}, (error, response, body) => {
|
||||||
expect(response.statusCode).toEqual(400);
|
expect(response.statusCode).toEqual(400);
|
||||||
expect(body.code).toEqual(255);
|
expect(body.code).toEqual(255);
|
||||||
expect(body.error).toEqual('field aString exists, cannot update');
|
expect(body.error).toEqual('Field aString exists, cannot update.');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
@@ -412,7 +412,7 @@ describe('schemas', () => {
|
|||||||
}, (error, response, body) => {
|
}, (error, response, body) => {
|
||||||
expect(response.statusCode).toEqual(400);
|
expect(response.statusCode).toEqual(400);
|
||||||
expect(body.code).toEqual(255);
|
expect(body.code).toEqual(255);
|
||||||
expect(body.error).toEqual('field nonExistentKey does not exist, cannot delete');
|
expect(body.error).toEqual('Field nonExistentKey does not exist, cannot delete.');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -114,59 +114,32 @@ function modifySchema(req) {
|
|||||||
return req.config.database.loadSchema()
|
return req.config.database.loadSchema()
|
||||||
.then(schema => {
|
.then(schema => {
|
||||||
if (!schema.data[className]) {
|
if (!schema.data[className]) {
|
||||||
return Promise.resolve({
|
throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME, `Class ${req.params.className} does not exist.`);
|
||||||
status: 400,
|
|
||||||
response: {
|
|
||||||
code: Parse.Error.INVALID_CLASS_NAME,
|
|
||||||
error: 'class ' + req.params.className + ' does not exist',
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
var existingFields = schema.data[className];
|
|
||||||
|
|
||||||
for (var submittedFieldName in submittedFields) {
|
|
||||||
if (existingFields[submittedFieldName] && submittedFields[submittedFieldName].__op !== 'Delete') {
|
|
||||||
return Promise.resolve({
|
|
||||||
status: 400,
|
|
||||||
response: {
|
|
||||||
code: 255,
|
|
||||||
error: 'field ' + submittedFieldName + ' exists, cannot update',
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!existingFields[submittedFieldName] && submittedFields[submittedFieldName].__op === 'Delete') {
|
let existingFields = schema.data[className];
|
||||||
return Promise.resolve({
|
Object.keys(submittedFields).forEach(name => {
|
||||||
status: 400,
|
let field = submittedFields[name];
|
||||||
response: {
|
if (existingFields[name] && field.__op !== 'Delete') {
|
||||||
code: 255,
|
throw new Parse.Error(255, `Field ${name} exists, cannot update.`);
|
||||||
error: 'field ' + submittedFieldName + ' does not exist, cannot delete',
|
}
|
||||||
|
if (!existingFields[name] && field.__op === 'Delete') {
|
||||||
|
throw new Parse.Error(255, `Field ${name} does not exist, cannot delete.`);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var newSchema = Schema.buildMergedSchemaObject(existingFields, submittedFields);
|
let newSchema = Schema.buildMergedSchemaObject(existingFields, submittedFields);
|
||||||
var mongoObject = Schema.mongoSchemaFromFieldsAndClassName(newSchema, className);
|
let mongoObject = Schema.mongoSchemaFromFieldsAndClassName(newSchema, className);
|
||||||
if (!mongoObject.result) {
|
if (!mongoObject.result) {
|
||||||
return Promise.resolve({
|
throw new Parse.Error(mongoObject.code, mongoObject.error);
|
||||||
status: 400,
|
|
||||||
response: mongoObject,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 a single save to _SCHEMA collection to handle all additions.
|
||||||
var deletionPromises = []
|
let deletionPromises = [];
|
||||||
Object.keys(submittedFields).forEach(submittedFieldName => {
|
Object.keys(submittedFields).forEach(submittedFieldName => {
|
||||||
if (submittedFields[submittedFieldName].__op === 'Delete') {
|
if (submittedFields[submittedFieldName].__op === 'Delete') {
|
||||||
var promise = req.config.database.connect()
|
let promise = schema.deleteField(submittedFieldName, className, req.config.database);
|
||||||
.then(() => schema.deleteField(
|
|
||||||
submittedFieldName,
|
|
||||||
className,
|
|
||||||
req.config.database.adapter.database,
|
|
||||||
req.config.database.collectionPrefix
|
|
||||||
));
|
|
||||||
deletionPromises.push(promise);
|
deletionPromises.push(promise);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -500,28 +500,17 @@ Schema.prototype.validateField = function(className, key, type, freeze) {
|
|||||||
|
|
||||||
// Passing the database and prefix is necessary in order to drop relation collections
|
// Passing the database and prefix is necessary in order to drop relation collections
|
||||||
// and remove fields from objects. Ideally the database would belong to
|
// and remove fields from objects. Ideally the database would belong to
|
||||||
// a database adapter and this fuction would close over it or access it via member.
|
// a database adapter and this function would close over it or access it via member.
|
||||||
Schema.prototype.deleteField = function(fieldName, className, database, prefix) {
|
Schema.prototype.deleteField = function(fieldName, className, database) {
|
||||||
if (!classNameIsValid(className)) {
|
if (!classNameIsValid(className)) {
|
||||||
return Promise.reject({
|
throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME, invalidClassNameMessage(className));
|
||||||
code: Parse.Error.INVALID_CLASS_NAME,
|
|
||||||
error: invalidClassNameMessage(className),
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!fieldNameIsValid(fieldName)) {
|
if (!fieldNameIsValid(fieldName)) {
|
||||||
return Promise.reject({
|
throw new Parse.Error(Parse.Error.INVALID_KEY_NAME, `invalid field name: ${fieldName}`);
|
||||||
code: Parse.Error.INVALID_KEY_NAME,
|
|
||||||
error: 'invalid field name: ' + fieldName,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Don't allow deleting the default fields.
|
//Don't allow deleting the default fields.
|
||||||
if (!fieldNameIsValidForClass(fieldName, className)) {
|
if (!fieldNameIsValidForClass(fieldName, className)) {
|
||||||
return Promise.reject({
|
throw new Parse.Error(136, `field ${fieldName} cannot be changed`);
|
||||||
code: 136,
|
|
||||||
error: 'field ' + fieldName + ' cannot be changed',
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.reload()
|
return this.reload()
|
||||||
@@ -529,51 +518,29 @@ Schema.prototype.deleteField = function(fieldName, className, database, prefix)
|
|||||||
return schema.hasClass(className)
|
return schema.hasClass(className)
|
||||||
.then(hasClass => {
|
.then(hasClass => {
|
||||||
if (!hasClass) {
|
if (!hasClass) {
|
||||||
return Promise.reject({
|
throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME, `Class ${className} does not exist.`);
|
||||||
code: Parse.Error.INVALID_CLASS_NAME,
|
|
||||||
error: 'class ' + className + ' does not exist',
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!schema.data[className][fieldName]) {
|
if (!schema.data[className][fieldName]) {
|
||||||
return Promise.reject({
|
throw new Parse.Error(255, `Field ${fieldName} does not exist, cannot delete.`);
|
||||||
code: 255,
|
|
||||||
error: 'field ' + fieldName + ' does not exist, cannot delete',
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (schema.data[className][fieldName].startsWith('relation<')) {
|
if (schema.data[className][fieldName].startsWith('relation<')) {
|
||||||
//For relations, drop the _Join table
|
//For relations, drop the _Join table
|
||||||
return database.dropCollection(prefix + '_Join:' + fieldName + ':' + className)
|
return database.dropCollection(`_Join:${fieldName}:${className}`);
|
||||||
//Save the _SCHEMA object
|
}
|
||||||
.then(() => this.collection.update({ _id: className }, { $unset: {[fieldName]: null }}));
|
|
||||||
} else {
|
// for non-relations, remove all the data.
|
||||||
//for non-relations, remove all the data. This is necessary to ensure that the data is still gone
|
// This is necessary to ensure that the data is still gone if they add the same field.
|
||||||
//if they add the same field.
|
return database.collection(className)
|
||||||
return new Promise((resolve, reject) => {
|
.then(collection => {
|
||||||
database.collection(prefix + className, (err, coll) => {
|
var mongoFieldName = schema.data[className][fieldName].startsWith('*') ? '_p_' + fieldName : fieldName;
|
||||||
if (err) {
|
return collection.update({}, { "$unset": { [mongoFieldName] : null } }, { multi: true });
|
||||||
reject(err);
|
});
|
||||||
} else {
|
|
||||||
var mongoFieldName = schema.data[className][fieldName].startsWith('*') ?
|
|
||||||
'_p_' + fieldName :
|
|
||||||
fieldName;
|
|
||||||
return coll.update({}, {
|
|
||||||
"$unset": { [mongoFieldName] : null },
|
|
||||||
}, {
|
|
||||||
multi: true,
|
|
||||||
})
|
})
|
||||||
//Save the _SCHEMA object
|
// Save the _SCHEMA object
|
||||||
.then(() => this.collection.update({ _id: className }, { $unset: {[fieldName]: null }}))
|
.then(() => this.collection.update({ _id: className }, { $unset: {[fieldName]: null }}));
|
||||||
.then(resolve)
|
|
||||||
.catch(reject);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
};
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Given a schema promise, construct another schema promise that
|
// Given a schema promise, construct another schema promise that
|
||||||
// validates this field once the schema loads.
|
// validates this field once the schema loads.
|
||||||
|
|||||||
Reference in New Issue
Block a user