Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds relation fields to objects #1424

Merged
merged 1 commit into from
Apr 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
})
})

});
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() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you leave this in by accident?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, we need it on the dummy schema otherwise the function is missing when running the tests

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah OK.

return {}
}
};

Expand Down
18 changes: 18 additions & 0 deletions src/Schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
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