Skip to content

Commit 414af99

Browse files
committed
Merge pull request #1114 from ParsePlatform/flovilmart.addUniqueObject
Adds support for plain object in $add, $addUnique, $remove
2 parents 134b6de + 937dde5 commit 414af99

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

spec/ParseObject.spec.js

+48-5
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,49 @@ describe('Parse.Object testing', () => {
600600
});
601601
});
602602

603+
it("addUnique with object", function(done) {
604+
var x1 = new Parse.Object('X');
605+
x1.set('stuff', [ 1, {'hello': 'world'}, {'foo': 'bar'}]);
606+
x1.save().then(() => {
607+
var objectId = x1.id;
608+
var x2 = new Parse.Object('X', {objectId: objectId});
609+
x2.addUnique('stuff', {'hello': 'world'});
610+
x2.addUnique('stuff', {'bar': 'baz'});
611+
expect(x2.get('stuff')).toEqual([{'hello': 'world'}, {'bar': 'baz'}]);
612+
return x2.save();
613+
}).then(() => {
614+
var query = new Parse.Query('X');
615+
return query.get(x1.id);
616+
}).then((x3) => {
617+
expect(x3.get('stuff')).toEqual([1, {'hello': 'world'}, {'foo': 'bar'}, {'bar': 'baz'}]);
618+
done();
619+
}, (error) => {
620+
fail(error);
621+
done();
622+
});
623+
});
624+
625+
it("removes with object", function(done) {
626+
var x1 = new Parse.Object('X');
627+
x1.set('stuff', [ 1, {'hello': 'world'}, {'foo': 'bar'}]);
628+
x1.save().then(() => {
629+
var objectId = x1.id;
630+
var x2 = new Parse.Object('X', {objectId: objectId});
631+
x2.remove('stuff', {'hello': 'world'});
632+
expect(x2.get('stuff')).toEqual([]);
633+
return x2.save();
634+
}).then(() => {
635+
var query = new Parse.Query('X');
636+
return query.get(x1.id);
637+
}).then((x3) => {
638+
expect(x3.get('stuff')).toEqual([1, {'foo': 'bar'}]);
639+
done();
640+
}, (error) => {
641+
fail(error);
642+
done();
643+
});
644+
});
645+
603646
it("dirty attributes", function(done) {
604647
var object = new Parse.Object("TestObject");
605648
object.set("cat", "good");
@@ -1794,14 +1837,14 @@ describe('Parse.Object testing', () => {
17941837
done();
17951838
});
17961839
});
1797-
1840+
17981841
it('should have undefined includes when object is missing', (done) => {
17991842
let obj1 = new Parse.Object("AnObject");
18001843
let obj2 = new Parse.Object("AnObject");
1801-
1844+
18021845
Parse.Object.saveAll([obj1, obj2]).then(() => {
18031846
obj1.set("obj", obj2);
1804-
// Save the pointer, delete the pointee
1847+
// Save the pointer, delete the pointee
18051848
return obj1.save().then(() => { return obj2.destroy() });
18061849
}).then(() => {
18071850
let query = new Parse.Query("AnObject");
@@ -1823,15 +1866,15 @@ describe('Parse.Object testing', () => {
18231866
done();
18241867
})
18251868
});
1826-
1869+
18271870
it('should have undefined includes when object is missing on deeper path', (done) => {
18281871
let obj1 = new Parse.Object("AnObject");
18291872
let obj2 = new Parse.Object("AnObject");
18301873
let obj3 = new Parse.Object("AnObject");
18311874
Parse.Object.saveAll([obj1, obj2, obj3]).then(() => {
18321875
obj1.set("obj", obj2);
18331876
obj2.set("obj", obj3);
1834-
// Save the pointer, delete the pointee
1877+
// Save the pointer, delete the pointee
18351878
return Parse.Object.saveAll([obj1, obj2]).then(() => { return obj3.destroy() });
18361879
}).then(() => {
18371880
let query = new Parse.Query("AnObject");

src/transform.js

+3
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,9 @@ function transformAtom(atom, force, options) {
398398
if (FileCoder.isValidJSON(atom)) {
399399
return (inArray || inObject ? atom : FileCoder.JSONToDatabase(atom));
400400
}
401+
if (inArray || inObject) {
402+
return atom;
403+
}
401404

402405
if (force) {
403406
throw new Parse.Error(Parse.Error.INVALID_JSON,

0 commit comments

Comments
 (0)