Schema.js cleanup (#1540)

* Tidy up schemas router

* de-duplicate logic for injecting default schemas

* Remove no-op promise

* use hasClass

* Make getAllSchemas part of SchemaController

* Move getOneSchema logic onto schema controller

* remove log
This commit is contained in:
Drew
2016-04-18 17:06:00 -07:00
parent 414ee6740d
commit ab0ea09067
5 changed files with 103 additions and 104 deletions

View File

@@ -14,36 +14,24 @@ function classNameMismatchResponse(bodyClass, pathClass) {
);
}
function injectDefaultSchema(schema) {
let defaultSchema = Schema.defaultColumns[schema.className];
if (defaultSchema) {
Object.keys(defaultSchema).forEach((key) => {
schema.fields[key] = defaultSchema[key];
});
}
return schema;
}
function getAllSchemas(req) {
return req.config.database.schemaCollection()
.then(collection => collection.getAllSchemas())
.then(schemas => schemas.map(injectDefaultSchema))
.then(schemas => ({ response: { results: schemas } }));
return req.config.database.loadSchema()
.then(schemaController => schemaController.getAllSchemas())
.then(schemas => ({ response: { results: schemas } }));
}
function getOneSchema(req) {
const className = req.params.className;
return req.config.database.schemaCollection()
.then(collection => collection.findSchema(className))
.then(injectDefaultSchema)
.then(schema => ({ response: schema }))
.catch(error => {
if (error === undefined) {
throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME, `Class ${className} does not exist.`);
} else {
throw new Parse.Error(Parse.Error.INTERNAL_SERVER_ERROR, 'Database adapter error.');
}
});
return req.config.database.loadSchema()
.then(schemaController => schemaController.getOneSchema(className))
.then(schema => ({ response: schema }))
.catch(error => {
if (error === undefined) {
throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME, `Class ${className} does not exist.`);
} else {
throw new Parse.Error(Parse.Error.INTERNAL_SERVER_ERROR, 'Database adapter error.');
}
});
}
function createSchema(req) {
@@ -72,19 +60,8 @@ function modifySchema(req) {
let className = req.params.className;
return req.config.database.loadSchema()
.then(schema => {
return schema.updateClass(className, submittedFields, req.body.classLevelPermissions, req.config.database);
}).then((result) => {
return Promise.resolve({response: result});
});
}
function getSchemaPermissions(req) {
var className = req.params.className;
return req.config.database.loadSchema()
.then(schema => {
return Promise.resolve({response: schema.perms[className]});
});
.then(schema => schema.updateClass(className, submittedFields, req.body.classLevelPermissions, req.config.database))
.then(result => ({response: result}));
}
// A helper function that removes all join tables for a schema. Returns a promise.