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', () => {
8080 } ) ;
8181 } ) ;
8282
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+
8392 it ( 'can find objects' , ( done ) => {
8493 const object = new TestObject ( { foo : 'bar' } ) ;
8594 object . save ( ) . then ( ( ) => {
Original file line number Diff line number Diff line change @@ -933,6 +933,34 @@ class ParseObject {
933933 return false ;
934934 }
935935
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+
936964 /**
937965 * Checks if the model is currently in a valid state.
938966 * @return {Boolean }
Original file line number Diff line number Diff line change @@ -101,6 +101,13 @@ mockQuery.prototype.include = function(keys) {
101101mockQuery . prototype . find = function ( ) {
102102 return Promise . resolve ( this . results ) ;
103103} ;
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+ } ;
104111jest . setMock ( '../ParseQuery' , mockQuery ) ;
105112
106113import { DEFAULT_PIN , PIN_PREFIX } from '../LocalDatastoreUtils' ;
@@ -1109,6 +1116,13 @@ describe('ParseObject', () => {
11091116 spy . mockRestore ( ) ;
11101117 } ) ;
11111118
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+
11121126 it ( 'can save the object' , ( done ) => {
11131127 CoreManager . getRESTController ( ) . _setXHR (
11141128 mockXHR ( [ {
You can’t perform that action at this time.
0 commit comments