loopback-component-jsonapi
loopback-component-jsonapi copied to clipboard
belongsTo through hasMany
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"
}
}
Thanks for submitting! Will try to take a look at this over the weekend. Going to try and squash your other bugs reports first ;)
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.
Fixed in #59
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
Hey @csprocket777, this should have been solved. Can you provide some detail about your setup and the example output etc?
@csprocket777 did you try to use hasOne relation type in rule model