Skip to content

Commit

Permalink
Merge branch 'master' into clean-up-schema
Browse files Browse the repository at this point in the history
  • Loading branch information
drew-gross committed Apr 8, 2016
2 parents ae08b2a + 30197a7 commit b16b84b
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 10 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ env:
branches:
only:
- master
- /^[0-9].[0-9].[0-9](-.*)?$/
cache:
directories:
- "$HOME/.mongodb/versions/downloads"
Expand Down
29 changes: 29 additions & 0 deletions spec/ParseAPI.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
})
})

});
11 changes: 2 additions & 9 deletions spec/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -88,7 +89,7 @@ beforeEach(function(done) {

afterEach(function(done) {
Parse.User.logOut().then(() => {
return clearData();
return TestUtils.destroyAllDataPermanently();
}).then(() => {
done();
}, (error) => {
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions spec/transform.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ var dummySchema = {
return 'geopoint';
}
return;
},
getRelationFields: function() {
return {}
}
};

Expand Down
13 changes: 13 additions & 0 deletions src/DatabaseAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -71,5 +83,6 @@ module.exports = {
setAppDatabaseOptions: setAppDatabaseOptions,
setAppDatabaseURI: setAppDatabaseURI,
clearDatabaseSettings: clearDatabaseSettings,
destroyAllDataPermanently: destroyAllDataPermanently,
defaultDatabaseURI: databaseURI
};
18 changes: 18 additions & 0 deletions src/Schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,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.
Expand Down
15 changes: 15 additions & 0 deletions src/TestUtils.js
Original file line number Diff line number Diff line change
@@ -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};
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 };
5 changes: 5 additions & 0 deletions src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit b16b84b

Please sign in to comment.