json-api-serializer icon indicating copy to clipboard operation
json-api-serializer copied to clipboard

add data option on relationships

Open danivek opened this issue 5 years ago • 1 comments

Add a data option function that returns the data to serialize for the relationship.

Example: Input data can contains meta information ( e.g. total) on data for a relationship.

{
  "id": "1",
  "title": "article 1",
  "comments": {
    "total": 2,
    "data": [
      {
        "id": "1",
        "title": "comment 1"
      },
      {
        "id": "2",
        "title": "comment 2"
      }
    ]
  }
}

Add a data option as a function to return data to serialize for the relationship:

const Serializer = new JSONAPISerializer();

Serializer.register('article', {
  relationships: {
    comments: {
      type: 'comment',
      data: (input) => input.data
    }
  }
});
Serializer.register('comment');

danivek avatar Oct 12 '20 09:10 danivek

Coverage Status

Coverage remained the same at 100.0% when pulling cc9e83d57d346aece3c48a4b226b0a735c052fa9 on add-data-options-for-relationships into b016d08718f08d08df9becc3e53b73d3fb44ce41 on master.

coveralls avatar Oct 12 '20 09:10 coveralls