* Remove adaptiveCollection * Remove an adaptiveCollection use * Remove an adaptiveCollection * make adaptiveCollection private * Remove collection from mongoadapter * Move schema collection usage into mongo adapter * stop relying on mongo format for removing join tables * reduce usage of schemaCollection * remove uses of _collection * Move CLP setting into mongo adapter * remove all uses of schemaCollection * make schemaCollection private * remove transform from schemaCollection * rename some stuff * Tweak paramaters and stuff * reorder some params * reorder find() arguments * finishsh touching up argument order * Accept a database adapter as a parameter * First passing test with postgres! * Actually use the provided className * index on unique-indexes: c454180 Revert "Log objects rather than JSON stringified objects (#1922)" * Start dealing with test shittyness * Make specific server config for tests async * Fix email validation * Fix broken cloud code * Save callback to variable * undo * Fix tests * Setup travis * fix travis maybe * try removing db user * indentation? * remove postgres version setting * sudo maybe? * use postgres username * fix check for _PushStatus * excludes * remove db=mongo * allow postgres to fail * Fix allow failure * postgres 9.4 * Remove mongo implementations and fix test * Fix test leaving behind connections
85 lines
3.1 KiB
JavaScript
85 lines
3.1 KiB
JavaScript
// schemas.js
|
|
|
|
var express = require('express'),
|
|
Parse = require('parse/node').Parse,
|
|
SchemaController = require('../Controllers/SchemaController');
|
|
|
|
import PromiseRouter from '../PromiseRouter';
|
|
import * as middleware from "../middlewares";
|
|
|
|
function classNameMismatchResponse(bodyClass, pathClass) {
|
|
throw new Parse.Error(
|
|
Parse.Error.INVALID_CLASS_NAME,
|
|
`Class name mismatch between ${bodyClass} and ${pathClass}.`
|
|
);
|
|
}
|
|
|
|
function getAllSchemas(req) {
|
|
return req.config.database.loadSchema()
|
|
.then(schemaController => schemaController.getAllClasses())
|
|
.then(schemas => ({ response: { results: schemas } }));
|
|
}
|
|
|
|
function getOneSchema(req) {
|
|
const className = req.params.className;
|
|
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) {
|
|
if (req.params.className && req.body.className) {
|
|
if (req.params.className != req.body.className) {
|
|
return classNameMismatchResponse(req.body.className, req.params.className);
|
|
}
|
|
}
|
|
|
|
const className = req.params.className || req.body.className;
|
|
if (!className) {
|
|
throw new Parse.Error(135, `POST ${req.path} needs a class name.`);
|
|
}
|
|
|
|
return req.config.database.loadSchema()
|
|
.then(schema => schema.addClassIfNotExists(className, req.body.fields, req.body.classLevelPermissions))
|
|
.then(schema => ({ response: schema }));
|
|
}
|
|
|
|
function modifySchema(req) {
|
|
if (req.body.className && req.body.className != req.params.className) {
|
|
return classNameMismatchResponse(req.body.className, req.params.className);
|
|
}
|
|
|
|
let submittedFields = req.body.fields || {};
|
|
let className = req.params.className;
|
|
|
|
return req.config.database.loadSchema()
|
|
.then(schema => schema.updateClass(className, submittedFields, req.body.classLevelPermissions, req.config.database))
|
|
.then(result => ({response: result}));
|
|
}
|
|
|
|
const deleteSchema = req => {
|
|
if (!SchemaController.classNameIsValid(req.params.className)) {
|
|
throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME, SchemaController.invalidClassNameMessage(req.params.className));
|
|
}
|
|
return req.config.database.deleteSchema(req.params.className)
|
|
.then(() => ({ response: {} }));
|
|
}
|
|
|
|
export class SchemasRouter extends PromiseRouter {
|
|
mountRoutes() {
|
|
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);
|
|
}
|
|
}
|