scoutforpets / jsonapi-mapper

JSON API-Compliant Serialization for your Node ORM
The Unlicense
42 stars 24 forks source link

Map a model with deep (two levels) of has-many nested relationship #50

Closed npatmaja closed 8 years ago

npatmaja commented 8 years ago

Hi, I have a model that has a has many relationship that also has a has many relationship

const A = bookshelf.Model.extend({
  b() {
    return this.hasMany(B);
  }
});

const B = bookshelf.Model.extend({
  c() {
    return this.hasMany(C);
  }
});

const C = bookshelf.Model.extend({});

// Queries
A.forge().fetch({ withRelated: ['b.c'] });

When I map the resulting model with jsonapi-mapper, it returns only the first level of the relationship (A has many B). Is this functionality not supported yet?

note: After looking at the source code, it seems that the function mapRelations works only for model as type Collection does not have property relations (it has property models tho), please do correct me if I'm wrong.

ShadowManu commented 8 years ago

This behavior was initially tried to be solved on #40, and it has included some tests, particularly this one that I think touches the topic you mention. However, the solution did not completely solve the problem and its a WIP in PR #47. I'll keep working on that PR from next week when I have time available. Of course, you are more than welcome to contribute, specially on providing failing test cases ;)

npatmaja commented 8 years ago

Hi @ShadowManu thanks for your answer, I'll look at my case and try to create a test case (or two) out of that :)

ShadowManu commented 8 years ago

This has been solved on #47 and I'll go ahead of release and close this.