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

belongsTo through hasMany

Open BenjaminHorn opened this issue 10 years ago • 6 comments

I have the following models:

// model/rule.json
{
  "name": "Rule",
  ...
  "postgresql": {
    "schema": "public",
    "table": "rule"
  },
  "include": ["Action"],
  "properties": {
  ...
  },

  "relations": {
  ...
    "actions": {
      "type": "hasMany",
      "model": "Action",
      "foreignKey": "ruleId"
    },
  ...
  },
  "acls": [],
  "methods": {}
}

// model/action.json
{
  "name": "Action",
  ...
  "properties": {
   ...
    "cardId": {
      "type": "Number",
      "required": false,
      "index": true,
      "length": null,
      "precision": 32,
      "scale": 0,
      "postgresql": {
        "columnName": "card_id",
        "dataType": "integer",
        "dataLength": null,
        "dataPrecision": 32,
        "dataScale": 0,
        "nullable": "YES"
      },
      "_selectable": true
    }
  },
  "validations": [],
  "relations": {
    "card": {
      "type": "belongsTo",
      "model": "Card",
      "foreignKey": "cardId"
    }
  },
..
}

// models/card.json

{
  "name": "Card",
        ...
  "properties": {
        ...
  },
  "validations": [],
  "relations": {
    "galleries": {
        ...
    },
    "tags": {
        ...
    },
    "actions": {
      "type": "hasMany",
      "model": "Action",
      "foreignKey": "cardId"
    }

  },
  "acls": [],
  "methods": {}
}

In Ember I try to access a rule, loop through its actions, and show each action's card

Something like this: http://jsbin.com/taqatuzagu/1/edit?html,js,output

The problem: If I go to localhost:300/rules/1/actions it does not show any information about the card relationship. the object holds the rule's relationships.

{
  "data": [
    {
      "type": "actions",
      "relationships": {
        ...
        "actions": {
          "links": {
            "related": "http://localhost.hu:3000/api/Rules/1/actions"
          }
        },
        ...
      },
      "id": "1",
      "attributes": {
        "name": "welcome",
        "url": "",
        "type": "card",
        "json": "",
        "card-id": 2
      },
      "links": {
        "self": "http://localhost.hu:3000/api/Actions/1"
      }
    },
    {
      "type": "actions",
      "relationships": {
        ...
        "actions": {
          "links": {
            "related": "http://localhost.hu:3000/api/Rules/3/actions"
          }
        }
        ...
      },
      "id": "3",
      "attributes": {
        "name": "second",
        "url": "",
        "type": "summary",
        "json": "",
        "card-id": 2
      },
      "links": {
        "self": "http://localhost.hu:3000/api/Actions/1"
      }
    }
  ],
  "links": {
    "self": "http://localhost.hu:3000/api/Rules/1/actions"
  }
}

BenjaminHorn avatar Nov 19 '15 13:11 BenjaminHorn

Thanks for submitting! Will try to take a look at this over the weekend. Going to try and squash your other bugs reports first ;)

digitalsadhu avatar Nov 19 '15 14:11 digitalsadhu

Hi @BenjaminHorn,

It would be super awesome if you want to look into this, try looking in these places:

  • https://github.com/digitalsadhu/loopback-component-jsonapi/blob/master/lib/patch.js#L93
  • https://github.com/digitalsadhu/loopback-component-jsonapi/blob/master/lib/patch.js#L148
  • https://github.com/digitalsadhu/loopback-component-jsonapi/blob/master/lib/patch.js#L181

On those lines, variables this.relations and relation should give you some information about the relationships and relation.modelTo and relation.modelFrom gives you actual models to look at. relation.modelTo.relations should give you info about the related models relations.

I usually fire up node inspector and start poking around to find out what is what.

I am a bit suprised that it doesn't just work though so you could also take a look at the serializing. See: https://github.com/digitalsadhu/loopback-component-jsonapi/blob/master/lib/serialize.js#L103 and https://github.com/digitalsadhu/loopback-component-jsonapi/blob/master/lib/serializer.js#L111-L160

Let me know if you have any more questions.

digitalsadhu avatar Nov 20 '15 10:11 digitalsadhu

Fixed in #59

digitalsadhu avatar Nov 22 '15 09:11 digitalsadhu

I'm still experiencing this issue and don't understand if it was solved or not. I'm getting relationship link endpoints that return no data

csprocket777 avatar Apr 20 '17 03:04 csprocket777

Hey @csprocket777, this should have been solved. Can you provide some detail about your setup and the example output etc?

digitalsadhu avatar Apr 20 '17 06:04 digitalsadhu

@csprocket777 did you try to use hasOne relation type in rule model

hamzahio avatar Apr 20 '17 06:04 hamzahio