Skip to content

PG: Support multiple global config #5242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions spec/ParseQuery.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
});
});
22 changes: 20 additions & 2 deletions src/Adapters/Storage/Postgres/PostgresStorageAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 (
Expand Down