PG: Support multiple global config (#5242)

* PG: Support Multiple Configs

* rename test

* refactor
This commit is contained in:
Diamond Lewis
2018-12-14 17:39:07 -06:00
committed by GitHub
parent 8c419ec52a
commit d478e001b5
2 changed files with 69 additions and 2 deletions

View File

@@ -4517,4 +4517,53 @@ describe('Parse.Query testing', () => {
.then(done.fail)
.catch(() => done());
});
it('can add new config to existing config', async () => {
await request({
method: 'PUT',
url: 'http://localhost:8378/1/config',
json: true,
body: {
params: {
files: [{ __type: 'File', name: 'name', url: 'http://url' }],
},
},
headers: masterKeyHeaders,
});
await request({
method: 'PUT',
url: 'http://localhost:8378/1/config',
json: true,
body: {
params: { newConfig: 'good' },
},
headers: masterKeyHeaders,
});
const result = await Parse.Config.get();
equal(result.get('files')[0].toJSON(), {
__type: 'File',
name: 'name',
url: 'http://url',
});
equal(result.get('newConfig'), 'good');
});
it('can set object type key', async () => {
const data = { bar: true, baz: 100 };
const object = new TestObject();
object.set('objectField', data);
await object.save();
const query = new Parse.Query(TestObject);
let result = await query.get(object.id);
equal(result.get('objectField'), data);
object.set('objectField.baz', 50, { ignoreValidation: true });
await object.save();
result = await query.get(object.id);
equal(result.get('objectField'), { bar: true, baz: 50 });
});
});

View File

@@ -969,6 +969,7 @@ export class PostgresStorageAdapter implements StorageAdapter {
const qs = `CREATE TABLE IF NOT EXISTS $1:name (${patternsArray.join()})`;
const values = [className, ...valuesArray];
debug(qs, values);
return conn.task('create-table', function*(t) {
try {
yield self._ensureSchemaCollectionExists(t);
@@ -1426,6 +1427,18 @@ export class PostgresStorageAdapter implements StorageAdapter {
schema = toPostgresSchema(schema);
const originalUpdate = { ...update };
// Set flag for dot notation fields
const dotNotationOptions = {};
Object.keys(update).forEach(fieldName => {
if (fieldName.indexOf('.') > -1) {
const components = fieldName.split('.');
const first = components.shift();
dotNotationOptions[first] = true;
} else {
dotNotationOptions[fieldName] = false;
}
});
update = handleDotFields(update);
// Resolve authData first,
// So we don't end up with multiple key updates
@@ -1615,13 +1628,18 @@ export class PostgresStorageAdapter implements StorageAdapter {
},
''
);
// Override Object
let updateObject = "'{}'::jsonb";
if (dotNotationOptions[fieldName]) {
// Merge Object
updateObject = `COALESCE($${index}:name, '{}'::jsonb)`;
}
updatePatterns.push(
`$${index}:name = ('{}'::jsonb ${deletePatterns} ${incrementPatterns} || $${index +
`$${index}:name = (${updateObject} ${deletePatterns} ${incrementPatterns} || $${index +
1 +
keysToDelete.length}::jsonb )`
);
values.push(fieldName, ...keysToDelete, JSON.stringify(fieldValue));
index += 2 + keysToDelete.length;
} else if (