Skip to content

Commit e1c4755

Browse files
committed
Returns updated keys when running with beforeSave
1 parent ba545fb commit e1c4755

File tree

3 files changed

+48
-16
lines changed

3 files changed

+48
-16
lines changed

spec/ParseAPI.spec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,19 @@ describe('miscellaneous', function() {
224224
});
225225
});
226226

227+
it('test beforeSave returns value on create and update', (done) => {
228+
var obj = new Parse.Object('BeforeSaveChanged');
229+
obj.set('foo', 'bing');
230+
obj.save().then(() => {
231+
expect(obj.get('foo')).toEqual('baz');
232+
obj.set('foo', 'bar');
233+
return obj.save().then(() => {
234+
expect(obj.get('foo')).toEqual('baz');
235+
done();
236+
})
237+
})
238+
});
239+
227240
it('test afterSave ran and created an object', function(done) {
228241
var obj = new Parse.Object('AfterSaveTest');
229242
obj.save();

src/Controllers/DatabaseController.js

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -179,21 +179,27 @@ DatabaseController.prototype.update = function(className, query, update, options
179179
return Promise.reject(new Parse.Error(Parse.Error.OBJECT_NOT_FOUND,
180180
'Object not found.'));
181181
}
182-
183-
let response = {};
184-
Object.keys(originalUpdate).forEach(key => {
185-
let keyUpdate = originalUpdate[key];
186-
// determine if that was an op
187-
if (keyUpdate && typeof keyUpdate === 'object' && keyUpdate.__op
188-
&& ['Add', 'AddUnique', 'Remove', 'Increment'].indexOf(keyUpdate.__op) > -1) {
189-
// only valid ops that produce an actionable result
190-
response[key] = result[key];
191-
}
192-
});
193-
return response;
182+
return sanitizeDatabaseResult(originalUpdate, result);
194183
});
195184
};
196185

186+
function sanitizeDatabaseResult(originalObject, result) {
187+
let response = {};
188+
if (!result) {
189+
return Promise.resolve(response);
190+
}
191+
Object.keys(originalObject).forEach(key => {
192+
let keyUpdate = originalObject[key];
193+
// determine if that was an op
194+
if (keyUpdate && typeof keyUpdate === 'object' && keyUpdate.__op
195+
&& ['Add', 'AddUnique', 'Remove', 'Increment'].indexOf(keyUpdate.__op) > -1) {
196+
// only valid ops that produce an actionable result
197+
response[key] = result[key];
198+
}
199+
});
200+
return Promise.resolve(response);
201+
}
202+
197203
// Processes relation-updating operations from a REST-format update.
198204
// Returns a promise that resolves successfully when these are
199205
// processed.
@@ -318,6 +324,7 @@ DatabaseController.prototype.destroy = function(className, query, options = {})
318324
// Returns a promise that resolves successfully iff the object saved.
319325
DatabaseController.prototype.create = function(className, object, options) {
320326
// Make a copy of the object, so we don't mutate the incoming data.
327+
let originalObject = object;
321328
object = deepcopy(object);
322329

323330
var schema;
@@ -338,6 +345,9 @@ DatabaseController.prototype.create = function(className, object, options) {
338345
.then(coll => {
339346
var mongoObject = transform.transformCreate(schema, className, object);
340347
return coll.insertOne(mongoObject);
348+
})
349+
.then(result => {
350+
return sanitizeDatabaseResult(originalObject, result.ops[0]);
341351
});
342352
};
343353

src/RestWrite.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,12 @@ RestWrite.prototype.runDatabaseOperation = function() {
711711
return this.config.database.update(
712712
this.className, this.query, this.data, this.runOptions).then((resp) => {
713713
resp.updatedAt = this.updatedAt;
714+
if (this.storage['changedByTrigger']) {
715+
resp = Object.keys(this.data).reduce((memo, key) => {
716+
memo[key] = resp[key] || this.data[key];
717+
return memo;
718+
}, resp);
719+
}
714720
this.response = {
715721
response: resp
716722
};
@@ -725,13 +731,16 @@ RestWrite.prototype.runDatabaseOperation = function() {
725731
}
726732
// Run a create
727733
return this.config.database.create(this.className, this.data, this.runOptions)
728-
.then(() => {
729-
var resp = {
734+
.then((resp) => {
735+
Object.assign(resp, {
730736
objectId: this.data.objectId,
731737
createdAt: this.data.createdAt
732-
};
738+
});
733739
if (this.storage['changedByTrigger']) {
734-
Object.assign(resp, this.data);
740+
resp = Object.keys(this.data).reduce((memo, key) => {
741+
memo[key] = resp[key] || this.data[key];
742+
return memo;
743+
}, resp);
735744
}
736745
if (this.storage['token']) {
737746
resp.sessionToken = this.storage['token'];

0 commit comments

Comments
 (0)