I have complicated relationship that includes 2 thorough models. I want to create custom method that solves my purpose. I have MySQL DB as datasouce.
3 Main Models.
Artist, Album, Songs.
Album and Song are related 2 each other with N:N relationship through model "Tracklist". Artist and Tracklist are connected through thorough model "TracklistArtist". I wish to get list of albums and songs of particular artist through this relationship. Though I achieved with patches but it returns duplicate list and same time I want unique record list. Please help.
I am just mentioning Model relation object here instead of complete model-object.
artist.json
{
"name": "Artist",
"relations": {
"tracklists": {
"type": "hasMany",
"model": "Tracklist",
"foreignKey": "",
"through": "TracklistArtist"
}
}
}
album.json
{
"name" : "Album",
"relations": {
"songs": {
"type": "hasMany",
"model": "Song",
"foreignKey": "",
"through": "Tracklist"
}
}
}
song.json
{
"name" : "Song",
"relations": {
"albums": {
"type": "hasMany",
"model": "Album",
"foreignKey": "",
"through": "Tracklist"
}
}
}
tracklist.json
{
"name": "Tracklist",
"relations": {
"album": {
"type": "belongsTo",
"model": "Album",
"foreignKey": ""
},
"song": {
"type": "belongsTo",
"model": "Song",
"foreignKey": ""
},
"artists": {
"type": "hasMany",
"model": "Artist",
"foreignKey": "",
"through": "TracklistArtist"
}
}
}
tracklist-artist.json
{
"name": "TracklistArtist",
"relations": {
"artist": {
"type": "belongsTo",
"model": "Artist",
"foreignKey": ""
},
"tracklist": {
"type": "belongsTo",
"model": "Tracklist",
"foreignKey": ""
}
}
}
I have checked patched from mysql-connector of loopback. Here is link. Check Here
Artist.tracklists({
id: $stateParams.artistId,
filter: {
groupBY:"albumId",
include: {relation: "album", scope: {}},
}
}).$promise.then(function (data) {
});