Skip to content

Commit 902666a

Browse files
committed
feat: allow updating Parse Server options dynamically
1 parent e9ab6b0 commit 902666a

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

spec/CloudCode.spec.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,12 @@ describe('Cloud Code', () => {
9292
});
9393
});
9494

95-
it('can get config', () => {
96-
const config = Parse.Server;
95+
it('can get and set config', () => {
9796
let currentConfig = Config.get('test');
98-
expect(Object.keys(config)).toEqual(Object.keys(currentConfig));
99-
config.silent = false;
100-
Parse.Server = config;
97+
expect(Object.keys(Parse.Server)).toEqual(Object.keys(currentConfig));
98+
Parse.Server.setSilent(['abc']);
10199
currentConfig = Config.get('test');
102-
expect(currentConfig.silent).toBeFalse();
100+
expect(currentConfig.silent).toEqual(['abc']);
103101
});
104102

105103
it('show warning on duplicate cloud functions', done => {

src/ParseServer.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import { SecurityRouter } from './Routers/SecurityRouter';
4545
import CheckRunner from './Security/CheckRunner';
4646
import Deprecator from './Deprecator/Deprecator';
4747
import { DefinedSchemas } from './SchemaMigrations/DefinedSchemas';
48+
import { ParseServerOptions as ParseServerDefintions } from './Options/Definitions.js';
4849

4950
// Mutate the Parse object to add the Cloud Code handlers
5051
addParseCloud();
@@ -440,13 +441,24 @@ function addParseCloud() {
440441
const ParseCloud = require('./cloud-code/Parse.Cloud');
441442
Object.defineProperty(Parse, 'Server', {
442443
get() {
443-
return Config.get(Parse.applicationId);
444-
},
445-
set(newVal) {
446-
newVal.appId = Parse.applicationId;
447-
Config.put(newVal);
444+
const target = Config.get(Parse.applicationId);
445+
const handler2 = {
446+
get(obj, prop) {
447+
if (prop.substring(0, 3) === 'set') {
448+
const method = `${prop.charAt(3).toLowerCase()}${prop.substring(4, prop.length)}`;
449+
if (!ParseServerDefintions[method]) {
450+
throw `${method} is not a valid Parse Server option`;
451+
}
452+
return value => {
453+
obj[method] = value;
454+
Config.put(obj);
455+
};
456+
}
457+
return obj[prop];
458+
},
459+
};
460+
return new Proxy(target, handler2);
448461
},
449-
configurable: true,
450462
});
451463
Object.assign(Parse.Cloud, ParseCloud);
452464
global.Parse = Parse;

0 commit comments

Comments
 (0)