From a770c4cba86a5e2e9b8b29e768ca35cc9eb0bff2 Mon Sep 17 00:00:00 2001 From: Florent Vilmart Date: Fri, 8 Apr 2016 07:42:02 -0400 Subject: [PATCH 1/3] 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 a5eeffbfc1f..09336a556e5 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 896df51fe29..4a47f3598fe 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 f2f188cb574..7a6710b9e99 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 51627af45fc..eba2ea16b50 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'; From ab6925a5e2cc26e9ab04a1f6f1c93979ee31c43f Mon Sep 17 00:00:00 2001 From: Florent Vilmart Date: Fri, 8 Apr 2016 08:02:54 -0400 Subject: [PATCH 2/3] Update .travis.yml (#1428) Adds regular expression to match tags (yes, match tag in branches...) Tags should follow: 1.0.0 or 1.0.0-some --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index b1fd440681e..6c79fd16cf7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,6 +10,7 @@ env: branches: only: - master + - /^[0-9].[0-9].[0-9](-.*)?$/ cache: directories: - "$HOME/.mongodb/versions/downloads" From 30197a7d84a328e9df8c68441f106b0713bd578e Mon Sep 17 00:00:00 2001 From: Steven Shipton Date: Fri, 8 Apr 2016 18:00:16 +0100 Subject: [PATCH 3/3] Expose DatabaseAdapter to simplify application tests (#1121) * Move helper.clearData to DatabaseAdapter. Expose DatabaseAdapter in index * fix indentation * Export DatabaseAdapter in index.js * Rename clearData to destroyAllDataPermanently. Only export destroyAllDataPermanently from DatabaseAdapter. Update helper * Expose wrapped TestUtils from index.js. TestUtils exposed select functions from other modules, only in test environment --- spec/helper.js | 11 ++--------- src/DatabaseAdapter.js | 13 +++++++++++++ src/TestUtils.js | 15 +++++++++++++++ src/index.js | 3 ++- 4 files changed, 32 insertions(+), 10 deletions(-) create mode 100644 src/TestUtils.js diff --git a/spec/helper.js b/spec/helper.js index c134f339ab0..03ddff97435 100644 --- a/spec/helper.js +++ b/spec/helper.js @@ -8,6 +8,7 @@ var express = require('express'); var facebook = require('../src/authDataManager/facebook'); var ParseServer = require('../src/index').ParseServer; var path = require('path'); +var TestUtils = require('../src/index').TestUtils; var databaseURI = process.env.DATABASE_URI; var cloudMain = process.env.CLOUD_CODE_MAIN || './spec/cloud/main.js'; @@ -88,7 +89,7 @@ beforeEach(function(done) { afterEach(function(done) { Parse.User.logOut().then(() => { - return clearData(); + return TestUtils.destroyAllDataPermanently(); }).then(() => { done(); }, (error) => { @@ -232,14 +233,6 @@ function mockFacebook() { return facebook; } -function clearData() { - var promises = []; - for (var conn in DatabaseAdapter.dbConnections) { - promises.push(DatabaseAdapter.dbConnections[conn].deleteEverything()); - } - return Promise.all(promises); -} - // This is polluting, but, it makes it way easier to directly port old tests. global.Parse = Parse; global.TestObject = TestObject; diff --git a/src/DatabaseAdapter.js b/src/DatabaseAdapter.js index 51403ba3cf1..3afdf1364ef 100644 --- a/src/DatabaseAdapter.js +++ b/src/DatabaseAdapter.js @@ -49,6 +49,18 @@ function clearDatabaseSettings() { appDatabaseOptions = {}; } +//Used by tests +function destroyAllDataPermanently() { + if (process.env.TESTING) { + var promises = []; + for (var conn in dbConnections) { + promises.push(dbConnections[conn].deleteEverything()); + } + return Promise.all(promises); + } + throw 'Only supported in test environment'; +} + function getDatabaseConnection(appId: string, collectionPrefix: string) { if (dbConnections[appId]) { return dbConnections[appId]; @@ -71,5 +83,6 @@ module.exports = { setAppDatabaseOptions: setAppDatabaseOptions, setAppDatabaseURI: setAppDatabaseURI, clearDatabaseSettings: clearDatabaseSettings, + destroyAllDataPermanently: destroyAllDataPermanently, defaultDatabaseURI: databaseURI }; diff --git a/src/TestUtils.js b/src/TestUtils.js new file mode 100644 index 00000000000..ebdb9f99143 --- /dev/null +++ b/src/TestUtils.js @@ -0,0 +1,15 @@ +import { destroyAllDataPermanently } from './DatabaseAdapter'; + +let unsupported = function() { + throw 'Only supported in test environment'; +}; + +let _destroyAllDataPermanently; +if (process.env.TESTING) { + _destroyAllDataPermanently = destroyAllDataPermanently; +} else { + _destroyAllDataPermanently = unsupported; +} + +export default { + destroyAllDataPermanently: _destroyAllDataPermanently}; diff --git a/src/index.js b/src/index.js index 7c453454680..39075d50e1c 100644 --- a/src/index.js +++ b/src/index.js @@ -2,6 +2,7 @@ import winston from 'winston'; import ParseServer from './ParseServer'; import S3Adapter from 'parse-server-s3-adapter' import FileSystemAdapter from 'parse-server-fs-adapter' +import TestUtils from './TestUtils'; import { useExternal } from './deprecated' // Factory function @@ -15,4 +16,4 @@ _ParseServer.createLiveQueryServer = ParseServer.createLiveQueryServer; let GCSAdapter = useExternal('GCSAdapter', 'parse-server-gcs-adapter'); export default ParseServer; -export { S3Adapter, GCSAdapter, FileSystemAdapter, _ParseServer as ParseServer }; +export { S3Adapter, GCSAdapter, FileSystemAdapter, TestUtils, _ParseServer as ParseServer };