Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions integration/test/ParseObjectTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down
28 changes: 28 additions & 0 deletions src/ParseObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
14 changes: 14 additions & 0 deletions src/__tests__/ParseObject-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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([{
Expand Down