From 300c98a7f5fa46d6653a804edd9e71c71b34f3bb Mon Sep 17 00:00:00 2001 From: Diamond Lewis Date: Fri, 14 Dec 2018 15:48:44 -0600 Subject: [PATCH 1/3] PG: Support Multiple Configs --- spec/ParseQuery.spec.js | 49 +++++++++++++++++++ .../Postgres/PostgresStorageAdapter.js | 35 ++++++++++--- 2 files changed, 77 insertions(+), 7 deletions(-) diff --git a/spec/ParseQuery.spec.js b/spec/ParseQuery.spec.js index 2c7325b858..61b5e43b54 100644 --- a/spec/ParseQuery.spec.js +++ b/spec/ParseQuery.spec.js @@ -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('foo', data); + await object.save(); + + const query = new Parse.Query(TestObject); + let result = await query.get(object.id); + equal(result.get('foo'), data); + + object.set('foo.baz', 50, { ignoreValidation: true }); + await object.save(); + + result = await query.get(object.id); + equal(result.get('foo'), { bar: true, baz: 50 }); + }); }); diff --git a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js index 6c6a47020d..067bb80e3a 100644 --- a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js +++ b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js @@ -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,21 @@ export class PostgresStorageAdapter implements StorageAdapter { }, '' ); - - updatePatterns.push( - `$${index}:name = ('{}'::jsonb ${deletePatterns} ${incrementPatterns} || $${index + - 1 + - keysToDelete.length}::jsonb )` - ); - + if (dotNotationOptions[fieldName]) { + // Merge Object + updatePatterns.push( + `$${index}:name = ( COALESCE($${index}:name, '{}'::jsonb) ${deletePatterns} ${incrementPatterns} || $${index + + 1 + + keysToDelete.length}::jsonb )` + ); + } else { + // Override Object + updatePatterns.push( + `$${index}:name = ('{}'::jsonb ${deletePatterns} ${incrementPatterns} || $${index + + 1 + + keysToDelete.length}::jsonb )` + ); + } values.push(fieldName, ...keysToDelete, JSON.stringify(fieldValue)); index += 2 + keysToDelete.length; } else if ( From 96d647d4abec21d6a4c46f0c571ceefd1f69c76d Mon Sep 17 00:00:00 2001 From: Diamond Lewis Date: Fri, 14 Dec 2018 15:55:17 -0600 Subject: [PATCH 2/3] rename test --- spec/ParseQuery.spec.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/ParseQuery.spec.js b/spec/ParseQuery.spec.js index 61b5e43b54..e63c11050f 100644 --- a/spec/ParseQuery.spec.js +++ b/spec/ParseQuery.spec.js @@ -4553,17 +4553,17 @@ describe('Parse.Query testing', () => { it('can set object type key', async () => { const data = { bar: true, baz: 100 }; const object = new TestObject(); - object.set('foo', data); + object.set('objectField', data); await object.save(); const query = new Parse.Query(TestObject); let result = await query.get(object.id); - equal(result.get('foo'), data); + equal(result.get('objectField'), data); - object.set('foo.baz', 50, { ignoreValidation: true }); + object.set('objectField.baz', 50, { ignoreValidation: true }); await object.save(); result = await query.get(object.id); - equal(result.get('foo'), { bar: true, baz: 50 }); + equal(result.get('objectField'), { bar: true, baz: 50 }); }); }); From 1285ea88be0a7e4a7e51591e17414c44748eae3c Mon Sep 17 00:00:00 2001 From: Diamond Lewis Date: Fri, 14 Dec 2018 16:03:01 -0600 Subject: [PATCH 3/3] refactor --- .../Postgres/PostgresStorageAdapter.js | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js index 067bb80e3a..a59dc9272c 100644 --- a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js +++ b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js @@ -1628,21 +1628,18 @@ export class PostgresStorageAdapter implements StorageAdapter { }, '' ); + // Override Object + let updateObject = "'{}'::jsonb"; + if (dotNotationOptions[fieldName]) { // Merge Object - updatePatterns.push( - `$${index}:name = ( COALESCE($${index}:name, '{}'::jsonb) ${deletePatterns} ${incrementPatterns} || $${index + - 1 + - keysToDelete.length}::jsonb )` - ); - } else { - // Override Object - updatePatterns.push( - `$${index}:name = ('{}'::jsonb ${deletePatterns} ${incrementPatterns} || $${index + - 1 + - keysToDelete.length}::jsonb )` - ); + updateObject = `COALESCE($${index}:name, '{}'::jsonb)`; } + updatePatterns.push( + `$${index}:name = (${updateObject} ${deletePatterns} ${incrementPatterns} || $${index + + 1 + + keysToDelete.length}::jsonb )` + ); values.push(fieldName, ...keysToDelete, JSON.stringify(fieldValue)); index += 2 + keysToDelete.length; } else if (