Sorry to bother you again Michael, but thereās still something not quite āclickingā in my head (probably healthy if youād take it literally :-D)
Although I can see how the email addresses are relationships to the resource object the way I modelled it, there is the practical issue that in my application they are not stored separately (nor can they, nor do I want to). Iām using MongoDB as my datastore and Iām utilizing itās feature of embedded documents to store the email addresses with the particular person. I know the spec is meant to be agnostic to these kind of details, but I feel I have to mention it in order to explain the problem Iām having.
If Iād choose the route of treating the email addresses as relationships, creating a person would require the POST to look something like:
POST /persons HTTP/1.1
Content-Type: application/vnd.api+json
Accept: application/vnd.api+json
{
"data": {
"type": "persons",
"relationships" {
"emailAddresses":
"data": [
{
"type": "emailAddresses",
"id": "1"
},
{
"type": "emailAddresses",
"id": "2"
},
]
}
}
}
Now, that would be fine in a situation where emailAddresses.id = 1 and emailAddresses.id = 2 actually exist. But as explained above, they do not exist, because they are created at the same time as the resource.
So I am kind of back to the idea that the email addresses are in fact attributes to the resource object, but they should not appear as an array in the JSON doc, even though thatās how they are stored. The only problem left in that case, is how to name those attribute fields in order to be able to identify them for deletion/updating. I was thinking of the solution below, which may not win a beauty contest, but as far as I can see does the job without breaking the rules of the spec:
GET /persons/ae1212145fsd HTTP/1.1
Content-Type: application/vnd.api+json
Accept: application/vnd.api+json
Response:
{
"data": {
"type": "persons",
"id": "ae1212145fsd",
"attributes" {
"emailAddresses": {
"email1@domain.com": "email1@domain.com",
"email2@domain.com": "email2@domain.com"
}
}
}
}
Then to update/delete
PATCH /persons/ae1212145fsd HTTP/1.1
Content-Type: application/vnd.api+json
Accept: application/vnd.api+json
{
"data": {
"type": "persons",
"id": "ae1212145fsd",
"attributes" {
emailAddresses: {
"email1@domain.com": null //to delete, or else the updated e-mail address
}
}
}
Does this argumentation make sense, or am I missing something?
Thanks again!