Skip to content
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

[Fix] Columns of type "Object" merging with old value on Postgres #4815

Merged
merged 1 commit into from
Jun 7, 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
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) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using async/await, there’s no need for done as a param and call at the end of the method

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