Skip to content

Commit 4ea455b

Browse files
yuki-takeichiflovilmart
authored andcommitted
support Relation object saving (#3074)
* support Parse.Relation object save * prevent Relation object from being saved in storage
1 parent e9e6a47 commit 4ea455b

File tree

5 files changed

+83
-0
lines changed

5 files changed

+83
-0
lines changed

spec/MongoTransform.spec.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,19 @@ describe('parseObjectToMongoObjectForCreate', () => {
236236
done();
237237
});
238238

239+
it('removes Relation types', (done) => {
240+
var input = {
241+
aRelation: { __type: 'Relation', className: 'Stuff' },
242+
};
243+
var output = transform.parseObjectToMongoObjectForCreate(null, input, {
244+
fields: {
245+
aRelation: { __type: 'Relation', className: 'Stuff' },
246+
},
247+
});
248+
expect(output).toEqual({});
249+
done();
250+
});
251+
239252
it('writes the old ACL format in addition to rperm and wperm on update', (done) => {
240253
var input = {
241254
_rperm: ['*'],
@@ -280,5 +293,19 @@ describe('parseObjectToMongoObjectForCreate', () => {
280293
expect(output.double).toBe(Number.MAX_VALUE);
281294
done();
282295
});
296+
});
283297

298+
describe('transformUpdate', () => {
299+
it('removes Relation types', (done) => {
300+
var input = {
301+
aRelation: { __type: 'Relation', className: 'Stuff' },
302+
};
303+
var output = transform.transformUpdate(null, input, {
304+
fields: {
305+
aRelation: { __type: 'Relation', className: 'Stuff' },
306+
},
307+
});
308+
expect(output).toEqual({});
309+
done();
310+
});
284311
});

spec/ParseRelation.spec.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,4 +741,32 @@ describe('Parse.Relation testing', () => {
741741
done();
742742
});
743743
});
744+
745+
it('can be saved without error', done => {
746+
let obj1 = new Parse.Object('PPAP');
747+
obj1.save()
748+
.then(() => {
749+
let newRelation = obj1.relation('aRelation');
750+
newRelation.add(obj1);
751+
obj1.save().then(() => {
752+
let relation = obj1.get('aRelation');
753+
obj1.set('aRelation', relation);
754+
obj1.save().then(() => {
755+
done();
756+
}, error => {
757+
fail('failed to save ParseRelation object');
758+
fail(error);
759+
done();
760+
});
761+
}, error => {
762+
fail('failed to create relation field');
763+
fail(error);
764+
done();
765+
});
766+
}, error => {
767+
fail('failed to save obj');
768+
fail(error);
769+
done();
770+
});
771+
});
744772
});

spec/Schema.spec.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,21 @@ describe('SchemaController', () => {
5656
});
5757
});
5858

59+
it('can validate Relation object', (done) => {
60+
config.database.loadSchema().then((schema) => {
61+
return schema.validateObject('Stuff', {aRelation: {__type:'Relation',className:'Stuff'}});
62+
}).then((schema) => {
63+
return schema.validateObject('Stuff', {aRelation: {__type:'Pointer',className:'Stuff'}})
64+
.then((schema) => {
65+
fail('expected invalidity');
66+
done();
67+
}, done);
68+
}, (err) => {
69+
fail(err);
70+
done();
71+
});
72+
});
73+
5974
it('rejects inconsistent types', (done) => {
6075
config.database.loadSchema().then((schema) => {
6176
return schema.validateObject('Stuff', {bacon: 7});

src/Adapters/Storage/Mongo/MongoTransform.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,9 @@ const parseObjectToMongoObjectForCreate = (className, restCreate, schema) => {
319319
restCreate = addLegacyACL(restCreate);
320320
let mongoCreate = {}
321321
for (let restKey in restCreate) {
322+
if (restCreate[restKey] && restCreate[restKey].__type === 'Relation') {
323+
continue;
324+
}
322325
let { key, value } = parseObjectKeyValueToMongoObjectKeyValue(
323326
restKey,
324327
restCreate[restKey],
@@ -359,6 +362,9 @@ const transformUpdate = (className, restUpdate, parseFormatSchema) => {
359362
}
360363
}
361364
for (var restKey in restUpdate) {
365+
if (restUpdate[restKey] && restUpdate[restKey].__type === 'Relation') {
366+
continue;
367+
}
362368
var out = transformKeyValueForUpdate(className, restKey, restUpdate[restKey], parseFormatSchema);
363369

364370
// If the output value is an object with any $ keys, it's an

src/Controllers/SchemaController.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,13 @@ function getObjectType(obj) {
895895
targetClass: obj.className
896896
}
897897
}
898+
case 'Relation' :
899+
if(obj.className) {
900+
return {
901+
type: 'Relation',
902+
targetClass: obj.className
903+
}
904+
}
898905
case 'File' :
899906
if(obj.name) {
900907
return 'File';

0 commit comments

Comments
 (0)