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 })
|
2022-01-13 03:04:49 +01:00
|
|
|
.then(schemaController => schemaController.getAllClasses({ clearCache: true }))
|
2020-07-13 17:13:08 -05:00
|
|
|
.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) {
|
2020-10-25 15:06:58 -05:00
|
|
|
throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME, `Class ${className} does not exist.`);
|
2017-06-20 09:15:26 -07:00
|
|
|
} else {
|
2020-10-25 15:06:58 -05: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
|
|
|
}
|
|
|
|
|
|
2021-11-01 09:28:49 -04:00
|
|
|
const checkIfDefinedSchemasIsUsed = req => {
|
|
|
|
|
if (req.config?.schema?.lockSchemas === true) {
|
|
|
|
|
throw new Parse.Error(
|
|
|
|
|
Parse.Error.OPERATION_FORBIDDEN,
|
|
|
|
|
'Cannot perform this operation when schemas options is used.'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const internalCreateSchema = async (className, body, config) => {
|
|
|
|
|
const controller = await config.database.loadSchema({ clearCache: true });
|
|
|
|
|
const response = await controller.addClassIfNotExists(
|
|
|
|
|
className,
|
|
|
|
|
body.fields,
|
|
|
|
|
body.classLevelPermissions,
|
|
|
|
|
body.indexes
|
|
|
|
|
);
|
|
|
|
|
return {
|
|
|
|
|
response,
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const internalUpdateSchema = async (className, body, config) => {
|
|
|
|
|
const controller = await config.database.loadSchema({ clearCache: true });
|
|
|
|
|
const response = await controller.updateClass(
|
|
|
|
|
className,
|
|
|
|
|
body.fields || {},
|
|
|
|
|
body.classLevelPermissions,
|
|
|
|
|
body.indexes,
|
|
|
|
|
config.database
|
|
|
|
|
);
|
|
|
|
|
return { response };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
async function createSchema(req) {
|
|
|
|
|
checkIfDefinedSchemasIsUsed(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) {
|
2020-10-25 15:06:58 -05: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
|
|
|
|
2021-11-01 09:28:49 -04:00
|
|
|
return await internalCreateSchema(className, req.body, req.config);
|
2016-02-05 14:56:11 -08:00
|
|
|
}
|
|
|
|
|
|
2016-02-09 11:26:46 -08:00
|
|
|
function modifySchema(req) {
|
2021-11-01 09:28:49 -04:00
|
|
|
checkIfDefinedSchemasIsUsed(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 className = req.params.className;
|
2016-02-09 11:26:46 -08:00
|
|
|
|
2021-11-01 09:28:49 -04:00
|
|
|
return internalUpdateSchema(className, req.body, req.config);
|
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
|
|
|
}
|
2020-10-25 15:06:58 -05:00
|
|
|
return req.config.database.deleteSchema(req.params.className).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() {
|
2020-10-25 15:06:58 -05:00
|
|
|
this.route('GET', '/schemas', middleware.promiseEnforceMasterKeyAccess, getAllSchemas);
|
2018-09-01 13:58:06 -04:00
|
|
|
this.route(
|
|
|
|
|
'GET',
|
|
|
|
|
'/schemas/:className',
|
|
|
|
|
middleware.promiseEnforceMasterKeyAccess,
|
|
|
|
|
getOneSchema
|
|
|
|
|
);
|
2020-10-25 15:06:58 -05:00
|
|
|
this.route('POST', '/schemas', middleware.promiseEnforceMasterKeyAccess, createSchema);
|
2018-09-01 13:58:06 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|