loopback-component-jsonapi icon indicating copy to clipboard operation
loopback-component-jsonapi copied to clipboard

Relationships

Open digitalsadhu opened this issue 10 years ago • 2 comments

Description

This is the big feature we still need to support. The current way to support these relationships is going to have to be to overwrite the remoting relationship methods in the Model class. (loopback repo, lib/model.js)

  • hasOneRemoting
  • hasManyRemoting
  • belongsToRemoting
  • ~~hasManyThroughRemoting~~ <-- will not be supported in first v1 (enough work to do already and this ones going to be hard)

This isn't ideal as it's reaching pretty far into the loopback internals. Plan should be to get it done this way and then run it past the strongloop guys eg. @bajtos to see if a better way can be found to do this

Explanation

Has One (PATCH ONLY)

Replace the existing resource with PATCH as follows

PATCH /articles/1/relationships/author HTTP/1.1
Content-Type: application/vnd.api+json
Accept: application/vnd.api+json

{
  "data": { "type": "people", "id": "12" }
}

Remove the existing resource with PATCH as follows

PATCH /articles/1/relationships/author HTTP/1.1
Content-Type: application/vnd.api+json
Accept: application/vnd.api+json

{
  "data": null
}

Has Many (PATCH, POST, DELETE)

We need to replace all relationships for a specific key with PATCH

Eg. The following completely replaces all tags for an article

PATCH /articles/1/relationships/tags HTTP/1.1
Content-Type: application/vnd.api+json
Accept: application/vnd.api+json

{
  "data": [
    { "type": "tags", "id": "2" },
    { "type": "tags", "id": "3" }
  ]
}

We need to remove all related resources for PATCH

Eg. The following completely removes all tags for an article

PATCH /articles/1/relationships/tags HTTP/1.1
Content-Type: application/vnd.api+json
Accept: application/vnd.api+json

{
  "data": []
}

We need to append with POST

Eg. The following will append comment 124 to the comments for article 1

POST /articles/1/relationships/comments HTTP/1.1
Content-Type: application/vnd.api+json
Accept: application/vnd.api+json

{
  "data": [
    { "type": "comments", "id": "123" }
  ]
}

We need to delete with a DELETE

Eg. The following removes comments 12 and 13 only from articles 1

DELETE /articles/1/relationships/comments HTTP/1.1
Content-Type: application/vnd.api+json
Accept: application/vnd.api+json

{
  "data": [
    { "type": "comments", "id": "12" },
    { "type": "comments", "id": "13" }
  ]
}

TODO

  • [ ] Expand test coverage to cover items below

GET /:collection/:id/:relatedCollection

  • [x] hasMany
  • [x] hasOne
  • [x] belongsTo

GET /:collection/:id/relationships/:relatedCollection

  • [x] hasMany
  • [x] hasOne
  • [x] belongsTo

POST /:collection/:id/relationships/:relatedCollection

  • [ ] hasMany
  • [ ] hasOne
  • [ ] belongsTo

PATCH /:collection/:id/relationships/:relatedCollection

  • [ ] hasMany
  • [ ] hasOne
  • [ ] belongsTo

DELETE /:collection/:id/relationships/:relatedCollection

  • [ ] hasMany
  • [ ] hasOne
  • [ ] belongsTo

digitalsadhu avatar Oct 26 '15 12:10 digitalsadhu

I've update this ticket and started work on tests.

digitalsadhu avatar Nov 13 '15 15:11 digitalsadhu

i think it's probably worth deprioritising this feature since as far as I can tell ember doesn't make use of relationship urls.

digitalsadhu avatar Jan 17 '16 18:01 digitalsadhu