Skip to content

Commit d478e00

Browse files
authored
PG: Support multiple global config (#5242)
* PG: Support Multiple Configs * rename test * refactor
1 parent 8c419ec commit d478e00

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

spec/ParseQuery.spec.js

+49
Original file line numberDiff line numberDiff line change
@@ -4517,4 +4517,53 @@ describe('Parse.Query testing', () => {
45174517
.then(done.fail)
45184518
.catch(() => done());
45194519
});
4520+
4521+
it('can add new config to existing config', async () => {
4522+
await request({
4523+
method: 'PUT',
4524+
url: 'http://localhost:8378/1/config',
4525+
json: true,
4526+
body: {
4527+
params: {
4528+
files: [{ __type: 'File', name: 'name', url: 'http://url' }],
4529+
},
4530+
},
4531+
headers: masterKeyHeaders,
4532+
});
4533+
4534+
await request({
4535+
method: 'PUT',
4536+
url: 'http://localhost:8378/1/config',
4537+
json: true,
4538+
body: {
4539+
params: { newConfig: 'good' },
4540+
},
4541+
headers: masterKeyHeaders,
4542+
});
4543+
4544+
const result = await Parse.Config.get();
4545+
equal(result.get('files')[0].toJSON(), {
4546+
__type: 'File',
4547+
name: 'name',
4548+
url: 'http://url',
4549+
});
4550+
equal(result.get('newConfig'), 'good');
4551+
});
4552+
4553+
it('can set object type key', async () => {
4554+
const data = { bar: true, baz: 100 };
4555+
const object = new TestObject();
4556+
object.set('objectField', data);
4557+
await object.save();
4558+
4559+
const query = new Parse.Query(TestObject);
4560+
let result = await query.get(object.id);
4561+
equal(result.get('objectField'), data);
4562+
4563+
object.set('objectField.baz', 50, { ignoreValidation: true });
4564+
await object.save();
4565+
4566+
result = await query.get(object.id);
4567+
equal(result.get('objectField'), { bar: true, baz: 50 });
4568+
});
45204569
});

src/Adapters/Storage/Postgres/PostgresStorageAdapter.js

+20-2
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,7 @@ export class PostgresStorageAdapter implements StorageAdapter {
969969
const qs = `CREATE TABLE IF NOT EXISTS $1:name (${patternsArray.join()})`;
970970
const values = [className, ...valuesArray];
971971

972+
debug(qs, values);
972973
return conn.task('create-table', function*(t) {
973974
try {
974975
yield self._ensureSchemaCollectionExists(t);
@@ -1426,6 +1427,18 @@ export class PostgresStorageAdapter implements StorageAdapter {
14261427
schema = toPostgresSchema(schema);
14271428

14281429
const originalUpdate = { ...update };
1430+
1431+
// Set flag for dot notation fields
1432+
const dotNotationOptions = {};
1433+
Object.keys(update).forEach(fieldName => {
1434+
if (fieldName.indexOf('.') > -1) {
1435+
const components = fieldName.split('.');
1436+
const first = components.shift();
1437+
dotNotationOptions[first] = true;
1438+
} else {
1439+
dotNotationOptions[fieldName] = false;
1440+
}
1441+
});
14291442
update = handleDotFields(update);
14301443
// Resolve authData first,
14311444
// So we don't end up with multiple key updates
@@ -1615,13 +1628,18 @@ export class PostgresStorageAdapter implements StorageAdapter {
16151628
},
16161629
''
16171630
);
1631+
// Override Object
1632+
let updateObject = "'{}'::jsonb";
16181633

1634+
if (dotNotationOptions[fieldName]) {
1635+
// Merge Object
1636+
updateObject = `COALESCE($${index}:name, '{}'::jsonb)`;
1637+
}
16191638
updatePatterns.push(
1620-
`$${index}:name = ('{}'::jsonb ${deletePatterns} ${incrementPatterns} || $${index +
1639+
`$${index}:name = (${updateObject} ${deletePatterns} ${incrementPatterns} || $${index +
16211640
1 +
16221641
keysToDelete.length}::jsonb )`
16231642
);
1624-
16251643
values.push(fieldName, ...keysToDelete, JSON.stringify(fieldValue));
16261644
index += 2 + keysToDelete.length;
16271645
} else if (

0 commit comments

Comments
 (0)