Schema format cleanup
This commit is contained in:
@@ -115,9 +115,20 @@ class MongoSchemaCollection {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add a collection. Currently the input is in mongo format, but that will change to Parse format in a
|
||||||
|
// later PR. Returns a promise that is expected to resolve with the newly created schema, in Parse format.
|
||||||
|
// If the class already exists, returns a promise that rejects with undefined as the reason. If the collection
|
||||||
|
// can't be added for a reason other than it already existing, requirements for rejection reason are TBD.
|
||||||
addSchema(name: string, fields) {
|
addSchema(name: string, fields) {
|
||||||
let mongoObject = _mongoSchemaObjectFromNameFields(name, fields);
|
let mongoObject = _mongoSchemaObjectFromNameFields(name, fields);
|
||||||
return this._collection.insertOne(mongoObject);
|
return this._collection.insertOne(mongoObject)
|
||||||
|
.then(result => mongoSchemaToParseSchema(result.ops[0]))
|
||||||
|
.catch(error => {
|
||||||
|
if (error.code === 11000) { //Mongo's duplicate key error
|
||||||
|
return Promise.reject();
|
||||||
|
}
|
||||||
|
return Promise.reject(error);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
updateSchema(name: string, update) {
|
updateSchema(name: string, update) {
|
||||||
|
|||||||
@@ -132,17 +132,16 @@ function validateCLP(perms) {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
// Valid classes must:
|
|
||||||
// Be one of _User, _Installation, _Role, _Session OR
|
|
||||||
// Be a join table OR
|
|
||||||
// Include only alpha-numeric and underscores, and not start with an underscore or number
|
|
||||||
var joinClassRegex = /^_Join:[A-Za-z0-9_]+:[A-Za-z0-9_]+/;
|
var joinClassRegex = /^_Join:[A-Za-z0-9_]+:[A-Za-z0-9_]+/;
|
||||||
var classAndFieldRegex = /^[A-Za-z][A-Za-z0-9_]*$/;
|
var classAndFieldRegex = /^[A-Za-z][A-Za-z0-9_]*$/;
|
||||||
function classNameIsValid(className) {
|
function classNameIsValid(className) {
|
||||||
return (systemClasses.indexOf(className) > -1 ||
|
// Valid classes must:
|
||||||
className === '_SCHEMA' || //TODO: remove this, as _SCHEMA is not a valid class name for storing Parse Objects.
|
return (
|
||||||
|
// Be one of _User, _Installation, _Role, _Session OR
|
||||||
|
systemClasses.indexOf(className) > -1 ||
|
||||||
|
// Be a join table OR
|
||||||
joinClassRegex.test(className) ||
|
joinClassRegex.test(className) ||
|
||||||
//Class names have the same constraints as field names, but also allow the previous additional names.
|
// Include only alpha-numeric and underscores, and not start with an underscore or number
|
||||||
fieldNameIsValid(className)
|
fieldNameIsValid(className)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -272,14 +271,13 @@ class Schema {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return this._collection.addSchema(className, mongoObject.result)
|
return this._collection.addSchema(className, mongoObject.result)
|
||||||
//TODO: Move this logic into the database adapter
|
.catch(error => {
|
||||||
.then(result => MongoSchemaCollection._TESTmongoSchemaToParseSchema(result.ops[0]))
|
if (error === undefined) {
|
||||||
.catch(error => {
|
throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME, `Class ${className} already exists.`);
|
||||||
if (error.code === 11000) { //Mongo's duplicate key error
|
} else {
|
||||||
throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME, `Class ${className} already exists.`);
|
throw new Parse.Error(Parse.Error.INTERNAL_SERVER_ERROR, 'Database adapter error.');
|
||||||
}
|
}
|
||||||
return Promise.reject(error);
|
});
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
updateClass(className, submittedFields, classLevelPermissions, database) {
|
updateClass(className, submittedFields, classLevelPermissions, database) {
|
||||||
|
|||||||
Reference in New Issue
Block a user