Skip to content

Add methods addAll, addAllUnique and removeAll #459

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 23, 2017
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
41 changes: 41 additions & 0 deletions integration/test/ParseObjectTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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');
Expand Down
36 changes: 36 additions & 0 deletions src/ParseObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<mixed>): 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
Expand All @@ -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<mixed>): ParseObject | boolean {
return this.set(attr, new AddUniqueOp(items));
}

/**
* Atomically remove all instances of an object from the array associated
* with a given key.
Expand All @@ -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<mixed>): 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
Expand Down
34 changes: 34 additions & 0 deletions src/__tests__/ParseObject-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand All @@ -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');
Expand Down