File tree Expand file tree Collapse file tree 3 files changed +51
-0
lines changed Expand file tree Collapse file tree 3 files changed +51
-0
lines changed Original file line number Diff line number Diff line change @@ -80,6 +80,15 @@ describe('Parse Object', () => {
80
80
} ) ;
81
81
} ) ;
82
82
83
+ it ( 'can check if object exists' , async ( ) => {
84
+ const object = new TestObject ( ) ;
85
+ assert . equal ( await object . exists ( ) , false ) ;
86
+ await object . save ( ) ;
87
+ assert . equal ( await object . exists ( ) , true ) ;
88
+ await object . destroy ( ) ;
89
+ assert . equal ( await object . exists ( ) , false ) ;
90
+ } ) ;
91
+
83
92
it ( 'can find objects' , ( done ) => {
84
93
const object = new TestObject ( { foo : 'bar' } ) ;
85
94
object . save ( ) . then ( ( ) => {
Original file line number Diff line number Diff line change @@ -933,6 +933,34 @@ class ParseObject {
933
933
return false ;
934
934
}
935
935
936
+ /**
937
+ * Returns true if this object exists on the Server
938
+ *
939
+ * @param {Object } options
940
+ * Valid options are:<ul>
941
+ * <li>useMasterKey: In Cloud Code and Node only, causes the Master Key to
942
+ * be used for this request.
943
+ * <li>sessionToken: A valid session token, used for making a request on
944
+ * behalf of a specific user.
945
+ * </ul>
946
+ * @return {Promise<boolean> } A boolean promise that is fulfilled if object exists.
947
+ */
948
+ async exists ( options ? : RequestOptions ) : Promise < boolean > {
949
+ if ( ! this . id ) {
950
+ return false ;
951
+ }
952
+ try {
953
+ const query = new ParseQuery ( this . className )
954
+ await query . get ( this . id , options ) ;
955
+ return true ;
956
+ } catch ( e ) {
957
+ if ( e . code === ParseError . OBJECT_NOT_FOUND ) {
958
+ return false ;
959
+ }
960
+ throw e ;
961
+ }
962
+ }
963
+
936
964
/**
937
965
* Checks if the model is currently in a valid state.
938
966
* @return {Boolean }
Original file line number Diff line number Diff line change @@ -101,6 +101,13 @@ mockQuery.prototype.include = function(keys) {
101
101
mockQuery . prototype . find = function ( ) {
102
102
return Promise . resolve ( this . results ) ;
103
103
} ;
104
+ mockQuery . prototype . get = function ( id ) {
105
+ const object = ParseObject . fromJSON ( {
106
+ className : this . className ,
107
+ objectId : id
108
+ } ) ;
109
+ return Promise . resolve ( object ) ;
110
+ } ;
104
111
jest . setMock ( '../ParseQuery' , mockQuery ) ;
105
112
106
113
import { DEFAULT_PIN , PIN_PREFIX } from '../LocalDatastoreUtils' ;
@@ -1109,6 +1116,13 @@ describe('ParseObject', () => {
1109
1116
spy . mockRestore ( ) ;
1110
1117
} ) ;
1111
1118
1119
+ it ( 'can check if object exists' , async ( ) => {
1120
+ const parent = new ParseObject ( 'Person' ) ;
1121
+ expect ( await parent . exists ( ) ) . toBe ( false ) ;
1122
+ parent . id = '1234'
1123
+ expect ( await parent . exists ( ) ) . toBe ( true ) ;
1124
+ } ) ;
1125
+
1112
1126
it ( 'can save the object' , ( done ) => {
1113
1127
CoreManager . getRESTController ( ) . _setXHR (
1114
1128
mockXHR ( [ {
You can’t perform that action at this time.
0 commit comments