SeyZ / jsonapi-serializer

A Node.js framework agnostic library for (de)serializing your data to JSON API

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How can I build deep relationships with my serializer

zubeirom opened this issue · comments

Here is what I am trying to do:

return new JSONAPISerializer('post', {
      id: 'fe_id',
      attributes: ['channel_id', 'title', 'description', 'thumbnail_url', 'video_url', 'views', 'createdAt', 'channel', 'upvotes', 'comments'],
      channel: {
        ref: 'id',
        attributes: ['channelname', 'firstname', 'lastname', 'biography', 'image_url']
      },
      upvotes: {
        ref: 'id', 
        attributes: ['post_id', 'channel_id']
      },
      comments: {
        ref: 'id',
        attributes: ['post_id', 'channel_id', 'comment', 'channel'],
        channel: {
          ref: 'id',
          attributes: ['channelname', 'firstname', 'lastname', 'biography', 'image_url']
        }
      }
    }).serialize(data);

So basically putting a relationship within a relationship. It does not work, the response does not contain the data. What can I do, and is it possible ?

I believe it should be possible. Check out the test for it here:

describe('Nested of nested document', function () {
it('should be serialized', function (done) {
var dataSet = {
_id: '54735750e16638ba1eee59cb',
firstName: 'Sandro',
lastName: 'Munda',
foo: {
bar: {
firstName: 'Lawrence',
lastName: 'Bennett',
barbar: {
firstName: 'Peter',
lastName: 'Forney'
}
}
}
};
var json = new JSONAPISerializer('users', {
id: '_id',
attributes: ['_id', 'firstName', 'lastName', 'foo'],
keyForAttribute: 'underscore_case',
foo: {
attributes: ['bar'],
bar: {
attributes: ['firstName', 'lastName', 'barbar'],
barbar: {
attributes: ['firstName']
}
}
}
}).serialize(dataSet);
expect(json.data).to.have.property('attributes').that.is
.an('object')
.eql({
_id: '54735750e16638ba1eee59cb',
'first_name': 'Sandro',
'last_name': 'Munda',
foo: {
bar: {
'first_name': 'Lawrence',
'last_name': 'Bennett',
barbar: {
'first_name': 'Peter'
}
}
}
});
done(null, json);
});
});
.

I can't see anything you're doing wrong in your code though, perhaps post the data you're trying to serialize as well?