Returns updated keys when running with beforeSave
This commit is contained in:
@@ -224,6 +224,19 @@ describe('miscellaneous', function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('test beforeSave returns value on create and update', (done) => {
|
||||||
|
var obj = new Parse.Object('BeforeSaveChanged');
|
||||||
|
obj.set('foo', 'bing');
|
||||||
|
obj.save().then(() => {
|
||||||
|
expect(obj.get('foo')).toEqual('baz');
|
||||||
|
obj.set('foo', 'bar');
|
||||||
|
return obj.save().then(() => {
|
||||||
|
expect(obj.get('foo')).toEqual('baz');
|
||||||
|
done();
|
||||||
|
})
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
it('test afterSave ran and created an object', function(done) {
|
it('test afterSave ran and created an object', function(done) {
|
||||||
var obj = new Parse.Object('AfterSaveTest');
|
var obj = new Parse.Object('AfterSaveTest');
|
||||||
obj.save();
|
obj.save();
|
||||||
|
|||||||
@@ -179,21 +179,27 @@ DatabaseController.prototype.update = function(className, query, update, options
|
|||||||
return Promise.reject(new Parse.Error(Parse.Error.OBJECT_NOT_FOUND,
|
return Promise.reject(new Parse.Error(Parse.Error.OBJECT_NOT_FOUND,
|
||||||
'Object not found.'));
|
'Object not found.'));
|
||||||
}
|
}
|
||||||
|
return sanitizeDatabaseResult(originalUpdate, result);
|
||||||
let response = {};
|
|
||||||
Object.keys(originalUpdate).forEach(key => {
|
|
||||||
let keyUpdate = originalUpdate[key];
|
|
||||||
// determine if that was an op
|
|
||||||
if (keyUpdate && typeof keyUpdate === 'object' && keyUpdate.__op
|
|
||||||
&& ['Add', 'AddUnique', 'Remove', 'Increment'].indexOf(keyUpdate.__op) > -1) {
|
|
||||||
// only valid ops that produce an actionable result
|
|
||||||
response[key] = result[key];
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return response;
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function sanitizeDatabaseResult(originalObject, result) {
|
||||||
|
let response = {};
|
||||||
|
if (!result) {
|
||||||
|
return Promise.resolve(response);
|
||||||
|
}
|
||||||
|
Object.keys(originalObject).forEach(key => {
|
||||||
|
let keyUpdate = originalObject[key];
|
||||||
|
// determine if that was an op
|
||||||
|
if (keyUpdate && typeof keyUpdate === 'object' && keyUpdate.__op
|
||||||
|
&& ['Add', 'AddUnique', 'Remove', 'Increment'].indexOf(keyUpdate.__op) > -1) {
|
||||||
|
// only valid ops that produce an actionable result
|
||||||
|
response[key] = result[key];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return Promise.resolve(response);
|
||||||
|
}
|
||||||
|
|
||||||
// Processes relation-updating operations from a REST-format update.
|
// Processes relation-updating operations from a REST-format update.
|
||||||
// Returns a promise that resolves successfully when these are
|
// Returns a promise that resolves successfully when these are
|
||||||
// processed.
|
// processed.
|
||||||
@@ -318,6 +324,7 @@ DatabaseController.prototype.destroy = function(className, query, options = {})
|
|||||||
// Returns a promise that resolves successfully iff the object saved.
|
// Returns a promise that resolves successfully iff the object saved.
|
||||||
DatabaseController.prototype.create = function(className, object, options) {
|
DatabaseController.prototype.create = function(className, object, options) {
|
||||||
// Make a copy of the object, so we don't mutate the incoming data.
|
// Make a copy of the object, so we don't mutate the incoming data.
|
||||||
|
let originalObject = object;
|
||||||
object = deepcopy(object);
|
object = deepcopy(object);
|
||||||
|
|
||||||
var schema;
|
var schema;
|
||||||
@@ -338,6 +345,9 @@ DatabaseController.prototype.create = function(className, object, options) {
|
|||||||
.then(coll => {
|
.then(coll => {
|
||||||
var mongoObject = transform.transformCreate(schema, className, object);
|
var mongoObject = transform.transformCreate(schema, className, object);
|
||||||
return coll.insertOne(mongoObject);
|
return coll.insertOne(mongoObject);
|
||||||
|
})
|
||||||
|
.then(result => {
|
||||||
|
return sanitizeDatabaseResult(originalObject, result.ops[0]);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -711,6 +711,12 @@ RestWrite.prototype.runDatabaseOperation = function() {
|
|||||||
return this.config.database.update(
|
return this.config.database.update(
|
||||||
this.className, this.query, this.data, this.runOptions).then((resp) => {
|
this.className, this.query, this.data, this.runOptions).then((resp) => {
|
||||||
resp.updatedAt = this.updatedAt;
|
resp.updatedAt = this.updatedAt;
|
||||||
|
if (this.storage['changedByTrigger']) {
|
||||||
|
resp = Object.keys(this.data).reduce((memo, key) => {
|
||||||
|
memo[key] = resp[key] || this.data[key];
|
||||||
|
return memo;
|
||||||
|
}, resp);
|
||||||
|
}
|
||||||
this.response = {
|
this.response = {
|
||||||
response: resp
|
response: resp
|
||||||
};
|
};
|
||||||
@@ -725,13 +731,16 @@ RestWrite.prototype.runDatabaseOperation = function() {
|
|||||||
}
|
}
|
||||||
// Run a create
|
// Run a create
|
||||||
return this.config.database.create(this.className, this.data, this.runOptions)
|
return this.config.database.create(this.className, this.data, this.runOptions)
|
||||||
.then(() => {
|
.then((resp) => {
|
||||||
var resp = {
|
Object.assign(resp, {
|
||||||
objectId: this.data.objectId,
|
objectId: this.data.objectId,
|
||||||
createdAt: this.data.createdAt
|
createdAt: this.data.createdAt
|
||||||
};
|
});
|
||||||
if (this.storage['changedByTrigger']) {
|
if (this.storage['changedByTrigger']) {
|
||||||
Object.assign(resp, this.data);
|
resp = Object.keys(this.data).reduce((memo, key) => {
|
||||||
|
memo[key] = resp[key] || this.data[key];
|
||||||
|
return memo;
|
||||||
|
}, resp);
|
||||||
}
|
}
|
||||||
if (this.storage['token']) {
|
if (this.storage['token']) {
|
||||||
resp.sessionToken = this.storage['token'];
|
resp.sessionToken = this.storage['token'];
|
||||||
|
|||||||
Reference in New Issue
Block a user