@@ -9,7 +9,6 @@ const Auth = require('./Auth');
9
9
const { encodeDate, checkProhibitedKeywords } = require ( './Utils' ) ;
10
10
var cryptoUtils = require ( './cryptoUtils' ) ;
11
11
var passwordCrypto = require ( './password' ) ;
12
- import Parse from 'parse/node' ;
13
12
import ParseError from './ParseError' ;
14
13
var triggers = require ( './triggers' ) ;
15
14
var ClientSDK = require ( './ClientSDK' ) ;
@@ -18,6 +17,7 @@ import RestQuery from './RestQuery';
18
17
import _ from 'lodash' ;
19
18
import logger from './logger' ;
20
19
import { requiredColumns } from './Controllers/SchemaController' ;
20
+ import { loadModule } from './Adapters/AdapterLoader' ;
21
21
22
22
// query and data are both provided in REST API format. So data
23
23
// types are encoded by plain old objects.
@@ -224,7 +224,7 @@ RestWrite.prototype.validateSchema = function () {
224
224
225
225
// Runs any beforeSave triggers against this operation.
226
226
// Any change leads to our data being mutated.
227
- RestWrite . prototype . runBeforeSaveTrigger = function ( ) {
227
+ RestWrite . prototype . runBeforeSaveTrigger = async function ( ) {
228
228
if ( this . response || this . runOptions . many ) {
229
229
return ;
230
230
}
@@ -235,8 +235,8 @@ RestWrite.prototype.runBeforeSaveTrigger = function () {
235
235
) {
236
236
return Promise . resolve ( ) ;
237
237
}
238
-
239
- const { originalObject, updatedObject } = this . buildParseObjects ( ) ;
238
+ const Parse = await loadModule ( 'parse/node.js' ) ;
239
+ const { originalObject, updatedObject } = this . buildParseObjects ( Parse ) ;
240
240
const identifier = updatedObject . _getStateIdentifier ( ) ;
241
241
const stateController = Parse . CoreManager . getObjectStateController ( ) ;
242
242
const [ pending ] = stateController . getPendingOps ( identifier ) ;
@@ -801,7 +801,7 @@ RestWrite.prototype._validateEmail = function () {
801
801
{ } ,
802
802
this . validSchemaController
803
803
)
804
- . then ( results => {
804
+ . then ( async ( results ) => {
805
805
if ( results . length > 0 ) {
806
806
throw new ParseError (
807
807
ParseError . EMAIL_TAKEN ,
@@ -815,7 +815,8 @@ RestWrite.prototype._validateEmail = function () {
815
815
Object . keys ( this . data . authData ) [ 0 ] === 'anonymous' )
816
816
) {
817
817
// We updated the email, send a new validation
818
- const { originalObject, updatedObject } = this . buildParseObjects ( ) ;
818
+ const Parse = await loadModule ( 'parse/node.js' ) ;
819
+ const { originalObject, updatedObject } = this . buildParseObjects ( Parse ) ;
819
820
const request = {
820
821
original : originalObject ,
821
822
object : updatedObject ,
@@ -950,7 +951,8 @@ RestWrite.prototype.createSessionTokenIfNeeded = async function () {
950
951
// If sign-up call
951
952
if ( ! this . storage . authProvider ) {
952
953
// Create request object for verification functions
953
- const { originalObject, updatedObject } = this . buildParseObjects ( ) ;
954
+ const Parse = await loadModule ( 'parse/node.js' ) ;
955
+ const { originalObject, updatedObject } = this . buildParseObjects ( Parse ) ;
954
956
const request = {
955
957
original : originalObject ,
956
958
object : updatedObject ,
@@ -1540,9 +1542,9 @@ RestWrite.prototype.runDatabaseOperation = function () {
1540
1542
false ,
1541
1543
this . validSchemaController
1542
1544
)
1543
- . then ( response => {
1545
+ . then ( async ( response ) => {
1544
1546
response . updatedAt = this . updatedAt ;
1545
- this . _updateResponseWithData ( response , this . data ) ;
1547
+ await this . _updateResponseWithData ( response , this . data ) ;
1546
1548
this . response = { response } ;
1547
1549
} ) ;
1548
1550
} ) ;
@@ -1628,14 +1630,14 @@ RestWrite.prototype.runDatabaseOperation = function () {
1628
1630
) ;
1629
1631
} ) ;
1630
1632
} )
1631
- . then ( response => {
1633
+ . then ( async ( response ) => {
1632
1634
response . objectId = this . data . objectId ;
1633
1635
response . createdAt = this . data . createdAt ;
1634
1636
1635
1637
if ( this . responseShouldHaveUsername ) {
1636
1638
response . username = this . data . username ;
1637
1639
}
1638
- this . _updateResponseWithData ( response , this . data ) ;
1640
+ await this . _updateResponseWithData ( response , this . data ) ;
1639
1641
this . response = {
1640
1642
status : 201 ,
1641
1643
response,
@@ -1646,7 +1648,7 @@ RestWrite.prototype.runDatabaseOperation = function () {
1646
1648
} ;
1647
1649
1648
1650
// Returns nothing - doesn't wait for the trigger.
1649
- RestWrite . prototype . runAfterSaveTrigger = function ( ) {
1651
+ RestWrite . prototype . runAfterSaveTrigger = async function ( ) {
1650
1652
if ( ! this . response || ! this . response . response || this . runOptions . many ) {
1651
1653
return ;
1652
1654
}
@@ -1661,8 +1663,8 @@ RestWrite.prototype.runAfterSaveTrigger = function () {
1661
1663
if ( ! hasAfterSaveHook && ! hasLiveQuery ) {
1662
1664
return Promise . resolve ( ) ;
1663
1665
}
1664
-
1665
- const { originalObject, updatedObject } = this . buildParseObjects ( ) ;
1666
+ const Parse = await loadModule ( 'parse/node.js' ) ;
1667
+ const { originalObject, updatedObject } = this . buildParseObjects ( Parse ) ;
1666
1668
updatedObject . _handleSaveResponse ( this . response . response , this . response . status || 200 ) ;
1667
1669
1668
1670
if ( hasLiveQuery ) {
@@ -1690,13 +1692,13 @@ RestWrite.prototype.runAfterSaveTrigger = function () {
1690
1692
this . config ,
1691
1693
this . context
1692
1694
)
1693
- . then ( result => {
1695
+ . then ( async ( result ) => {
1694
1696
const jsonReturned = result && ! result . _toFullJSON ;
1695
1697
if ( jsonReturned ) {
1696
1698
this . pendingOps . operations = { } ;
1697
1699
this . response . response = result ;
1698
1700
} else {
1699
- this . response . response = this . _updateResponseWithData (
1701
+ this . response . response = await this . _updateResponseWithData (
1700
1702
( result || updatedObject ) . toJSON ( ) ,
1701
1703
this . data
1702
1704
) ;
@@ -1721,7 +1723,7 @@ RestWrite.prototype.objectId = function () {
1721
1723
} ;
1722
1724
1723
1725
// Returns a copy of the data and delete bad keys (_auth_data, _hashed_password...)
1724
- RestWrite . prototype . sanitizedData = function ( ) {
1726
+ RestWrite . prototype . sanitizedData = function ( Parse ) {
1725
1727
const data = Object . keys ( this . data ) . reduce ( ( data , key ) => {
1726
1728
// Regexp comes from Parse.Object.prototype.validate
1727
1729
if ( ! / ^ [ A - Z a - z ] [ 0 - 9 A - Z a - z _ ] * $ / . test ( key ) ) {
@@ -1733,7 +1735,7 @@ RestWrite.prototype.sanitizedData = function () {
1733
1735
} ;
1734
1736
1735
1737
// Returns an updated copy of the object
1736
- RestWrite . prototype . buildParseObjects = function ( ) {
1738
+ RestWrite . prototype . buildParseObjects = function ( Parse ) {
1737
1739
const extraData = { className : this . className , objectId : this . query ?. objectId } ;
1738
1740
let originalObject ;
1739
1741
if ( this . query && this . query . objectId ) {
@@ -1772,7 +1774,7 @@ RestWrite.prototype.buildParseObjects = function () {
1772
1774
return data ;
1773
1775
} , deepcopy ( this . data ) ) ;
1774
1776
1775
- const sanitized = this . sanitizedData ( ) ;
1777
+ const sanitized = this . sanitizedData ( Parse ) ;
1776
1778
for ( const attribute of readOnlyAttributes ) {
1777
1779
delete sanitized [ attribute ] ;
1778
1780
}
@@ -1796,13 +1798,17 @@ RestWrite.prototype.cleanUserAuthData = function () {
1796
1798
}
1797
1799
} ;
1798
1800
1799
- RestWrite . prototype . _updateResponseWithData = function ( response , data ) {
1800
- const stateController = Parse . CoreManager . getObjectStateController ( ) ;
1801
- const [ pending ] = stateController . getPendingOps ( this . pendingOps . identifier ) ;
1802
- for ( const key in this . pendingOps . operations ) {
1803
- if ( ! pending [ key ] ) {
1804
- data [ key ] = this . originalData ? this . originalData [ key ] : { __op : 'Delete' } ;
1805
- this . storage . fieldsChangedByTrigger . push ( key ) ;
1801
+ RestWrite . prototype . _updateResponseWithData = async function ( response , data ) {
1802
+ // Operations are set if beforeSave trigger is used
1803
+ if ( this . pendingOps . operations ) {
1804
+ const Parse = await loadModule ( 'parse/node.js' ) ;
1805
+ const stateController = Parse . CoreManager . getObjectStateController ( ) ;
1806
+ const [ pending ] = stateController . getPendingOps ( this . pendingOps . identifier ) ;
1807
+ for ( const key in this . pendingOps . operations ) {
1808
+ if ( ! pending [ key ] ) {
1809
+ data [ key ] = this . originalData ? this . originalData [ key ] : { __op : 'Delete' } ;
1810
+ this . storage . fieldsChangedByTrigger . push ( key ) ;
1811
+ }
1806
1812
}
1807
1813
}
1808
1814
const skipKeys = [ ...( requiredColumns . read [ this . className ] || [ ] ) ] ;
0 commit comments