Search code examples
node.jsloopbackjs

hasManyThrough polymorphic relation


I have a student model,a favorite model and media models eg music,video etc. I want to implement hasManyThrough ploymorphic relation in which the through model is favorite and then stores these favorites in favorite table in my case mongoDB. Am using loopback3 and its documentation isn't clear about this topic.Any lead?


Solution

  • Your models would look like this:

    common/models/student.json

    {
      "name": "Student",
      "base": "User",
      "idInjection": true,
      "options": {
        "validateUpsert": true
      },
      "properties": {
    
      },
      "validations": [],
      "relations": {
        "favorites": {
          "type": "hasMany",
          "model": "Favorite",
          "foreignKey": "studentId"
        },
        "videos": {
          "type": "hasMany",
          "model": "Video",
          "foreignKey": "studentId",
          "through": "Favorite",
          "keyThrough": "favoriteId"
        },
        "musics": {
          "type": "hasMany",
          "model": "Music",
          "foreignKey": "studentId",
          "through": "Favorite",
          "keyThrough": "favoriteId"
        }
      },
      "acls": [],
      "methods": {}
    }
    

    common/models/video.json

    {
      "name": "Video",
      "base": "PersistedModel",
      "idInjection": true,
      "options": {
        "validateUpsert": true
      },
      "properties": {
    
      },
      "validations": [],
      "relations": {
        "favorites": {
          "type": "hasMany",
          "model": "Favorite",
          "foreignKey": "videoId"
        },
        "students": {
          "type": "hasMany",
          "model": "Student",
          "foreignKey": "videoId",
          "through": "Favorite",
          "keyThrough": "studentId"
        }
      },
      "acls": [
      ],
      "methods": {}
    }
    

    common/models/favorite.json

    {
      "name": "Favorite",
      "base": "PersistedModel",
      "idInjection": true,
      "options": {
        "validateUpsert": true
      },
      "properties": {
    
      },
      "validations": [],
      "relations": {
        "student": {
          "type": "belongsTo",
          "model": "Student",
          "foreignKey": "studentId"
        },
        "video": {
          "type": "belongsTo",
          "model": "Video",
          "foreignKey": "videoId"
        },
        "music": {
          "type": "belongsTo",
          "model": "Music",
          "foreignKey": "musicId"
        }
      },
      "acls": [],
      "methods": {}
    }
    

    Then, you simply need to POST a new Favorite item with the attributes studentId and videoId to add the new relation.

    EDIT: Added music.json

    common/models/music.json

    {
      "name": "Music",
      "base": "PersistedModel",
      "idInjection": true,
      "options": {
        "validateUpsert": true
      },
      "properties": {
    
      },
      "validations": [],
      "relations": {
        "favorites": {
          "type": "hasMany",
          "model": "Favorite",
          "foreignKey": "musicId"
        },
        "students": {
          "type": "hasMany",
          "model": "Student",
          "foreignKey": "musicId",
          "through": "Favorite",
          "keyThrough": "studentId"
        }
      },
      "acls": [
      ],
      "methods": {}
    }