BVB Source Codes

parse-server Show SchemasRouter.js Source code

Return Download parse-server: download SchemasRouter.js Source code - Download parse-server Source code - Type:.js
  1. // schemas.js
  2.  
  3. var Parse = require('parse/node').Parse,
  4.   SchemaController = require('../Controllers/SchemaController');
  5.  
  6. import PromiseRouter   from '../PromiseRouter';
  7. import * as middleware from "../middlewares";
  8.  
  9. function classNameMismatchResponse(bodyClass, pathClass) {
  10.   throw new Parse.Error(
  11.     Parse.Error.INVALID_CLASS_NAME,
  12.     `Class name mismatch between ${bodyClass} and ${pathClass}.`
  13.   );
  14. }
  15.  
  16. function getAllSchemas(req) {
  17.   return req.config.database.loadSchema({ clearCache: true})
  18.   .then(schemaController => schemaController.getAllClasses(true))
  19.   .then(schemas => ({ response: { results: schemas } }));
  20. }
  21.  
  22. function getOneSchema(req) {
  23.   const className = req.params.className;
  24.   return req.config.database.loadSchema({ clearCache: true})
  25.   .then(schemaController => schemaController.getOneSchema(className, true))
  26.   .then(schema => ({ response: schema }))
  27.   .catch(error => {
  28.     if (error === undefined) {
  29.       throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME, `Class ${className} does not exist.`);
  30.     } else {
  31.       throw new Parse.Error(Parse.Error.INTERNAL_SERVER_ERROR, 'Database adapter error.');
  32.     }
  33.   });
  34. }
  35.  
  36. function createSchema(req) {
  37.   if (req.params.className && req.body.className) {
  38.     if (req.params.className != req.body.className) {
  39.       return classNameMismatchResponse(req.body.className, req.params.className);
  40.     }
  41.   }
  42.  
  43.   const className = req.params.className || req.body.className;
  44.   if (!className) {
  45.     throw new Parse.Error(135, `POST ${req.path} needs a class name.`);
  46.   }
  47.  
  48.   return req.config.database.loadSchema({ clearCache: true})
  49.     .then(schema => schema.addClassIfNotExists(className, req.body.fields, req.body.classLevelPermissions))
  50.     .then(schema => ({ response: schema }));
  51. }
  52.  
  53. function modifySchema(req) {
  54.   if (req.body.className && req.body.className != req.params.className) {
  55.     return classNameMismatchResponse(req.body.className, req.params.className);
  56.   }
  57.  
  58.   const submittedFields = req.body.fields || {};
  59.   const className = req.params.className;
  60.  
  61.   return req.config.database.loadSchema({ clearCache: true})
  62.   .then(schema => schema.updateClass(className, submittedFields, req.body.classLevelPermissions, req.config.database))
  63.   .then(result => ({response: result}));
  64. }
  65.  
  66. const deleteSchema = req => {
  67.   if (!SchemaController.classNameIsValid(req.params.className)) {
  68.     throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME, SchemaController.invalidClassNameMessage(req.params.className));
  69.   }
  70.   return req.config.database.deleteSchema(req.params.className)
  71.   .then(() => ({ response: {} }));
  72. }
  73.  
  74. export class SchemasRouter extends PromiseRouter {
  75.   mountRoutes() {
  76.     this.route('GET', '/schemas', middleware.promiseEnforceMasterKeyAccess, getAllSchemas);
  77.     this.route('GET', '/schemas/:className', middleware.promiseEnforceMasterKeyAccess, getOneSchema);
  78.     this.route('POST', '/schemas', middleware.promiseEnforceMasterKeyAccess, createSchema);
  79.     this.route('POST', '/schemas/:className', middleware.promiseEnforceMasterKeyAccess, createSchema);
  80.     this.route('PUT', '/schemas/:className', middleware.promiseEnforceMasterKeyAccess, modifySchema);
  81.     this.route('DELETE', '/schemas/:className', middleware.promiseEnforceMasterKeyAccess, deleteSchema);
  82.   }
  83. }
  84.  
downloadSchemasRouter.js Source code - Download parse-server Source code
Related Source Codes/Software:
react-boilerplate - 2017-06-07
webtorrent - Streaming torrent client for the web ... 2017-06-06
machine-learning-for-software-engineers - A complete daily plan for studying to become a mac... 2017-06-06
upterm - A terminal emulator for the 21st century. 2017-06-06
lottie-android - Render After Effects animations natively on Androi... 2017-06-07
AsyncDisplayKit - Smooth asynchronous user interfaces for iOS apps. ... 2017-06-07
ionicons - The premium icon font for Ionic ... 2017-06-07
storybook - 2017-06-07
prettier - Prettier is an opinionated JavaScript formatter. ... 2017-06-08
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top