From 114f7d1b58c81c403dc9b82466b98b3907c14fee Mon Sep 17 00:00:00 2001 From: Federico Rampazzo Date: Sat, 26 Mar 2016 11:15:17 +0000 Subject: [PATCH 1/4] Make global config visible from parse-dashboard when it's enabled --- src/Routers/FeaturesRouter.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Routers/FeaturesRouter.js b/src/Routers/FeaturesRouter.js index db9a77d786..48361a3840 100644 --- a/src/Routers/FeaturesRouter.js +++ b/src/Routers/FeaturesRouter.js @@ -2,15 +2,17 @@ import { version } from '../../package.json'; import PromiseRouter from '../PromiseRouter'; import * as middleware from "../middlewares"; +const isGlobalConfigEnabled = !!(process.env.PARSE_EXPERIMENTAL_CONFIG_ENABLED || process.env.TESTING) + export class FeaturesRouter extends PromiseRouter { mountRoutes() { this.route('GET','/serverInfo', middleware.promiseEnforceMasterKeyAccess, req => { const features = { globalConfig: { - create: false, - read: false, - update: false, - delete: false, + create: isGlobalConfigEnabled, + read: isGlobalConfigEnabled, + update: isGlobalConfigEnabled, + delete: isGlobalConfigEnabled, }, hooks: { create: false, From 94b10de7b85b258521386a87fe7b06a7ef5ec693 Mon Sep 17 00:00:00 2001 From: Federico Rampazzo Date: Sun, 27 Mar 2016 00:24:38 +0000 Subject: [PATCH 2/4] Fixed config upsert implementation to handle nested object and __op:Delete --- src/Routers/GlobalConfigRouter.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/Routers/GlobalConfigRouter.js b/src/Routers/GlobalConfigRouter.js index 156cecf694..5d375e3f4b 100644 --- a/src/Routers/GlobalConfigRouter.js +++ b/src/Routers/GlobalConfigRouter.js @@ -19,8 +19,19 @@ export class GlobalConfigRouter extends PromiseRouter { updateGlobalConfig(req) { return req.config.database.adaptiveCollection('_GlobalConfig') - .then(coll => coll.upsertOne({ _id: 1 }, { $set: req.body })) - .then(() => ({ response: { result: true } })); + .then(coll => coll.find({ '_id': 1 }, { limit: 1 })) + .then(results => { + const previousConfig = results && results[0] && results[0].params || {}; + const newConfig = Object.assign({}, previousConfig, req.body.params); + for (var key in newConfig) { + if (newConfig[key] && newConfig[key].__op && newConfig[key].__op === "Delete") { + delete newConfig[key]; + } + } + return req.config.database.adaptiveCollection('_GlobalConfig') + .then(coll => coll.upsertOne({ _id: 1 }, { $set: { params: newConfig } })) + .then(() => ({ response: { result: true } })); + }) } mountRoutes() { From 6b9fd16273a3435b5b5e898fcc9fc69049171595 Mon Sep 17 00:00:00 2001 From: Federico Rampazzo Date: Sun, 27 Mar 2016 03:46:36 +0100 Subject: [PATCH 3/4] Added merging in Config with scoped set --- src/Routers/GlobalConfigRouter.js | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Routers/GlobalConfigRouter.js b/src/Routers/GlobalConfigRouter.js index 5d375e3f4b..fa42570b89 100644 --- a/src/Routers/GlobalConfigRouter.js +++ b/src/Routers/GlobalConfigRouter.js @@ -18,20 +18,20 @@ export class GlobalConfigRouter extends PromiseRouter { } updateGlobalConfig(req) { + const params = req.body.params; + const update = {}; + Object.keys(params).forEach((key) => { + if(params[key] && params[key].__op && params[key].__op === "Delete") { + if (!update.$unset) update.$unset = {}; + update.$unset["params." + key] = ""; + } else { + if (!update.$set) update.$set = {}; + update.$set["params." + key] = params[key]; + } + }); return req.config.database.adaptiveCollection('_GlobalConfig') - .then(coll => coll.find({ '_id': 1 }, { limit: 1 })) - .then(results => { - const previousConfig = results && results[0] && results[0].params || {}; - const newConfig = Object.assign({}, previousConfig, req.body.params); - for (var key in newConfig) { - if (newConfig[key] && newConfig[key].__op && newConfig[key].__op === "Delete") { - delete newConfig[key]; - } - } - return req.config.database.adaptiveCollection('_GlobalConfig') - .then(coll => coll.upsertOne({ _id: 1 }, { $set: { params: newConfig } })) - .then(() => ({ response: { result: true } })); - }) + .then(coll => coll.upsertOne({ _id: 1 }, update)) + .then(() => ({ response: { result: true } })); } mountRoutes() { From 236c7d15c2de8756cbf38571dc208f51ade151cc Mon Sep 17 00:00:00 2001 From: Federico Rampazzo Date: Sun, 27 Mar 2016 04:32:58 +0100 Subject: [PATCH 4/4] Refactor code --- src/Routers/GlobalConfigRouter.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Routers/GlobalConfigRouter.js b/src/Routers/GlobalConfigRouter.js index fa42570b89..8cc2646346 100644 --- a/src/Routers/GlobalConfigRouter.js +++ b/src/Routers/GlobalConfigRouter.js @@ -19,16 +19,16 @@ export class GlobalConfigRouter extends PromiseRouter { updateGlobalConfig(req) { const params = req.body.params; - const update = {}; - Object.keys(params).forEach((key) => { + const update = Object.keys(params).reduce((acc, key) => { if(params[key] && params[key].__op && params[key].__op === "Delete") { - if (!update.$unset) update.$unset = {}; - update.$unset["params." + key] = ""; + if (!acc.$unset) acc.$unset = {}; + acc.$unset[`params.${key}`] = ""; } else { - if (!update.$set) update.$set = {}; - update.$set["params." + key] = params[key]; + if (!acc.$set) acc.$set = {}; + acc.$set[`params.${key}`] = params[key]; } - }); + return acc; + }, {}); return req.config.database.adaptiveCollection('_GlobalConfig') .then(coll => coll.upsertOne({ _id: 1 }, update)) .then(() => ({ response: { result: true } }));