Skip to content

fix: Don't send keys that are unchanged to database #7590

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

Closed
wants to merge 2 commits into from
Closed
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
40 changes: 40 additions & 0 deletions spec/CloudCode.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3147,6 +3147,46 @@ describe('afterLogin hook', () => {
const query = new Parse.Query(TestObject);
await query.find({ context: { a: 'a' } });
});

it('unchanged keys are not marked as dirty', async () => {
Parse.Cloud.beforeSave('TestObject', ({ object }) => {
if (!object.existed()) {
object.set('admin', true);
return object;
}
expect(obj.dirtyKeys()).toEqual([]);
expect(obj.dirty('admin')).toBeFalse();
if (object.dirty('admin')) {
throw 'you cannot modify admin';
}
});
const obj = new TestObject({ foo: 'bar' });
await obj.save();
expect(obj.get('admin')).toBeTrue();
const savedObj = await new Parse.Query(TestObject).first();
expect(savedObj.get('admin')).toBeTrue();
savedObj.set('admin', true);
await savedObj.save();
});

it('changed keys are marked as dirty', async () => {
Parse.Cloud.beforeSave('TestObject', ({ object }) => {
if (!object.existed()) {
object.set('admin', false);
return object;
}
expect(object.dirtyKeys()).toEqual(['admin']);
expect(object.dirty('admin')).toBeTrue();
});
const obj = new TestObject({ foo: 'bar' });
await obj.save();
expect(obj.get('admin')).toBeFalse();
const savedObj = await new Parse.Query(TestObject).first();
expect(savedObj.get('admin')).toBeFalse();
expect(savedObj.id).toBe(obj.id);
savedObj.set('admin', true);
await savedObj.save();
});
});

describe('saveFile hooks', () => {
Expand Down
6 changes: 6 additions & 0 deletions src/RestWrite.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,12 @@ RestWrite.prototype.runBeforeSaveTrigger = function () {
return;
}

for (const key in this.data) {
if (_.isEqual(this.data[key], (this.originalData ?? {})[key])) {
delete this.data[key];
}
}

// Avoid doing any setup for triggers if there is no 'beforeSave' trigger for this class.
if (
!triggers.triggerExists(this.className, triggers.Types.beforeSave, this.config.applicationId)
Expand Down