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
|
|
|
|
2018-09-01 13:58:06 -04:00
|
|
|
import PromiseRouter from '../PromiseRouter';
|
|
|
|
|
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) {
|
2018-09-01 13:58:06 -04:00
|
|
|
return req.config.database
|
|
|
|
|
.loadSchema({ clearCache: true })
|
2020-07-13 17:13:08 -05: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;
|
2018-09-01 13:58:06 -04:00
|
|
|
return req.config.database
|
|
|
|
|
.loadSchema({ clearCache: true })
|
2020-07-13 17:13:08 -05:00
|
|
|
.then(schemaController => schemaController.getOneSchema(className, true))
|
|
|
|
|
.then(schema => ({ response: schema }))
|
|
|
|
|
.catch(error => {
|
2017-06-20 09:15:26 -07:00
|
|
|
if (error === undefined) {
|
2018-09-01 13:58:06 -04:00
|
|
|
throw new Parse.Error(
|
|
|
|
|
Parse.Error.INVALID_CLASS_NAME,
|
|
|
|
|
`Class ${className} does not exist.`
|
|
|
|
|
);
|
2017-06-20 09:15:26 -07:00
|
|
|
} else {
|
2018-09-01 13:58:06 -04:00
|
|
|
throw new Parse.Error(
|
|
|
|
|
Parse.Error.INTERNAL_SERVER_ERROR,
|
|
|
|
|
'Database adapter error.'
|
|
|
|
|
);
|
2017-06-20 09:15:26 -07:00
|
|
|
}
|
|
|
|
|
});
|
2016-02-03 16:10:00 -08:00
|
|
|
}
|
|
|
|
|
|
2016-02-05 14:56:11 -08:00
|
|
|
function createSchema(req) {
|
2017-10-26 15:35:07 -04:00
|
|
|
if (req.auth.isReadOnly) {
|
2018-09-01 13:58:06 -04:00
|
|
|
throw new Parse.Error(
|
|
|
|
|
Parse.Error.OPERATION_FORBIDDEN,
|
|
|
|
|
"read-only masterKey isn't allowed to create a schema."
|
|
|
|
|
);
|
2017-10-26 15:35:07 -04:00
|
|
|
}
|
2016-02-05 14:56:11 -08:00
|
|
|
if (req.params.className && req.body.className) {
|
|
|
|
|
if (req.params.className != req.body.className) {
|
2018-09-01 13:58:06 -04: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
|
|
|
|
2018-09-01 13:58:06 -04:00
|
|
|
return req.config.database
|
|
|
|
|
.loadSchema({ clearCache: true })
|
2020-07-13 17:13:08 -05:00
|
|
|
.then(schema =>
|
2018-09-01 13:58:06 -04:00
|
|
|
schema.addClassIfNotExists(
|
|
|
|
|
className,
|
|
|
|
|
req.body.fields,
|
|
|
|
|
req.body.classLevelPermissions,
|
|
|
|
|
req.body.indexes
|
|
|
|
|
)
|
|
|
|
|
)
|
2020-07-13 17:13:08 -05:00
|
|
|
.then(schema => ({ response: schema }));
|
2016-02-05 14:56:11 -08:00
|
|
|
}
|
|
|
|
|
|
2016-02-09 11:26:46 -08:00
|
|
|
function modifySchema(req) {
|
2017-10-26 15:35:07 -04:00
|
|
|
if (req.auth.isReadOnly) {
|
2018-09-01 13:58:06 -04:00
|
|
|
throw new Parse.Error(
|
|
|
|
|
Parse.Error.OPERATION_FORBIDDEN,
|
|
|
|
|
"read-only masterKey isn't allowed to update a schema."
|
|
|
|
|
);
|
2017-10-26 15:35:07 -04:00
|
|
|
}
|
2016-02-09 11:26:46 -08:00
|
|
|
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
|
|
|
|
2018-09-01 13:58:06 -04:00
|
|
|
return req.config.database
|
|
|
|
|
.loadSchema({ clearCache: true })
|
2020-07-13 17:13:08 -05:00
|
|
|
.then(schema =>
|
2018-09-01 13:58:06 -04:00
|
|
|
schema.updateClass(
|
|
|
|
|
className,
|
|
|
|
|
submittedFields,
|
|
|
|
|
req.body.classLevelPermissions,
|
|
|
|
|
req.body.indexes,
|
|
|
|
|
req.config.database
|
|
|
|
|
)
|
|
|
|
|
)
|
2020-07-13 17:13:08 -05:00
|
|
|
.then(result => ({ response: result }));
|
2016-03-07 14:49:09 -05:00
|
|
|
}
|
|
|
|
|
|
2020-07-13 17:13:08 -05:00
|
|
|
const deleteSchema = req => {
|
2017-10-26 15:35:07 -04:00
|
|
|
if (req.auth.isReadOnly) {
|
2018-09-01 13:58:06 -04:00
|
|
|
throw new Parse.Error(
|
|
|
|
|
Parse.Error.OPERATION_FORBIDDEN,
|
|
|
|
|
"read-only masterKey isn't allowed to delete a schema."
|
|
|
|
|
);
|
2017-10-26 15:35:07 -04:00
|
|
|
}
|
2016-04-18 18:59:57 -07:00
|
|
|
if (!SchemaController.classNameIsValid(req.params.className)) {
|
2018-09-01 13:58:06 -04:00
|
|
|
throw new Parse.Error(
|
|
|
|
|
Parse.Error.INVALID_CLASS_NAME,
|
|
|
|
|
SchemaController.invalidClassNameMessage(req.params.className)
|
|
|
|
|
);
|
2016-02-17 19:00:17 -08:00
|
|
|
}
|
2018-09-01 13:58:06 -04:00
|
|
|
return req.config.database
|
|
|
|
|
.deleteSchema(req.params.className)
|
2017-06-20 09:15:26 -07:00
|
|
|
.then(() => ({ response: {} }));
|
2018-09-01 13:58:06 -04:00
|
|
|
};
|
2016-02-17 19:00:17 -08:00
|
|
|
|
2016-02-19 23:47:44 -05:00
|
|
|
export class SchemasRouter extends PromiseRouter {
|
|
|
|
|
mountRoutes() {
|
2018-09-01 13:58:06 -04: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
|
|
|
}
|
|
|
|
|
}
|