Skip to content

Commit

Permalink
Introduce the ignoreRelationshipData
Browse files Browse the repository at this point in the history
  • Loading branch information
SeyZ committed Oct 16, 2015
1 parent bfaf3b0 commit a4e2909
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 15 deletions.
23 changes: 11 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,19 @@ API](http://jsonapi.org) (1.0 compliant).
- *topLevelLinks*: An object that describes the top-level links. Values can be *string* or a *function* (see examples below)
- *dataLinks*: An object that describes the links inside data. Values can be *string* or a *function* (see examples below)
- *relationshipLinks*: An object that describes the links inside relationships. Values can be *string* or a *function* (see examples below)
- *keyForAttribute*: A function or string to customize attributes. Functions are passed the attribute as a single argument and expect a string to be returned. Strings are aliases for inbuilt functions for common case conversions. Options include:
- dash-case (default)
- lisp-case
- spinal-case
- kebab-case
- underscore_case
- snake_case
- CamelCase
- camelCase
- *ignoreRelationshipData*: Do not include the `data` key inside the relationship. Default: false.
- *keyForAttribute*: A function or string to customize attributes. Functions are passed the attribute as a single argument and expect a string to be returned. Strings are aliases for inbuilt functions for common case conversions. Options include:
- dash-case (default)
- lisp-case
- spinal-case
- kebab-case
- underscore_case
- snake_case
- CamelCase
- camelCase
- *pluralizeType*: A boolean to indicate if the type must be pluralized or not. Default: true.
- *typeForAttribute*: A function that maps the attribute (passed as an argument) to the type you want to override. Option *pluralizeType* ignored if set.
- *meta*: An object to include non-standard meta-information.


- *meta*: An object to include non-standard meta-information.

## Examples

Expand Down
6 changes: 5 additions & 1 deletion browserify/jsonapi-serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,11 @@ module.exports = function (collectionName, record, payload, opts) {
data = that.serializeRef(current[attribute], current, attribute, opts);
}

dest.relationships[keyForAttribute(attribute)] = { data: data };
dest.relationships[keyForAttribute(attribute)] = {};
if (!opts.ignoreRelationshipData) {
dest.relationships[keyForAttribute(attribute)].data = data;
}

if (opts.relationshipLinks) {
dest.relationships[keyForAttribute(attribute)].links =
getLinks(current[attribute], opts.relationshipLinks);
Expand Down
6 changes: 5 additions & 1 deletion lib/serializer-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,11 @@ module.exports = function (collectionName, record, payload, opts) {
data = that.serializeRef(current[attribute], current, attribute, opts);
}

dest.relationships[keyForAttribute(attribute)] = { data: data };
dest.relationships[keyForAttribute(attribute)] = {};
if (!opts.ignoreRelationshipData) {
dest.relationships[keyForAttribute(attribute)].data = data;
}

if (opts.relationshipLinks) {
dest.relationships[keyForAttribute(attribute)].links =
getLinks(current[attribute], opts.relationshipLinks);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jsonapi-serializer",
"version": "2.0.3",
"version": "2.0.4",
"description": "A Node.js framework agnostic library for serializing your data to JSON API",
"main": "index.js",
"scripts": {
Expand Down
43 changes: 43 additions & 0 deletions test/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1325,4 +1325,47 @@ describe('JSON API Serializer', function () {
done(null, json);
});
});

describe('ignoreRelationshipData on a relationship', function () {
it('should not contains the data key', function (done) {
var dataSet = [{
id: '54735750e16638ba1eee59cb',
firstName: 'Sandro',
lastName: 'Munda',
address: {
id: '54735722e16620ba1eee36af',
addressLine1: '406 Madison Court',
zipCode: '49426',
country: 'USA'
},
}, {
id: '5490143e69e49d0c8f9fc6bc',
firstName: 'Lawrence',
lastName: 'Bennett',
address: {
id: '54735697e16624ba1eee36bf',
addressLine1: '361 Shady Lane',
zipCode: '23185',
country: 'USA'
}
}];

var json = new JsonApiSerializer('users', dataSet, {
attributes: ['firstName', 'lastName', 'address'],
address: {
ref: 'id',
attributes: [],
included: false,
ignoreRelationshipData: true,
relationshipLinks: {
related: '/foo/bar'
}
}
});

expect(json.data[0].relationships.address).to.not.have.key('data');
expect(json.data[1].relationships.address).to.not.have.key('data');
done(null, json);
});
});
});

0 comments on commit a4e2909

Please sign in to comment.