Skip to content

Commit af28f27

Browse files
arniuflovilmart
authored andcommitted
Add methods addAll, addAllUnique and removeAll (#459)
* Add methods `addAll`, `addAllUnique` and `removeAll` * Add methods `addAll`, `addAllUnique` and `removeAll` * Add test for `addAll`, `addAllUnique` and `removeAll` * Revert test case
1 parent 13438fe commit af28f27

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

integration/test/ParseObjectTest.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,24 @@ describe('Parse Object', () => {
826826
});
827827
});
828828

829+
it('can add objects to an array in batch mode', (done) => {
830+
let child1 = new Parse.Object('Person');
831+
let child2 = new Parse.Object('Person');
832+
let parent = new Parse.Object('Person');
833+
834+
Promise.all([child1.save(), child2.save()]).then((children) => {
835+
parent.addAll('children', children);
836+
return parent.save();
837+
}).then(() => {
838+
let query = new Parse.Query('Person');
839+
return query.get(parent.id);
840+
}).then((p) => {
841+
assert.equal(p.get('children')[0].id, child1.id);
842+
assert.equal(p.get('children')[1].id, child2.id);
843+
done();
844+
});
845+
});
846+
829847
it('can convert saved objects to json', (done) => {
830848
let object = new TestObject();
831849
object.save({ foo: 'bar' }).then(() => {
@@ -865,6 +883,29 @@ describe('Parse Object', () => {
865883
});
866884
});
867885

886+
it('can remove objects from array fields in batch mode', (done) => {
887+
let obj1 = new TestObject();
888+
let obj2 = new TestObject();
889+
890+
Promise.all([obj1.save(), obj2.save()]).then((objects) => {
891+
let container = new TestObject();
892+
container.addAll('array', objects);
893+
assert.equal(container.get('array').length, 2);
894+
return container.save();
895+
}).then((container) => {
896+
let o1 = new TestObject();
897+
o1.id = obj1.id;
898+
let o2 = new TestObject();
899+
o2.id = obj2.id;
900+
let o3 = new TestObject();
901+
o3.id = 'there_is_no_such_object'
902+
903+
container.removeAll('array', [o1, o2, o3]);
904+
assert.equal(container.get('array').length, 0);
905+
done();
906+
});
907+
});
908+
868909
it('can perform async methods', (done) => {
869910
let object = new TestObject();
870911
object.set('time', 'adventure');

src/ParseObject.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,17 @@ export default class ParseObject {
735735
return this.set(attr, new AddOp([item]));
736736
}
737737

738+
/**
739+
* Atomically add the objects to the end of the array associated with a given
740+
* key.
741+
* @method addAll
742+
* @param attr {String} The key.
743+
* @param items {[]} The items to add.
744+
*/
745+
addAll(attr: string, items: Array<mixed>): ParseObject | boolean {
746+
return this.set(attr, new AddOp(items));
747+
}
748+
738749
/**
739750
* Atomically add an object to the array associated with a given key, only
740751
* if it is not already present in the array. The position of the insert is
@@ -748,6 +759,19 @@ export default class ParseObject {
748759
return this.set(attr, new AddUniqueOp([item]));
749760
}
750761

762+
/**
763+
* Atomically add the objects to the array associated with a given key, only
764+
* if it is not already present in the array. The position of the insert is
765+
* not guaranteed.
766+
*
767+
* @method addAllUnique
768+
* @param attr {String} The key.
769+
* @param items {[]} The objects to add.
770+
*/
771+
addAllUnique(attr: string, items: Array<mixed>): ParseObject | boolean {
772+
return this.set(attr, new AddUniqueOp(items));
773+
}
774+
751775
/**
752776
* Atomically remove all instances of an object from the array associated
753777
* with a given key.
@@ -760,6 +784,18 @@ export default class ParseObject {
760784
return this.set(attr, new RemoveOp([item]));
761785
}
762786

787+
/**
788+
* Atomically remove all instances of the objects from the array associated
789+
* with a given key.
790+
*
791+
* @method removeAll
792+
* @param attr {String} The key.
793+
* @param items {[]} The object to remove.
794+
*/
795+
removeAll(attr: string, items: Array<mixed>): ParseObject | boolean {
796+
return this.set(attr, new RemoveOp(items));
797+
}
798+
763799
/**
764800
* Returns an instance of a subclass of Parse.Op describing what kind of
765801
* modification has been performed on this field since the last time it was

src/__tests__/ParseObject-test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,25 @@ describe('ParseObject', () => {
423423
expect(o.get('available')).toEqual(['Monday', 'Wednesday', 'Thursday']);
424424
});
425425

426+
it('can add elements to an array field in batch mode', () => {
427+
var o = new ParseObject('Schedule');
428+
o.addAll('available', ['Monday', 'Wednesday']);
429+
expect(o.get('available')).toEqual(['Monday', 'Wednesday']);
430+
431+
o.set('colors', ['red']);
432+
o.addAll('colors', ['green', 'blue']);
433+
expect(o.get('colors')).toEqual(['red', 'green', 'blue']);
434+
435+
o._handleSaveResponse({
436+
objectId: 'S1',
437+
available: ['Monday', 'Wednesday'],
438+
colors: ['red', 'green', 'blue']
439+
});
440+
441+
o.addAllUnique('available', ['Thursday', 'Monday']);
442+
expect(o.get('available').length).toEqual(3);
443+
});
444+
426445
it('can remove elements from an array field', () => {
427446
var o = new ParseObject('Schedule');
428447
o.set('available', ['Monday', 'Tuesday']);
@@ -440,6 +459,21 @@ describe('ParseObject', () => {
440459
expect(o.get('available')).toEqual([]);
441460
});
442461

462+
it('can remove elements from an array field in batch mode', () => {
463+
var o = new ParseObject('Schedule');
464+
o.set('available', ['Monday', 'Tuesday']);
465+
o.removeAll('available', ['Tuesday', 'Saturday']);
466+
expect(o.get('available')).toEqual(['Monday']);
467+
468+
o._handleSaveResponse({
469+
objectId: 'S2',
470+
available: ['Monday']
471+
});
472+
473+
o.removeAll('available', ['Monday', 'Tuesday']);
474+
expect(o.get('available')).toEqual([]);
475+
});
476+
443477
it('can chain sets', () => {
444478
var o = new ParseObject('Person');
445479
o.set('developer', true).set('platform', 'web');

0 commit comments

Comments
 (0)