jsonapi-serializer icon indicating copy to clipboard operation
jsonapi-serializer copied to clipboard

#143 Allow meta for each included resource object

Open xaviertholot opened this issue 8 years ago • 1 comments

This PR solves #143 by allowing a meta attributes to includes resources.

According to JSON-API specifications, an included object should be a resource object : http://jsonapi.org/format/#document-top-level

included: an array of resource objects that are related to the primary data and/or each other (“included resources”).

Resource objects are specified here : http://jsonapi.org/format/#document-resource-objects and can contain a meta attribute.

Example :

var JSONAPISerializer = require("jsonapi-serializer").Serializer;

var dataSet = [{
  id: '54735750e16638ba1eee59cb',
  firstName: 'Sandro',
  lastName: 'Munda',
  address: {
    addressLine1: '406 Madison Court',
    zipCode: '49426',
    country: 'USA'
  },
}, {
  id: '5490143e69e49d0c8f9fc6bc',
  firstName: 'Lawrence',
  lastName: 'Bennett',
  address: {
    addressLine1: '361 Shady Lane',
    zipCode: '23185',
    country: 'USA'
  }
}];

var json = new JSONAPISerializer('users', {
  id: 'id',
  attributes: ['firstName', 'lastName', 'address'],
  address: {
    ref: function(collection, field) {
      return collection.id + field.country + field.zipCode;
    },
    attributes: ['addressLine1', 'country', 'zipCode'],
    meta: {
      addressType: 'home'
    }
  }
}).serialize(dataSet);

Before, it returned :

{
  "data": [
    {
      "type": "users",
      "id": "54735750e16638ba1eee59cb",
      "attributes": {
        "first-name": "Sandro",
        "last-name": "Munda"
      },
      "relationships": {
        "address": {
          "data": {
            "type": "addresses",
            "id": "54735750e16638ba1eee59cbUSA49426"
          }
        }
      }
    },
    {
      "type": "users",
      "id": "5490143e69e49d0c8f9fc6bc",
      "attributes": {
        "first-name": "Lawrence",
        "last-name": "Bennett"
      },
      "relationships": {
        "address": {
          "data": {
            "type": "addresses",
            "id": "5490143e69e49d0c8f9fc6bcUSA23185"
          }
        }
      }
    }
  ],
  "included": [
    {
      "type": "addresses",
      "id": "54735750e16638ba1eee59cbUSA49426",
      "attributes": {
        "address-line1": "406 Madison Court",
        "country": "USA",
        "zip-code": "49426"
      }
    },
    {
      "type": "addresses",
      "id": "5490143e69e49d0c8f9fc6bcUSA23185",
      "attributes": {
        "address-line1": "361 Shady Lane",
        "country": "USA",
        "zip-code": "23185"
      }
    }
  ]
}

Now, it returns :

{
  "data": [
    {
      "type": "users",
      "id": "54735750e16638ba1eee59cb",
      "attributes": {
        "first-name": "Sandro",
        "last-name": "Munda"
      },
      "relationships": {
        "address": {
          "data": {
            "type": "addresses",
            "id": "54735750e16638ba1eee59cbUSA49426"
          }
        }
      }
    },
    {
      "type": "users",
      "id": "5490143e69e49d0c8f9fc6bc",
      "attributes": {
        "first-name": "Lawrence",
        "last-name": "Bennett"
      },
      "relationships": {
        "address": {
          "data": {
            "type": "addresses",
            "id": "5490143e69e49d0c8f9fc6bcUSA23185"
          }
        }
      }
    }
  ],
  "included": [
    {
      "type": "addresses",
      "id": "54735750e16638ba1eee59cbUSA49426",
      "attributes": {
        "address-line1": "406 Madison Court",
        "country": "USA",
        "zip-code": "49426"
      },
      "meta": {
        "addressType": "home"
      }
    },
    {
      "type": "addresses",
      "id": "5490143e69e49d0c8f9fc6bcUSA23185",
      "attributes": {
        "address-line1": "361 Shady Lane",
        "country": "USA",
        "zip-code": "23185"
      },
      "meta": {
        "addressType": "home"
      }
    }
  ]
}

xaviertholot avatar Oct 27 '17 12:10 xaviertholot

Hi @SeyZ any estimate when would this change be released?

lesterbp avatar Jul 31 '19 08:07 lesterbp