diff --git a/integration/test/ParseObjectTest.js b/integration/test/ParseObjectTest.js index c02fda473..3ee699929 100644 --- a/integration/test/ParseObjectTest.js +++ b/integration/test/ParseObjectTest.js @@ -826,6 +826,24 @@ describe('Parse Object', () => { }); }); + it('can add objects to an array in batch mode', (done) => { + let child1 = new Parse.Object('Person'); + let child2 = new Parse.Object('Person'); + let parent = new Parse.Object('Person'); + + Promise.all([child1.save(), child2.save()]).then((children) => { + parent.addAll('children', children); + return parent.save(); + }).then(() => { + let query = new Parse.Query('Person'); + return query.get(parent.id); + }).then((p) => { + assert.equal(p.get('children')[0].id, child1.id); + assert.equal(p.get('children')[1].id, child2.id); + done(); + }); + }); + it('can convert saved objects to json', (done) => { let object = new TestObject(); object.save({ foo: 'bar' }).then(() => { @@ -865,6 +883,29 @@ describe('Parse Object', () => { }); }); + it('can remove objects from array fields in batch mode', (done) => { + let obj1 = new TestObject(); + let obj2 = new TestObject(); + + Promise.all([obj1.save(), obj2.save()]).then((objects) => { + let container = new TestObject(); + container.addAll('array', objects); + assert.equal(container.get('array').length, 2); + return container.save(); + }).then((container) => { + let o1 = new TestObject(); + o1.id = obj1.id; + let o2 = new TestObject(); + o2.id = obj2.id; + let o3 = new TestObject(); + o3.id = 'there_is_no_such_object' + + container.removeAll('array', [o1, o2, o3]); + assert.equal(container.get('array').length, 0); + done(); + }); + }); + it('can perform async methods', (done) => { let object = new TestObject(); object.set('time', 'adventure'); diff --git a/src/ParseObject.js b/src/ParseObject.js index e5dd6ba83..9d82a600f 100644 --- a/src/ParseObject.js +++ b/src/ParseObject.js @@ -735,6 +735,17 @@ export default class ParseObject { return this.set(attr, new AddOp([item])); } + /** + * Atomically add the objects to the end of the array associated with a given + * key. + * @method addAll + * @param attr {String} The key. + * @param items {[]} The items to add. + */ + addAll(attr: string, items: Array): ParseObject | boolean { + return this.set(attr, new AddOp(items)); + } + /** * Atomically add an object to the array associated with a given key, only * if it is not already present in the array. The position of the insert is @@ -748,6 +759,19 @@ export default class ParseObject { return this.set(attr, new AddUniqueOp([item])); } + /** + * Atomically add the objects to the array associated with a given key, only + * if it is not already present in the array. The position of the insert is + * not guaranteed. + * + * @method addAllUnique + * @param attr {String} The key. + * @param items {[]} The objects to add. + */ + addAllUnique(attr: string, items: Array): ParseObject | boolean { + return this.set(attr, new AddUniqueOp(items)); + } + /** * Atomically remove all instances of an object from the array associated * with a given key. @@ -760,6 +784,18 @@ export default class ParseObject { return this.set(attr, new RemoveOp([item])); } + /** + * Atomically remove all instances of the objects from the array associated + * with a given key. + * + * @method removeAll + * @param attr {String} The key. + * @param items {[]} The object to remove. + */ + removeAll(attr: string, items: Array): ParseObject | boolean { + return this.set(attr, new RemoveOp(items)); + } + /** * Returns an instance of a subclass of Parse.Op describing what kind of * modification has been performed on this field since the last time it was diff --git a/src/__tests__/ParseObject-test.js b/src/__tests__/ParseObject-test.js index 17e539593..9432036a5 100644 --- a/src/__tests__/ParseObject-test.js +++ b/src/__tests__/ParseObject-test.js @@ -423,6 +423,25 @@ describe('ParseObject', () => { expect(o.get('available')).toEqual(['Monday', 'Wednesday', 'Thursday']); }); + it('can add elements to an array field in batch mode', () => { + var o = new ParseObject('Schedule'); + o.addAll('available', ['Monday', 'Wednesday']); + expect(o.get('available')).toEqual(['Monday', 'Wednesday']); + + o.set('colors', ['red']); + o.addAll('colors', ['green', 'blue']); + expect(o.get('colors')).toEqual(['red', 'green', 'blue']); + + o._handleSaveResponse({ + objectId: 'S1', + available: ['Monday', 'Wednesday'], + colors: ['red', 'green', 'blue'] + }); + + o.addAllUnique('available', ['Thursday', 'Monday']); + expect(o.get('available').length).toEqual(3); + }); + it('can remove elements from an array field', () => { var o = new ParseObject('Schedule'); o.set('available', ['Monday', 'Tuesday']); @@ -440,6 +459,21 @@ describe('ParseObject', () => { expect(o.get('available')).toEqual([]); }); + it('can remove elements from an array field in batch mode', () => { + var o = new ParseObject('Schedule'); + o.set('available', ['Monday', 'Tuesday']); + o.removeAll('available', ['Tuesday', 'Saturday']); + expect(o.get('available')).toEqual(['Monday']); + + o._handleSaveResponse({ + objectId: 'S2', + available: ['Monday'] + }); + + o.removeAll('available', ['Monday', 'Tuesday']); + expect(o.get('available')).toEqual([]); + }); + it('can chain sets', () => { var o = new ParseObject('Person'); o.set('developer', true).set('platform', 'web');