jsonapi-serializer
jsonapi-serializer copied to clipboard
#143 Allow meta for each included resource object
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"
}
}
]
}
Hi @SeyZ any estimate when would this change be released?