digitalsadhu / loopback-component-jsonapi

JSONAPI support for loopback.
MIT License
101 stars 32 forks source link

handle hasAndBelongsToMany relations on POST #114

Open behnoodk opened 8 years ago

behnoodk commented 8 years ago

When adding a record through POST which has a hasAndBelongsToMany with another model, the component is acting strange. I'm thinking even if this is not supported yet, it should not behave this way:

//loopback models/article.json
{
  "name": "article",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    //....
  },
  "validations": [],
  "relations": {
    "category": {
      "type": "belongsTo",
      "model": "category",
      "foreignKey": ""
    },
    "tags": {
      "type": "hasAndBelongsToMany",
      "model": "tag"
    }
  },
  "acls": [],
  "methods": {}
}
//Request sent by Ember JSONAdapter
POST /api/articles
{
  "data": {
    "attributes": {
      "title": "test",
      "slug": "test",
      "date": "Thu Feb 18 2016 17:03:20 GMT+0000",
      "text": "<p>test</p>",
      "posterimageurl": "test"
    },
    "relationships": {
      "tags": {
        "data": [{
          "type": "tags",
          "id": "4kDzGcCcl"
        }, {
          "type": "tags",
          "id": "56c5dbd15b282680a6d89ab0"
        }]
      },
      "category": {
        "data": {
          "type": "categories",
          "id": "41Mi5B35x"
        }
      }
    },
    "type": "articles"
  }
}
//response given by loopback
{
  "data": {
    "type": "articles",
    "relationships": {
      "category": {
        "links": {
          "related": "http://dev:3000/api/articles/NJDrKxyox/category"
        },
        "data": {
          "type": "categories",
          "id": "41Mi5B35x"
        }
      },
      "tags": {
        "links": {
          "related": "http://dev:3000/api/articles/NJDrKxyox/tags"
        }
      }
    },
    "id": "NJDrKxyox",
    "attributes": {
      "title": "test",
      "slug": "test",
      "date": "2016-02-18T17:03:20.000Z",
      "text": "<p>test</p>",
      "posterimageurl": "/api/containers/common/download/179.41eLgvacx.jpg"
    },
    "links": {
      "self": "http://dev:3000/api/articles/NJDrKxyox"
    }
  }
}

So loopback handles the belongsTo relationship correctly. but the hasAndBelongsToMany is behaving strange. first there is nothing in the response and second when I check the db, it has updated the field articleId of all the included tags to the id of created article. and when a new article is created, this articleId gets overwritten for all the tags provided in the request.

digitalsadhu commented 8 years ago

Yea there are definitely some issues with hasandbelongstomany. Also any use of "through"

We ideally need to start with some failing tests and take it from there.

Do you have any time to do this?

digitalsadhu commented 8 years ago

Ps thanks for reporting! (and sorry for the slow reply)

behnoodk commented 8 years ago

I had some other issues with loopback and decided to write my API from scratch with express since it's a small API. Unfortunately I do not have time to do this. I think at the very least it would be good to mention this in the README. this component does a great job for all belongsTo relationships, but acts unexpectedly for other types of relationships.

thedanheller commented 6 years ago

@behnoodk how do you send this kind of request? I can only send with the attributes content as body.