Skip to content

Commit cf3a872

Browse files
jaeggerrflovilmart
authored andcommitted
Don't merge JSON fields after save() when using Postgres to keep same behaviour as MongoDB (#4808) (#4815)
1 parent 1afc34e commit cf3a872

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

spec/ParseObject.spec.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1980,4 +1980,31 @@ describe('Parse.Object testing', () => {
19801980
done();
19811981
})
19821982
});
1983+
1984+
it ('Update object field should store exactly same sent object', async (done) => {
1985+
let object = new TestObject();
1986+
1987+
// Set initial data
1988+
object.set("jsonData", { a: "b" });
1989+
object = await object.save();
1990+
equal(object.get('jsonData'), { a: "b" });
1991+
1992+
// Set empty JSON
1993+
object.set("jsonData", {});
1994+
object = await object.save();
1995+
equal(object.get('jsonData'), {});
1996+
1997+
// Set new JSON data
1998+
object.unset('jsonData')
1999+
object.set("jsonData", { c: "d" });
2000+
object = await object.save();
2001+
equal(object.get('jsonData'), { c: "d" });
2002+
2003+
// Fetch object from server
2004+
object = await object.fetch()
2005+
console.log(object.id, object.get('jsonData'))
2006+
equal(object.get('jsonData'), { c: "d" });
2007+
2008+
done();
2009+
});
19832010
});

src/Adapters/Storage/Postgres/PostgresStorageAdapter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1324,7 +1324,7 @@ export class PostgresStorageAdapter implements StorageAdapter {
13241324
return p + ` - '$${index + 1 + i}:value'`;
13251325
}, '');
13261326

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

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

0 commit comments

Comments
 (0)