From 75a4ca556464a49f9842b64489b691f9f66fb757 Mon Sep 17 00:00:00 2001 From: Florent Vilmart Date: Thu, 7 Apr 2016 23:09:22 -0400 Subject: [PATCH] Adds relation fields to objects --- spec/ParseAPI.spec.js | 29 +++++++++++++++++++++++++++++ spec/transform.spec.js | 3 +++ src/Schema.js | 18 ++++++++++++++++++ src/transform.js | 5 +++++ 4 files changed, 55 insertions(+) diff --git a/spec/ParseAPI.spec.js b/spec/ParseAPI.spec.js index a5eeffbfc1..09336a556e 100644 --- a/spec/ParseAPI.spec.js +++ b/spec/ParseAPI.spec.js @@ -1271,4 +1271,33 @@ describe('miscellaneous', function() { }); }); + it('gets relation fields', (done) => { + let object = new Parse.Object('AnObject'); + let relatedObject = new Parse.Object('RelatedObject'); + Parse.Object.saveAll([object, relatedObject]).then(() => { + object.relation('related').add(relatedObject); + return object.save(); + }).then(() => { + let headers = { + 'Content-Type': 'application/json', + 'X-Parse-Application-Id': 'test', + 'X-Parse-REST-API-Key': 'rest' + }; + let requestOptions = { + headers: headers, + url: 'http://localhost:8378/1/classes/AnObject', + json: true + }; + request.get(requestOptions, (err, res, body) => { + expect(body.results.length).toBe(1); + let result = body.results[0]; + expect(result.related).toEqual({ + __type: "Relation", + className: 'RelatedObject' + }) + done(); + }); + }) + }) + }); diff --git a/spec/transform.spec.js b/spec/transform.spec.js index 896df51fe2..4a47f3598f 100644 --- a/spec/transform.spec.js +++ b/spec/transform.spec.js @@ -13,6 +13,9 @@ var dummySchema = { return 'geopoint'; } return; + }, + getRelationFields: function() { + return {} } }; diff --git a/src/Schema.js b/src/Schema.js index f2f188cb57..7a6710b9e9 100644 --- a/src/Schema.js +++ b/src/Schema.js @@ -634,6 +634,24 @@ class Schema { } return false; }; + + getRelationFields(className) { + if (this.data && this.data[className]) { + let classData = this.data[className]; + return Object.keys(classData).filter((field) => { + return classData[field].startsWith('relation'); + }).reduce((memo, field) => { + let type = classData[field]; + let className = type.slice('relation<'.length, type.length - 1); + memo[field] = { + __type: 'Relation', + className: className + }; + return memo; + }, {}); + } + return {}; + } } // Returns a promise for a new Schema. diff --git a/src/transform.js b/src/transform.js index 51627af45f..eba2ea16b5 100644 --- a/src/transform.js +++ b/src/transform.js @@ -732,6 +732,11 @@ function untransformObject(schema, className, mongoObject, isNestedObject = fals mongoObject[key], true); } } + + if (!isNestedObject) { + let relationFields = schema.getRelationFields(className); + Object.assign(restObject, relationFields); + } return restObject; default: throw 'unknown js type';