Skip to content

Improve update of jsonb fields. Add PG 9.5 to travis. #2984

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 2 commits into from
Nov 2, 2016
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
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
language: node_js
dist: trusty
sudo: required
node_js:
- '4.5'
- '6.1'
services:
- postgresql
- redis-server
addons:
postgresql: '9.4'
postgresql: '9.5'
before_script:
- ls -al "$HOME/.mongodb/versions"
- psql -c 'create database parse_server_postgres_adapter_test_database;' -U postgres
Expand Down
2 changes: 1 addition & 1 deletion spec/RestCreate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('rest create', () => {
});
});

it_exclude_dbs(['postgres'])('handles object and subdocument', done => {
it('handles object and subdocument', done => {
let obj = { subdoc: {foo: 'bar', wu: 'tan'} };
rest.create(config, auth.nobody(config), 'MyClass', obj)
.then(() => database.adapter.find('MyClass', { fields: {} }, {}, {}))
Expand Down
17 changes: 14 additions & 3 deletions src/Adapters/Storage/Postgres/PostgresStorageAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,7 @@ export class PostgresStorageAdapter {
let index = 2;
schema = toPostgresSchema(schema);

const originalUpdate = {...update};
update = handleDotFields(update);
// Resolve authData first,
// So we don't end up with multiple key updates
Expand Down Expand Up @@ -914,9 +915,19 @@ export class PostgresStorageAdapter {
} else if (typeof fieldValue === 'object'
&& schema.fields[fieldName]
&& schema.fields[fieldName].type === 'Object') {
updatePatterns.push(`$${index}:name = $${index + 1}`);
values.push(fieldName, fieldValue);
index += 2;
const keysToDelete = Object.keys(originalUpdate).filter(k => {
// choose top level fields that have a delete operation set
return originalUpdate[k].__op === 'Delete' && k.split('.').length === 2
}).map(k => k.split('.')[1]);

const deletePatterns = keysToDelete.reduce((p, c, i) => {
return p + ` - '$${index + 1 + i}:value'`;
}, '');

updatePatterns.push(`$${index}:name = ( COALESCE($${index}:name, '{}'::jsonb) ${deletePatterns} || $${index + 1 + keysToDelete.length}::jsonb )`);

values.push(fieldName, ...keysToDelete, JSON.stringify(fieldValue));
index += 2 + keysToDelete.length;
} else if (Array.isArray(fieldValue)
&& schema.fields[fieldName]
&& schema.fields[fieldName].type === 'Array') {
Expand Down