Repository: parse-community/Parse-SDK-JS
Pull request #898 (2019-08-21 20:39:54)

Support object.exists()
Returns a promise boolean if the object exists on the server

See https://github.com/parse-community/parse-server/issues/5950

codecov[bot]: # [Codecov](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/898?src=pr&el=h1) Report
> Merging [#898](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/898?src=pr&el=desc) into [master](https://codecov.io/gh/parse-community/Parse-SDK-JS/commit/3af83eaa157bdd85aa65abf2462eec3490f49ee4?src=pr&el=desc) will **increase** coverage by `<.01%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/898/graphs/tree.svg?width=650&token=8nCENM0QHj&height=150&src=pr)](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/898?src=pr&el=tree)

```diff
@@            Coverage Diff            @@
##           master    #898      +/-   ##
=========================================
+ Coverage    92.1%   92.1%   +<.01%     
=========================================
  Files          54      54              
  Lines        5026    5031       +5     
  Branches     1127    1127              
=========================================
+ Hits         4629    4634       +5     
  Misses        397     397
```


| [Impacted Files](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/898?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/ParseObject.js](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/898/diff?src=pr&el=tree#diff-c3JjL1BhcnNlT2JqZWN0Lmpz) | `90.26% <100%> (+0.05%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/898?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/898?src=pr&el=footer). Last update [3af83ea...cbd01b1](https://codecov.io/gh/parse-community/Parse-SDK-JS/pull/898?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/src/ParseObject.js b/src/ParseObject.js
index 7270227..55341dd 100644
--- a/src/ParseObject.js
+++ b/src/ParseObject.js
@@ -933,6 +933,34 @@ class ParseObject {
     return false;
   }
 
+  /**
+   * Returns true if this object exists on the Server
+   *
+   * @param {Object} options
+   * Valid options are:<ul>
+   *   <li>useMasterKey: In Cloud Code and Node only, causes the Master Key to
+   *     be used for this request.
+   *   <li>sessionToken: A valid session token, used for making a request on
+   *       behalf of a specific user.
+   * </ul>
+   * @return {Promise<boolean>} A boolean promise that is fulfilled if object exists.
+   */
+  async exists(options?: RequestOptions): Promise<boolean> {
+    if (!this.id) {
+      return false;
+    }
+    try {
+      const query = new ParseQuery(this.className)
+      await query.get(this.id, options);
+      return true;
+    } catch (e) {
+      if (e.code === ParseError.OBJECT_NOT_FOUND) {
+        return false;
+      }
+      throw e;
+    }
+  }
+
   /**
    * Checks if the model is currently in a valid state.
    * @return {Boolean}


Test patch:
diff --git a/integration/test/ParseObjectTest.js b/integration/test/ParseObjectTest.js
index d4dddc3..a1a3d8a 100644
--- a/integration/test/ParseObjectTest.js
+++ b/integration/test/ParseObjectTest.js
@@ -80,6 +80,15 @@ describe('Parse Object', () => {
     });
   });
 
+  it('can check if object exists', async () => {
+    const object = new TestObject();
+    assert.equal(await object.exists(), false);
+    await object.save();
+    assert.equal(await object.exists(), true);
+    await object.destroy();
+    assert.equal(await object.exists(), false);
+  });
+
   it('can find objects', (done) => {
     const object = new TestObject({ foo: 'bar' });
     object.save().then(() => {
diff --git a/src/__tests__/ParseObject-test.js b/src/__tests__/ParseObject-test.js
index 8b74298..07e3453 100644
--- a/src/__tests__/ParseObject-test.js
+++ b/src/__tests__/ParseObject-test.js
@@ -101,6 +101,13 @@ mockQuery.prototype.include = function(keys) {
 mockQuery.prototype.find = function() {
   return Promise.resolve(this.results);
 };
+mockQuery.prototype.get = function(id) {
+  const object = ParseObject.fromJSON({
+    className: this.className,
+    objectId: id
+  });
+  return Promise.resolve(object);
+};
 jest.setMock('../ParseQuery', mockQuery);
 
 import { DEFAULT_PIN, PIN_PREFIX } from '../LocalDatastoreUtils';
@@ -1109,6 +1116,13 @@ describe('ParseObject', () => {
     spy.mockRestore();
   });
 
+  it('can check if object exists', async () => {
+    const parent = new ParseObject('Person');
+    expect(await parent.exists()).toBe(false);
+    parent.id = '1234'
+    expect(await parent.exists()).toBe(true);
+  });
+
   it('can save the object', (done) => {
     CoreManager.getRESTController()._setXHR(
       mockXHR([{