@@ -81,6 +81,7 @@ function RestWrite(config, auth, className, query, data, originalData, clientSDK
81
81
// Shared SchemaController to be reused to reduce the number of loadSchema() calls per request
82
82
// Once set the schemaData should be immutable
83
83
this . validSchemaController = null ;
84
+ this . pendingOps = { } ;
84
85
}
85
86
86
87
// A convenient method to perform all the steps of processing the
@@ -211,18 +212,11 @@ RestWrite.prototype.runBeforeSaveTrigger = function () {
211
212
return Promise . resolve ( ) ;
212
213
}
213
214
214
- // Cloud code gets a bit of extra data for its objects
215
- var extraData = { className : this . className } ;
216
- if ( this . query && this . query . objectId ) {
217
- extraData . objectId = this . query . objectId ;
218
- }
215
+ const { originalObject, updatedObject } = this . buildParseObjects ( ) ;
219
216
220
- let originalObject = null ;
221
- const updatedObject = this . buildUpdatedObject ( extraData ) ;
222
- if ( this . query && this . query . objectId ) {
223
- // This is an update for existing object.
224
- originalObject = triggers . inflate ( extraData , this . originalData ) ;
225
- }
217
+ const stateController = Parse . CoreManager . getObjectStateController ( ) ;
218
+ const [ pending ] = stateController . getPendingOps ( updatedObject . _getStateIdentifier ( ) ) ;
219
+ this . pendingOps = { ...pending } ;
226
220
227
221
return Promise . resolve ( )
228
222
. then ( ( ) => {
@@ -1517,20 +1511,7 @@ RestWrite.prototype.runAfterSaveTrigger = function () {
1517
1511
return Promise . resolve ( ) ;
1518
1512
}
1519
1513
1520
- var extraData = { className : this . className } ;
1521
- if ( this . query && this . query . objectId ) {
1522
- extraData . objectId = this . query . objectId ;
1523
- }
1524
-
1525
- // Build the original object, we only do this for a update write.
1526
- let originalObject ;
1527
- if ( this . query && this . query . objectId ) {
1528
- originalObject = triggers . inflate ( extraData , this . originalData ) ;
1529
- }
1530
-
1531
- // Build the inflated object, different from beforeSave, originalData is not empty
1532
- // since developers can change data in the beforeSave.
1533
- const updatedObject = this . buildUpdatedObject ( extraData ) ;
1514
+ const { originalObject, updatedObject } = this . buildParseObjects ( ) ;
1534
1515
updatedObject . _handleSaveResponse ( this . response . response , this . response . status || 200 ) ;
1535
1516
1536
1517
this . config . database . loadSchema ( ) . then ( schemaController => {
@@ -1556,7 +1537,7 @@ RestWrite.prototype.runAfterSaveTrigger = function () {
1556
1537
)
1557
1538
. then ( result => {
1558
1539
if ( result && typeof result === 'object' ) {
1559
- this . response . response = result ;
1540
+ this . response . response = this . _updateResponseWithData ( result . _toFullJSON ( ) , this . data ) ;
1560
1541
}
1561
1542
} )
1562
1543
. catch ( function ( err ) {
@@ -1590,7 +1571,13 @@ RestWrite.prototype.sanitizedData = function () {
1590
1571
} ;
1591
1572
1592
1573
// Returns an updated copy of the object
1593
- RestWrite . prototype . buildUpdatedObject = function ( extraData ) {
1574
+ RestWrite . prototype . buildParseObjects = function ( ) {
1575
+ const extraData = { className : this . className , objectId : this . query ?. objectId } ;
1576
+ let originalObject ;
1577
+ if ( this . query && this . query . objectId ) {
1578
+ originalObject = triggers . inflate ( extraData , this . originalData ) ;
1579
+ }
1580
+
1594
1581
const className = Parse . Object . fromJSON ( extraData ) ;
1595
1582
const readOnlyAttributes = className . constructor . readOnlyAttributes
1596
1583
? className . constructor . readOnlyAttributes ( )
@@ -1628,7 +1615,7 @@ RestWrite.prototype.buildUpdatedObject = function (extraData) {
1628
1615
delete sanitized [ attribute ] ;
1629
1616
}
1630
1617
updatedObject . set ( sanitized ) ;
1631
- return updatedObject ;
1618
+ return { updatedObject, originalObject } ;
1632
1619
} ;
1633
1620
1634
1621
RestWrite . prototype . cleanUserAuthData = function ( ) {
@@ -1648,6 +1635,15 @@ RestWrite.prototype.cleanUserAuthData = function () {
1648
1635
} ;
1649
1636
1650
1637
RestWrite . prototype . _updateResponseWithData = function ( response , data ) {
1638
+ const { updatedObject } = this . buildParseObjects ( ) ;
1639
+ const stateController = Parse . CoreManager . getObjectStateController ( ) ;
1640
+ const [ pending ] = stateController . getPendingOps ( updatedObject . _getStateIdentifier ( ) ) ;
1641
+ for ( const key in this . pendingOps ) {
1642
+ if ( ! pending [ key ] ) {
1643
+ data [ key ] = this . originalData ? this . originalData [ key ] : { __op : 'Delete' } ;
1644
+ this . storage . fieldsChangedByTrigger . push ( key ) ;
1645
+ }
1646
+ }
1651
1647
if ( _ . isEmpty ( this . storage . fieldsChangedByTrigger ) ) {
1652
1648
return response ;
1653
1649
}
0 commit comments