Skip to content

Commit

Permalink
Don't merge JSON fields after save() when using Postgres to keep same…
Browse files Browse the repository at this point in the history
… behaviour as MongoDB (#4808) (#4815)
  • Loading branch information
jaeggerr authored and flovilmart committed Aug 12, 2018
1 parent 108c7e9 commit 923e2b8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
27 changes: 27 additions & 0 deletions spec/ParseObject.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1980,4 +1980,31 @@ describe('Parse.Object testing', () => {
done();
})
});

it ('Update object field should store exactly same sent object', async (done) => {
let object = new TestObject();

// Set initial data
object.set("jsonData", { a: "b" });
object = await object.save();
equal(object.get('jsonData'), { a: "b" });

// Set empty JSON
object.set("jsonData", {});
object = await object.save();
equal(object.get('jsonData'), {});

// Set new JSON data
object.unset('jsonData')
object.set("jsonData", { c: "d" });
object = await object.save();
equal(object.get('jsonData'), { c: "d" });

// Fetch object from server
object = await object.fetch()
console.log(object.id, object.get('jsonData'))
equal(object.get('jsonData'), { c: "d" });

done();
});
});
2 changes: 1 addition & 1 deletion src/Adapters/Storage/Postgres/PostgresStorageAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -1324,7 +1324,7 @@ export class PostgresStorageAdapter implements StorageAdapter {
return p + ` - '$${index + 1 + i}:value'`;
}, '');

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

values.push(fieldName, ...keysToDelete, JSON.stringify(fieldValue));
index += 2 + keysToDelete.length;
Expand Down

0 comments on commit 923e2b8

Please sign in to comment.