From 18fa88ed9ad658e2392951ac1083894f8ef212eb Mon Sep 17 00:00:00 2001 From: Yuki Takeichi Date: Sat, 19 Nov 2016 19:59:07 +0900 Subject: [PATCH 1/2] support Parse.Relation object save --- spec/ParseRelation.spec.js | 28 ++++++++++++++++++++++++++++ spec/Schema.spec.js | 15 +++++++++++++++ src/Controllers/SchemaController.js | 7 +++++++ 3 files changed, 50 insertions(+) diff --git a/spec/ParseRelation.spec.js b/spec/ParseRelation.spec.js index 1d5ca2abd3..9e80067db6 100644 --- a/spec/ParseRelation.spec.js +++ b/spec/ParseRelation.spec.js @@ -741,4 +741,32 @@ describe('Parse.Relation testing', () => { done(); }); }); + + it('can be saved without error', done => { + let obj1 = new Parse.Object('PPAP'); + obj1.save() + .then(() => { + let newRelation = obj1.relation('aRelation'); + newRelation.add(obj1); + obj1.save().then(() => { + let relation = obj1.get('aRelation'); + obj1.set('aRelation', relation); + obj1.save().then(() => { + done(); + }, error => { + fail('failed to save ParseRelation object'); + fail(error); + done(); + }); + }, error => { + fail('failed to create relation field'); + fail(error); + done(); + }); + }, error => { + fail('failed to save obj'); + fail(error); + done(); + }); + }); }); diff --git a/spec/Schema.spec.js b/spec/Schema.spec.js index 4a9fc2aa56..3c59dca2fa 100644 --- a/spec/Schema.spec.js +++ b/spec/Schema.spec.js @@ -56,6 +56,21 @@ describe('SchemaController', () => { }); }); + it('can validate Relation object', (done) => { + config.database.loadSchema().then((schema) => { + return schema.validateObject('Stuff', {aRelation: {__type:'Relation',className:'Stuff'}}); + }).then((schema) => { + return schema.validateObject('Stuff', {aRelation: {__type:'Pointer',className:'Stuff'}}) + .then((schema) => { + fail('expected invalidity'); + done(); + }, done); + }, (err) => { + fail(err); + done(); + }); + }); + it('rejects inconsistent types', (done) => { config.database.loadSchema().then((schema) => { return schema.validateObject('Stuff', {bacon: 7}); diff --git a/src/Controllers/SchemaController.js b/src/Controllers/SchemaController.js index 316d3da362..a2192deaea 100644 --- a/src/Controllers/SchemaController.js +++ b/src/Controllers/SchemaController.js @@ -895,6 +895,13 @@ function getObjectType(obj) { targetClass: obj.className } } + case 'Relation' : + if(obj.className) { + return { + type: 'Relation', + targetClass: obj.className + } + } case 'File' : if(obj.name) { return 'File'; From cd424bcc90d19d96db370173e6f921d43f295cf9 Mon Sep 17 00:00:00 2001 From: Yuki Takeichi Date: Sat, 19 Nov 2016 20:36:17 +0900 Subject: [PATCH 2/2] prevent Relation object from being saved in storage --- spec/MongoTransform.spec.js | 27 ++++++++++++++++++++ src/Adapters/Storage/Mongo/MongoTransform.js | 6 +++++ 2 files changed, 33 insertions(+) diff --git a/spec/MongoTransform.spec.js b/spec/MongoTransform.spec.js index 9ae40e1615..db575bc1dc 100644 --- a/spec/MongoTransform.spec.js +++ b/spec/MongoTransform.spec.js @@ -236,6 +236,19 @@ describe('parseObjectToMongoObjectForCreate', () => { done(); }); + it('removes Relation types', (done) => { + var input = { + aRelation: { __type: 'Relation', className: 'Stuff' }, + }; + var output = transform.parseObjectToMongoObjectForCreate(null, input, { + fields: { + aRelation: { __type: 'Relation', className: 'Stuff' }, + }, + }); + expect(output).toEqual({}); + done(); + }); + it('writes the old ACL format in addition to rperm and wperm on update', (done) => { var input = { _rperm: ['*'], @@ -280,5 +293,19 @@ describe('parseObjectToMongoObjectForCreate', () => { expect(output.double).toBe(Number.MAX_VALUE); done(); }); +}); +describe('transformUpdate', () => { + it('removes Relation types', (done) => { + var input = { + aRelation: { __type: 'Relation', className: 'Stuff' }, + }; + var output = transform.transformUpdate(null, input, { + fields: { + aRelation: { __type: 'Relation', className: 'Stuff' }, + }, + }); + expect(output).toEqual({}); + done(); + }); }); diff --git a/src/Adapters/Storage/Mongo/MongoTransform.js b/src/Adapters/Storage/Mongo/MongoTransform.js index f3de0b440b..0b968047ad 100644 --- a/src/Adapters/Storage/Mongo/MongoTransform.js +++ b/src/Adapters/Storage/Mongo/MongoTransform.js @@ -319,6 +319,9 @@ const parseObjectToMongoObjectForCreate = (className, restCreate, schema) => { restCreate = addLegacyACL(restCreate); let mongoCreate = {} for (let restKey in restCreate) { + if (restCreate[restKey] && restCreate[restKey].__type === 'Relation') { + continue; + } let { key, value } = parseObjectKeyValueToMongoObjectKeyValue( restKey, restCreate[restKey], @@ -359,6 +362,9 @@ const transformUpdate = (className, restUpdate, parseFormatSchema) => { } } for (var restKey in restUpdate) { + if (restUpdate[restKey] && restUpdate[restKey].__type === 'Relation') { + continue; + } var out = transformKeyValueForUpdate(className, restKey, restUpdate[restKey], parseFormatSchema); // If the output value is an object with any $ keys, it's an