Skip to content

Commit c43c735

Browse files
committed
Adds support for GlobalConfig
1 parent 066ac35 commit c43c735

File tree

3 files changed

+51
-13
lines changed

3 files changed

+51
-13
lines changed

spec/ParseGlobalConfig.spec.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@ describe('a GlobalConfig', () => {
99
let config = new Config('test');
1010
config.database.adapter.upsertOneObject(
1111
'_GlobalConfig',
12-
{ fields: {} },
12+
{ fields: { objectId: { type: 'Number' }, params: {type: 'Object'}} },
1313
{ objectId: 1 },
1414
{ params: { companies: ['US', 'DK'] } }
15-
).then(done, done);
15+
).then(done, (err) => {
16+
jfail(err);
17+
done();
18+
});
1619
});
1720

18-
it_exclude_dbs(['postgres'])('can be retrieved', (done) => {
21+
it('can be retrieved', (done) => {
1922
request.get({
2023
url : 'http://localhost:8378/1/config',
2124
json : true,
@@ -32,7 +35,7 @@ describe('a GlobalConfig', () => {
3235
});
3336
});
3437

35-
it_exclude_dbs(['postgres'])('can be updated when a master key exists', (done) => {
38+
it('can be updated when a master key exists', (done) => {
3639
request.put({
3740
url : 'http://localhost:8378/1/config',
3841
json : true,
@@ -48,7 +51,7 @@ describe('a GlobalConfig', () => {
4851
});
4952
});
5053

51-
it_exclude_dbs(['postgres'])('properly handles delete op', (done) => {
54+
it('properly handles delete op', (done) => {
5255
request.put({
5356
url : 'http://localhost:8378/1/config',
5457
json : true,
@@ -79,7 +82,7 @@ describe('a GlobalConfig', () => {
7982
});
8083
});
8184

82-
it_exclude_dbs(['postgres'])('fail to update if master key is missing', (done) => {
85+
it('fail to update if master key is missing', (done) => {
8386
request.put({
8487
url : 'http://localhost:8378/1/config',
8588
json : true,
@@ -95,7 +98,7 @@ describe('a GlobalConfig', () => {
9598
});
9699
});
97100

98-
it_exclude_dbs(['postgres'])('failed getting config when it is missing', (done) => {
101+
it('failed getting config when it is missing', (done) => {
99102
let config = new Config('test');
100103
config.database.adapter.deleteObjectsByQuery(
101104
'_GlobalConfig',

src/Adapters/Storage/Postgres/PostgresStorageAdapter.js

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,31 @@ const toPostgresSchema = (schema) => {
110110
return schema;
111111
}
112112

113+
const handleDotFields = (object) => {
114+
Object.keys(object).forEach(fieldName => {
115+
if (fieldName.indexOf('.') > -1) {
116+
let components = fieldName.split('.');
117+
let first = components.shift();
118+
object[first] = object[first] || {};
119+
let currentObj = object[first];
120+
let next;
121+
let value = object[fieldName];
122+
if (value && value.__op === 'Delete') {
123+
value = undefined;
124+
}
125+
while(next = components.shift()) {
126+
currentObj[next] = currentObj[next] || {};
127+
if (components.length === 0) {
128+
currentObj[next] = value;
129+
}
130+
currentObj = currentObj[next];
131+
}
132+
delete object[fieldName];
133+
}
134+
});
135+
return object;
136+
}
137+
113138
// Returns the list of join tables on a schema
114139
const joinTablesForSchema = (schema) => {
115140
let list = [];
@@ -601,6 +626,9 @@ export class PostgresStorageAdapter {
601626
let valuesArray = [];
602627
schema = toPostgresSchema(schema);
603628
let geoPoints = {};
629+
630+
object = handleDotFields(object);
631+
604632
Object.keys(object).forEach(fieldName => {
605633
var authDataMatch = fieldName.match(/^_auth_data_([a-zA-Z0-9_]+)$/);
606634
if (authDataMatch) {
@@ -738,6 +766,8 @@ export class PostgresStorageAdapter {
738766
let values = [className]
739767
let index = 2;
740768
schema = toPostgresSchema(schema);
769+
770+
update = handleDotFields(update);
741771
// Resolve authData first,
742772
// So we don't end up with multiple key updates
743773
for (let fieldName in update) {
@@ -864,10 +894,11 @@ export class PostgresStorageAdapter {
864894
// Hopefully, we can get rid of this. It's only used for config and hooks.
865895
upsertOneObject(className, schema, query, update) {
866896
debug('upsertOneObject', {className, query, update});
867-
return this.createObject(className, schema, update).catch((err) => {
897+
let createValue = Object.assign({}, query, update);
898+
return this.createObject(className, schema, createValue).catch((err) => {
868899
// ignore duplicate value errors as it's upsert
869900
if (err.code == Parse.Error.DUPLICATE_VALUE) {
870-
return;
901+
return this.findOneAndUpdate(className, schema, query, update);
871902
}
872903
throw err;
873904
});
@@ -1019,8 +1050,7 @@ export class PostgresStorageAdapter {
10191050
throw err;
10201051
});
10211052
});
1022-
return Promise.all(promises).then(() => {
1023-
return Promise.all([
1053+
promises = promises.concat([
10241054
this._client.any(json_object_set_key).catch((err) => {
10251055
console.error(err);
10261056
}),
@@ -1040,7 +1070,7 @@ export class PostgresStorageAdapter {
10401070
console.error(err);
10411071
})
10421072
]);
1043-
}).then(() => {
1073+
return Promise.all(promises).then(() => {
10441074
debug(`initialzationDone in ${new Date().getTime() - now}`);
10451075
})
10461076
}

src/Controllers/SchemaController.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ const defaultColumns = Object.freeze({
9292
"className": {type:'String'},
9393
"triggerName": {type:'String'},
9494
"url": {type:'String'}
95+
},
96+
_GlobalConfig: {
97+
"objectId": {type: 'Number'},
98+
"params": {type: 'Object'}
9599
}
96100
});
97101

@@ -265,12 +269,13 @@ const injectDefaultSchema = ({className, fields, classLevelPermissions}) => ({
265269
});
266270

267271
const _HooksSchema = {className: "_Hooks", fields: defaultColumns._Hooks};
272+
const _GlobalConfigSchema = { className: "_GlobalConfig", fields: defaultColumns._GlobalConfig }
268273
const _PushStatusSchema = convertSchemaToAdapterSchema(injectDefaultSchema({
269274
className: "_PushStatus",
270275
fields: {},
271276
classLevelPermissions: {}
272277
}));
273-
const VolatileClassesSchemas = [_HooksSchema, _PushStatusSchema];
278+
const VolatileClassesSchemas = [_HooksSchema, _PushStatusSchema, _GlobalConfigSchema];
274279

275280
const dbTypeMatchesObjectType = (dbType, objectType) => {
276281
if (dbType.type !== objectType.type) return false;

0 commit comments

Comments
 (0)