Added merging in Config with scoped set

This commit is contained in:
Federico Rampazzo
2016-03-27 03:46:36 +01:00
parent 94b10de7b8
commit 6b9fd16273

View File

@@ -18,20 +18,20 @@ export class GlobalConfigRouter extends PromiseRouter {
} }
updateGlobalConfig(req) { updateGlobalConfig(req) {
return req.config.database.adaptiveCollection('_GlobalConfig') const params = req.body.params;
.then(coll => coll.find({ '_id': 1 }, { limit: 1 })) const update = {};
.then(results => { Object.keys(params).forEach((key) => {
const previousConfig = results && results[0] && results[0].params || {}; if(params[key] && params[key].__op && params[key].__op === "Delete") {
const newConfig = Object.assign({}, previousConfig, req.body.params); if (!update.$unset) update.$unset = {};
for (var key in newConfig) { update.$unset["params." + key] = "";
if (newConfig[key] && newConfig[key].__op && newConfig[key].__op === "Delete") { } else {
delete newConfig[key]; if (!update.$set) update.$set = {};
} update.$set["params." + key] = params[key];
} }
});
return req.config.database.adaptiveCollection('_GlobalConfig') return req.config.database.adaptiveCollection('_GlobalConfig')
.then(coll => coll.upsertOne({ _id: 1 }, { $set: { params: newConfig } })) .then(coll => coll.upsertOne({ _id: 1 }, update))
.then(() => ({ response: { result: true } })); .then(() => ({ response: { result: true } }));
})
} }
mountRoutes() { mountRoutes() {