Skip to content

Commit ba545fb

Browse files
committed
Returns full modifications on PUT
1 parent d6af77b commit ba545fb

File tree

2 files changed

+55
-6
lines changed

2 files changed

+55
-6
lines changed

spec/ParseAPI.spec.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,50 @@ describe('miscellaneous', function() {
868868
});
869869
});
870870

871+
it('should return the updated fields on PUT', (done) => {
872+
let obj = new Parse.Object('GameScore');
873+
obj.save({a:'hello', c: 1, d: ['1'], e:['1'], f:['1','2']}).then(( ) => {
874+
var headers = {
875+
'Content-Type': 'application/json',
876+
'X-Parse-Application-Id': 'test',
877+
'X-Parse-REST-API-Key': 'rest',
878+
'X-Parse-Installation-Id': 'yolo'
879+
};
880+
request.put({
881+
headers: headers,
882+
url: 'http://localhost:8378/1/classes/GameScore/'+obj.id,
883+
body: JSON.stringify({
884+
a: 'b',
885+
c: {"__op":"Increment","amount":2},
886+
d: {"__op":"Add", objects: ['2']},
887+
e: {"__op":"AddUnique", objects: ['1', '2']},
888+
f: {"__op":"Remove", objects: ['2']},
889+
selfThing: {"__type":"Pointer","className":"GameScore","objectId":obj.id},
890+
})
891+
}, (error, response, body) => {
892+
body = JSON.parse(body);
893+
expect(body.a).toBeUndefined();
894+
expect(body.c).toEqual(3); // 2+1
895+
expect(body.d.length).toBe(2);
896+
expect(body.d.indexOf('1') > -1).toBe(true);
897+
expect(body.d.indexOf('2') > -1).toBe(true);
898+
expect(body.e.length).toBe(2);
899+
expect(body.e.indexOf('1') > -1).toBe(true);
900+
expect(body.e.indexOf('2') > -1).toBe(true);
901+
expect(body.f.length).toBe(1);
902+
expect(body.f.indexOf('1') > -1).toBe(true);
903+
// return nothing on other self
904+
expect(body.selfThing).toBeUndefined();
905+
// updatedAt is always set
906+
expect(body.updatedAt).not.toBeUndefined();
907+
done();
908+
});
909+
}).fail((err) => {
910+
fail('Should not fail');
911+
done();
912+
})
913+
})
914+
871915
it('test cloud function error handling', (done) => {
872916
// Register a function which will fail
873917
Parse.Cloud.define('willFail', (req, res) => {

src/Controllers/DatabaseController.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ DatabaseController.prototype.untransformObject = function(
139139
// one of the provided strings must provide the caller with
140140
// write permissions.
141141
DatabaseController.prototype.update = function(className, query, update, options) {
142+
143+
const originalUpdate = update;
142144
// Make a copy of the object, so we don't mutate the incoming data.
143145
update = deepcopy(update);
144146

@@ -179,12 +181,15 @@ DatabaseController.prototype.update = function(className, query, update, options
179181
}
180182

181183
let response = {};
182-
let inc = mongoUpdate['$inc'];
183-
if (inc) {
184-
Object.keys(inc).forEach(key => {
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
185190
response[key] = result[key];
186-
});
187-
}
191+
}
192+
});
188193
return response;
189194
});
190195
};
@@ -474,7 +479,7 @@ DatabaseController.prototype.reduceRelationKeys = function(className, query) {
474479

475480
DatabaseController.prototype.addInObjectIdsIds = function(ids, query) {
476481
if (typeof query.objectId == 'string') {
477-
// Add equality op as we are sure
482+
// Add equality op as we are sure
478483
// we had a constraint on that one
479484
query.objectId = {'$eq': query.objectId};
480485
}

0 commit comments

Comments
 (0)