Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make GlobalConfig work like parse.com #1210

Merged
merged 4 commits into from
Mar 27, 2016
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions src/Routers/GlobalConfigRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ export class GlobalConfigRouter extends PromiseRouter {
}

updateGlobalConfig(req) {
const params = req.body.params;
const update = {};
Object.keys(params).forEach((key) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use reduce instead of out of block scope assignment? Initial value with {$set:{}, $unset:{}}?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, but mongo is complaining if $set or $unset are empty objects

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, so empty initial. I prefer avoiding assigning on out of blocks objects

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];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use ES2015 string templates 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() {
Expand Down