Skip to content

Commit

Permalink
Merge pull request #234 from rocknrolla777/relationships-in-include
Browse files Browse the repository at this point in the history
Relationships in include
  • Loading branch information
digitalsadhu authored Jul 8, 2017
2 parents d2130d7 + 4f63728 commit ae2dafb
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 8 deletions.
31 changes: 26 additions & 5 deletions lib/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function defaultSerialize (options, cb) {
resultData,
options.requestedIncludes,
options.relationships,
options.app
options
)
} catch (err) {
cb(err)
Expand Down Expand Up @@ -344,7 +344,11 @@ function makeLinks (links, item) {
* @throws {Error}
* @return {undefined}
*/
function handleIncludes (resp, includes, relations, app) {
function handleIncludes (resp, includes, relations, options) {
var app = options.app

relations = utils.setIncludedRelations(relations, app)

var resources = _.isArray(resp.data) ? resp.data : [resp.data]

if (typeof includes === 'string') {
Expand All @@ -360,6 +364,7 @@ function handleIncludes (resp, includes, relations, app) {
var embedded = resources.map(function subsituteEmbeddedForIds (resource) {
return includes.map(function (include) {
var relation = relations[include]
var includedRelations = relations[include].relations
var propertyKey = relation.keyFrom
var plural = ''
if (relation.polymorphic && utils.relationFkOnModelFrom(relation)) {
Expand Down Expand Up @@ -392,7 +397,9 @@ function handleIncludes (resp, includes, relations, app) {
rel,
propertyKey,
relation.keyTo,
plural
plural,
includedRelations,
options
)
})
embeds = _.compact(embeds)
Expand All @@ -410,7 +417,9 @@ function handleIncludes (resp, includes, relations, app) {
rel,
propertyKey,
relation.keyFrom,
plural
plural,
includedRelations,
options
)

resource.relationships[include].data = {
Expand Down Expand Up @@ -456,12 +465,24 @@ function handleIncludes (resp, includes, relations, app) {
* @memberOf {Serializer}
* @param {Object} relationship
* @param {String} key
* @param {String} fk
* @param {String} type
* @param {Object} includedRelations
* @param {Object} options
* @return {Object}
*/
function createCompoundIncludes (relationship, key, fk, type) {
function createCompoundIncludes (relationship, key, fk, type, includedRelations, options) {
var compoundInclude = makeRelation(type, String(relationship[key]))

if (options && !_.isEmpty(includedRelations)) {
var defaultModelPath = options.modelPath
options.modelPath = type

compoundInclude.relationships = parseRelations(relationship, includedRelations, options)

options.modelPath = defaultModelPath
}

// remove the id key since its part of the base compound document, not part of attributes
delete relationship[key]
delete relationship[fk]
Expand Down
13 changes: 12 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ module.exports = {
primaryKeyForModel: primaryKeyForModel,
shouldNotApplyJsonApi: shouldNotApplyJsonApi,
shouldApplyJsonApi: shouldApplyJsonApi,
relationFkOnModelFrom: relationFkOnModelFrom
relationFkOnModelFrom: relationFkOnModelFrom,
setIncludedRelations: setIncludedRelations
}

function primaryKeyForModel (model) {
Expand Down Expand Up @@ -227,3 +228,13 @@ function shouldNotApplyJsonApi (ctx, options) {
function relationFkOnModelFrom (relation) {
return relation.type === 'belongsTo' || relation.type === 'referencesMany'
}

function setIncludedRelations (relations, app) {
for (var key in relations) {
if (relations.hasOwnProperty(key)) {
var name = (relations[key].modelTo && relations[key].modelTo.sharedClass.name) || relations[key].name
relations[key].relations = app.models[name] && app.models[name].relations
}
}
return relations
}
20 changes: 18 additions & 2 deletions test/hasMany.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,8 @@ describe('loopback json api hasMany relationships', function () {
expect(res.body.included[0]).to.have.all.keys(
'type',
'id',
'attributes'
'attributes',
'relationships'
)
expect(res.body.included[0].type).to.equal('comments')
expect(res.body.included[0].id).to.equal('1')
Expand Down Expand Up @@ -275,6 +276,13 @@ describe('loopback json api hasMany relationships', function () {
attributes: {
title: 'My comment',
comment: 'My comment text'
},
relationships: {
replies: {
links: {
related: res.body.included[0].relationships.replies.links.related
}
}
}
})
expect(res.body.included[1]).to.deep.equal({
Expand All @@ -283,6 +291,13 @@ describe('loopback json api hasMany relationships', function () {
attributes: {
title: 'My second comment',
comment: 'My second comment text'
},
relationships: {
replies: {
links: {
related: res.body.included[1].relationships.replies.links.related
}
}
}
})
done()
Expand Down Expand Up @@ -368,7 +383,8 @@ describe('loopback json api hasMany relationships', function () {
expect(res.body.included[0]).to.have.all.keys(
'type',
'id',
'attributes'
'attributes',
'relationships'
)
expect(res.body.included[0].type).to.equal('comments')
expect(res.body.included[0].id).to.equal('1')
Expand Down
29 changes: 29 additions & 0 deletions test/hasManyRelationships.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ describe('loopback json api hasMany relationships', function () {
attributes: {
firstName: 'Joe',
lastName: 'Shmoe'
},
relationships: {
posts: {
links: {
related: res.body.included[0].relationships.posts.links.related
}
}
}
})
expect(res.body.included[1]).to.deep.equal({
Expand All @@ -112,6 +119,17 @@ describe('loopback json api hasMany relationships', function () {
attributes: {
title: 'My comment',
comment: 'My comment text'
},
relationships: {
post: {
data: {
id: 1,
type: 'posts'
},
links: {
related: res.body.included[1].relationships.post.links.related
}
}
}
})
expect(res.body.included[2]).to.deep.equal({
Expand All @@ -120,6 +138,17 @@ describe('loopback json api hasMany relationships', function () {
attributes: {
title: 'My second comment',
comment: 'My second comment text'
},
relationships: {
post: {
data: {
id: 1,
type: 'posts'
},
links: {
related: res.body.included[2].relationships.post.links.related
}
}
}
})
done()
Expand Down

0 comments on commit ae2dafb

Please sign in to comment.