Reputation: 319
I am doing a REST web server in nodejs and I was thinking to implement it using Collection+JSON as hypermedia-type.
However I did not understand if it is possible to represent in the collection 1 to n relationships. So if for example I have a collection Basket it is possible to make the items inside Basket to point to item of other defined collection?
Upvotes: 1
Views: 235
Reputation: 697
Using the traditional ecommerce example, here's how a collection-JSON endpoint for a product might be structured. Note that your client should group by rel-tag. Your rel tag list is your "contract" with your clients. "This rel tag always means that."
"collection":
{
"href": "/products/1234.json",
"items":
[
{
"name": "name",
"value": "10 Speed Bike"
}
{
"name": "manufacturer",
"value": "Schwin"
}
{
"name": "price",
"value": 150.00
}
],
"links":
[
{
"href": "/products/1234/attachments/flier.pdf",
"rel": "attachment",
"prompt": "Flier from the Manufacturer",
"render": "link"
},
{
"href": "/products/1234/attachments/specs.pdf",
"rel": "attachment",
"prompt": "Spec Sheet from Manufacturer",
"render": "link"
},
{
"href": "/products/1234/attachments/warranty.pdf",
"rel": "attachment",
"prompt": "Limited Lifetime Warranty",
"render": "link"
},
{
"href": "/store/attachments/returns.pdf",
"rel": "attachment",
"prompt": "Our Return Policy",
"render": "link"
},
{
"href": "/images/1235478324.png",
"rel": "product-image",
"prompt": "A Schwin 10-speed bike in red",
"render": "image"
},
{
"href": "/images/1235478355.png",
"rel": "product-image",
"prompt": "A girl riding a 10-speed bike.",
"render": "image"
},
{
"href": "/images/1235478385.png",
"rel": "product-image",
"prompt": "A grandma watching grandkids ride bicycles.",
"render": "image"
},
{
"href": "/images/1235478545.png",
"rel": "swatch",
"prompt": "Radiant Red",
"render": "image"
},
{
"href": "/images/1235478545.png",
"rel": "swatch",
"prompt": "Brilliant Blue",
"render": "image"
},
{
"href": "/images/1235478588.png",
"rel": "swatch",
"prompt": "Stunning Silver",
"render": "image"
},
{
"href": "/blog/2014/07/26",
"rel": "content",
"prompt": "Our store now sells Schwin bicycles!",
"render": "link"
},
{
"href": "/manufacturers/schwin",
"rel": "manufacturer",
"prompt": "Schwin",
"render": "link"
}, ]
}
In this example, the client would know that the presence of a "swatch" rel-tag means that it should show the color swatches section... one swatch for every "rel-tag." Your client might even render differently if there is 1 swatch vs many.
You could be forgiven for wanting to extend collection+JSON ( I've extended it extensively ). collection+JSON's verbose syntax, which I initially saw as a problem, in fact provides a great way to extend while maintaining backwards-compatibility.
Upvotes: 0
Reputation: 4093
You can use the collection
link relation, which is described as:
Refers to a resource which represents a collection of which the current resource is a member.
This seems to meet your need.
Upvotes: 0