Skip to content

Commit 477e978

Browse files
committed
Merge pull request #901 from ParsePlatform/nlutsenko.config
Migrate ParseGlobalConfig to new database storage API.
2 parents f1f9bde + fa69541 commit 477e978

File tree

3 files changed

+45
-51
lines changed

3 files changed

+45
-51
lines changed

Diff for: spec/ParseGlobalConfig.spec.js

+27-27
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@ var Parse = require('parse/node').Parse;
55
let Config = require('../src/Config');
66

77
describe('a GlobalConfig', () => {
8-
beforeEach(function(done) {
8+
beforeEach(function (done) {
99
let config = new Config('test');
10-
config.database.rawCollection('_GlobalConfig')
11-
.then(coll => coll.updateOne({ '_id': 1}, { $set: { params: { companies: ['US', 'DK'] } } }, { upsert: true }))
10+
config.database.adaptiveCollection('_GlobalConfig')
11+
.then(coll => coll.upsertOne({ '_id': 1 }, { $set: { params: { companies: ['US', 'DK'] } } }))
1212
.then(done());
1313
});
1414

1515
it('can be retrieved', (done) => {
1616
request.get({
17-
url: 'http://localhost:8378/1/config',
18-
json: true,
17+
url : 'http://localhost:8378/1/config',
18+
json : true,
1919
headers: {
2020
'X-Parse-Application-Id': 'test',
21-
'X-Parse-Master-Key': 'test',
22-
},
21+
'X-Parse-Master-Key' : 'test'
22+
}
2323
}, (error, response, body) => {
2424
expect(response.statusCode).toEqual(200);
2525
expect(body.params.companies).toEqual(['US', 'DK']);
@@ -29,13 +29,13 @@ describe('a GlobalConfig', () => {
2929

3030
it('can be updated when a master key exists', (done) => {
3131
request.put({
32-
url: 'http://localhost:8378/1/config',
33-
json: true,
34-
body: { params: { companies: ['US', 'DK', 'SE'] } },
32+
url : 'http://localhost:8378/1/config',
33+
json : true,
34+
body : { params: { companies: ['US', 'DK', 'SE'] } },
3535
headers: {
3636
'X-Parse-Application-Id': 'test',
37-
'X-Parse-Master-Key': 'test'
38-
},
37+
'X-Parse-Master-Key' : 'test'
38+
}
3939
}, (error, response, body) => {
4040
expect(response.statusCode).toEqual(200);
4141
expect(body.result).toEqual(true);
@@ -45,35 +45,35 @@ describe('a GlobalConfig', () => {
4545

4646
it('fail to update if master key is missing', (done) => {
4747
request.put({
48-
url: 'http://localhost:8378/1/config',
49-
json: true,
50-
body: { params: { companies: [] } },
48+
url : 'http://localhost:8378/1/config',
49+
json : true,
50+
body : { params: { companies: [] } },
5151
headers: {
5252
'X-Parse-Application-Id': 'test',
53-
'X-Parse-REST-API-Key': 'rest'
54-
},
53+
'X-Parse-REST-API-Key' : 'rest'
54+
}
5555
}, (error, response, body) => {
5656
expect(response.statusCode).toEqual(403);
5757
expect(body.error).toEqual('unauthorized: master key is required');
5858
done();
5959
});
60-
});
60+
});
6161

6262
it('failed getting config when it is missing', (done) => {
6363
let config = new Config('test');
64-
config.database.rawCollection('_GlobalConfig')
65-
.then(coll => coll.deleteOne({ '_id': 1}, {}, {}))
66-
.then(_ => {
64+
config.database.adaptiveCollection('_GlobalConfig')
65+
.then(coll => coll.deleteOne({ '_id': 1 }))
66+
.then(() => {
6767
request.get({
68-
url: 'http://localhost:8378/1/config',
69-
json: true,
68+
url : 'http://localhost:8378/1/config',
69+
json : true,
7070
headers: {
7171
'X-Parse-Application-Id': 'test',
72-
'X-Parse-Master-Key': 'test',
73-
},
72+
'X-Parse-Master-Key' : 'test'
73+
}
7474
}, (error, response, body) => {
75-
expect(response.statusCode).toEqual(404);
76-
expect(body.code).toEqual(Parse.Error.INVALID_KEY_NAME);
75+
expect(response.statusCode).toEqual(200);
76+
expect(body.params).toEqual({});
7777
done();
7878
});
7979
});

Diff for: src/Adapters/Storage/Mongo/MongoCollection.js

+4
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ export default class MongoCollection {
7575
});
7676
}
7777

78+
deleteOne(query) {
79+
return this._mongoCollection.deleteOne(query);
80+
}
81+
7882
remove(query) {
7983
return this._mongoCollection.remove(query);
8084
}

Diff for: src/Routers/GlobalConfigRouter.js

+14-24
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,28 @@
11
// global_config.js
22

3-
var Parse = require('parse/node').Parse;
4-
53
import PromiseRouter from '../PromiseRouter';
64
import * as middleware from "../middlewares";
75

86
export class GlobalConfigRouter extends PromiseRouter {
97
getGlobalConfig(req) {
10-
return req.config.database.rawCollection('_GlobalConfig')
11-
.then(coll => coll.findOne({'_id': 1}))
12-
.then(globalConfig => ({response: { params: globalConfig.params }}))
13-
.catch(() => ({
14-
status: 404,
15-
response: {
16-
code: Parse.Error.INVALID_KEY_NAME,
17-
error: 'config does not exist',
8+
return req.config.database.adaptiveCollection('_GlobalConfig')
9+
.then(coll => coll.find({ '_id': 1 }, { limit: 1 }))
10+
.then(results => {
11+
if (results.length != 1) {
12+
// If there is no config in the database - return empty config.
13+
return { response: { params: {} } };
1814
}
19-
}));
15+
let globalConfig = results[0];
16+
return { response: { params: globalConfig.params } };
17+
});
2018
}
19+
2120
updateGlobalConfig(req) {
22-
return req.config.database.rawCollection('_GlobalConfig')
23-
.then(coll => coll.findOneAndUpdate({ _id: 1 }, { $set: req.body }))
24-
.then(response => {
25-
return { response: { result: true } }
26-
})
27-
.catch(() => ({
28-
status: 404,
29-
response: {
30-
code: Parse.Error.INVALID_KEY_NAME,
31-
error: 'config cannot be updated',
32-
}
33-
}));
21+
return req.config.database.adaptiveCollection('_GlobalConfig')
22+
.then(coll => coll.upsertOne({ _id: 1 }, { $set: req.body }))
23+
.then(() => ({ response: { result: true } }));
3424
}
35-
25+
3626
mountRoutes() {
3727
this.route('GET', '/config', req => { return this.getGlobalConfig(req) });
3828
this.route('PUT', '/config', middleware.promiseEnforceMasterKeyAccess, req => { return this.updateGlobalConfig(req) });

0 commit comments

Comments
 (0)