Skip to content

Commit 325d09c

Browse files
drew-grossflovilmart
authored andcommitted
Schema format cleanup
1 parent ef92a79 commit 325d09c

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

src/Adapters/Storage/Mongo/MongoSchemaCollection.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,20 @@ class MongoSchemaCollection {
115115
});
116116
}
117117

118+
// Add a collection. Currently the input is in mongo format, but that will change to Parse format in a
119+
// later PR. Returns a promise that is expected to resolve with the newly created schema, in Parse format.
120+
// If the class already exists, returns a promise that rejects with undefined as the reason. If the collection
121+
// can't be added for a reason other than it already existing, requirements for rejection reason are TBD.
118122
addSchema(name: string, fields) {
119123
let mongoObject = _mongoSchemaObjectFromNameFields(name, fields);
120-
return this._collection.insertOne(mongoObject);
124+
return this._collection.insertOne(mongoObject)
125+
.then(result => mongoSchemaToParseSchema(result.ops[0]))
126+
.catch(error => {
127+
if (error.code === 11000) { //Mongo's duplicate key error
128+
return Promise.reject();
129+
}
130+
return Promise.reject(error);
131+
});
121132
}
122133

123134
updateSchema(name: string, update) {

src/Schema.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -132,17 +132,16 @@ function validateCLP(perms) {
132132
});
133133
});
134134
}
135-
// Valid classes must:
136-
// Be one of _User, _Installation, _Role, _Session OR
137-
// Be a join table OR
138-
// Include only alpha-numeric and underscores, and not start with an underscore or number
139135
var joinClassRegex = /^_Join:[A-Za-z0-9_]+:[A-Za-z0-9_]+/;
140136
var classAndFieldRegex = /^[A-Za-z][A-Za-z0-9_]*$/;
141137
function classNameIsValid(className) {
142-
return (systemClasses.indexOf(className) > -1 ||
143-
className === '_SCHEMA' || //TODO: remove this, as _SCHEMA is not a valid class name for storing Parse Objects.
138+
// Valid classes must:
139+
return (
140+
// Be one of _User, _Installation, _Role, _Session OR
141+
systemClasses.indexOf(className) > -1 ||
142+
// Be a join table OR
144143
joinClassRegex.test(className) ||
145-
//Class names have the same constraints as field names, but also allow the previous additional names.
144+
// Include only alpha-numeric and underscores, and not start with an underscore or number
146145
fieldNameIsValid(className)
147146
);
148147
}
@@ -272,14 +271,13 @@ class Schema {
272271
}
273272

274273
return this._collection.addSchema(className, mongoObject.result)
275-
//TODO: Move this logic into the database adapter
276-
.then(result => MongoSchemaCollection._TESTmongoSchemaToParseSchema(result.ops[0]))
277-
.catch(error => {
278-
if (error.code === 11000) { //Mongo's duplicate key error
279-
throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME, `Class ${className} already exists.`);
280-
}
281-
return Promise.reject(error);
282-
});
274+
.catch(error => {
275+
if (error === undefined) {
276+
throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME, `Class ${className} already exists.`);
277+
} else {
278+
throw new Parse.Error(Parse.Error.INTERNAL_SERVER_ERROR, 'Database adapter error.');
279+
}
280+
});
283281
}
284282

285283
updateClass(className, submittedFields, classLevelPermissions, database) {

0 commit comments

Comments
 (0)