2016-02-02 17:38:47 -08:00
|
|
|
// schemas.js
|
|
|
|
|
|
2016-11-24 15:47:41 -05:00
|
|
|
var Parse = require('parse/node').Parse,
|
2016-04-18 18:59:57 -07:00
|
|
|
SchemaController = require('../Controllers/SchemaController');
|
2016-02-02 17:38:47 -08:00
|
|
|
|
2016-03-07 13:44:45 -08:00
|
|
|
import PromiseRouter from '../PromiseRouter';
|
2016-03-01 15:17:10 -08:00
|
|
|
import * as middleware from "../middlewares";
|
2016-02-09 11:26:46 -08:00
|
|
|
|
|
|
|
|
function classNameMismatchResponse(bodyClass, pathClass) {
|
2016-03-02 21:38:06 -08:00
|
|
|
throw new Parse.Error(
|
|
|
|
|
Parse.Error.INVALID_CLASS_NAME,
|
|
|
|
|
`Class name mismatch between ${bodyClass} and ${pathClass}.`
|
|
|
|
|
);
|
2016-02-09 11:26:46 -08:00
|
|
|
}
|
|
|
|
|
|
2016-02-02 17:38:47 -08:00
|
|
|
function getAllSchemas(req) {
|
2016-07-23 06:23:59 +02:00
|
|
|
return req.config.database.loadSchema({ clearCache: true})
|
2017-06-20 09:15:26 -07:00
|
|
|
.then(schemaController => schemaController.getAllClasses(true))
|
|
|
|
|
.then(schemas => ({ response: { results: schemas } }));
|
2016-02-02 17:38:47 -08:00
|
|
|
}
|
|
|
|
|
|
2016-02-03 16:10:00 -08:00
|
|
|
function getOneSchema(req) {
|
2016-03-02 21:33:33 -08:00
|
|
|
const className = req.params.className;
|
2016-07-23 06:23:59 +02:00
|
|
|
return req.config.database.loadSchema({ clearCache: true})
|
2017-06-20 09:15:26 -07:00
|
|
|
.then(schemaController => schemaController.getOneSchema(className, true))
|
|
|
|
|
.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.');
|
|
|
|
|
}
|
|
|
|
|
});
|
2016-02-03 16:10:00 -08:00
|
|
|
}
|
|
|
|
|
|
2016-02-05 14:56:11 -08:00
|
|
|
function createSchema(req) {
|
|
|
|
|
if (req.params.className && req.body.className) {
|
|
|
|
|
if (req.params.className != req.body.className) {
|
2016-02-09 11:26:46 -08:00
|
|
|
return classNameMismatchResponse(req.body.className, req.params.className);
|
2016-02-05 14:56:11 -08:00
|
|
|
}
|
|
|
|
|
}
|
2016-03-02 21:38:06 -08:00
|
|
|
|
|
|
|
|
const className = req.params.className || req.body.className;
|
2016-02-05 14:56:11 -08:00
|
|
|
if (!className) {
|
2016-03-02 21:38:06 -08:00
|
|
|
throw new Parse.Error(135, `POST ${req.path} needs a class name.`);
|
2016-02-05 14:56:11 -08:00
|
|
|
}
|
2016-03-02 21:38:06 -08:00
|
|
|
|
2016-07-23 06:23:59 +02:00
|
|
|
return req.config.database.loadSchema({ clearCache: true})
|
2016-06-12 16:35:13 -07:00
|
|
|
.then(schema => schema.addClassIfNotExists(className, req.body.fields, req.body.classLevelPermissions))
|
2016-04-05 21:16:39 -07:00
|
|
|
.then(schema => ({ response: schema }));
|
2016-02-05 14:56:11 -08:00
|
|
|
}
|
|
|
|
|
|
2016-02-09 11:26:46 -08:00
|
|
|
function modifySchema(req) {
|
|
|
|
|
if (req.body.className && req.body.className != req.params.className) {
|
2016-02-16 12:30:30 -08:00
|
|
|
return classNameMismatchResponse(req.body.className, req.params.className);
|
2016-02-09 11:26:46 -08:00
|
|
|
}
|
|
|
|
|
|
2016-12-07 15:17:05 -08:00
|
|
|
const submittedFields = req.body.fields || {};
|
|
|
|
|
const className = req.params.className;
|
2016-02-09 11:26:46 -08:00
|
|
|
|
2016-07-23 06:23:59 +02:00
|
|
|
return req.config.database.loadSchema({ clearCache: true})
|
2017-06-20 09:15:26 -07:00
|
|
|
.then(schema => schema.updateClass(className, submittedFields, req.body.classLevelPermissions, req.config.database))
|
|
|
|
|
.then(result => ({response: result}));
|
2016-03-07 14:49:09 -05:00
|
|
|
}
|
|
|
|
|
|
2016-06-12 16:35:13 -07:00
|
|
|
const deleteSchema = req => {
|
2016-04-18 18:59:57 -07:00
|
|
|
if (!SchemaController.classNameIsValid(req.params.className)) {
|
|
|
|
|
throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME, SchemaController.invalidClassNameMessage(req.params.className));
|
2016-02-17 19:00:17 -08:00
|
|
|
}
|
2016-04-14 19:24:56 -04:00
|
|
|
return req.config.database.deleteSchema(req.params.className)
|
2017-06-20 09:15:26 -07:00
|
|
|
.then(() => ({ response: {} }));
|
2016-02-17 19:00:17 -08:00
|
|
|
}
|
|
|
|
|
|
2016-02-19 23:47:44 -05:00
|
|
|
export class SchemasRouter extends PromiseRouter {
|
|
|
|
|
mountRoutes() {
|
2016-03-01 15:17:10 -08:00
|
|
|
this.route('GET', '/schemas', middleware.promiseEnforceMasterKeyAccess, getAllSchemas);
|
|
|
|
|
this.route('GET', '/schemas/:className', middleware.promiseEnforceMasterKeyAccess, getOneSchema);
|
|
|
|
|
this.route('POST', '/schemas', middleware.promiseEnforceMasterKeyAccess, createSchema);
|
|
|
|
|
this.route('POST', '/schemas/:className', middleware.promiseEnforceMasterKeyAccess, createSchema);
|
|
|
|
|
this.route('PUT', '/schemas/:className', middleware.promiseEnforceMasterKeyAccess, modifySchema);
|
|
|
|
|
this.route('DELETE', '/schemas/:className', middleware.promiseEnforceMasterKeyAccess, deleteSchema);
|
2016-02-19 23:47:44 -05:00
|
|
|
}
|
|
|
|
|
}
|