RSA
RSA

Reputation: 1449

how to get nested master-detail or master - detail -detail query in Loopback

I have category and category_subs, master-detail model, post model belongs to category_subs. In the following code, I can get master-detail of both but I don't know how to include post to them or even attachment model of the post to the remote method.

module.exports = function (Category) {
    Category.categorySubs = function (id, cb) {
    Category.find({
        where: {
          id: id
        },    
        include: {
            relation: 'categorySubs',
          scope: {
              include: 'category_subs'
          }
        }
      },
      function (err, posts) {
        cb(null, posts);
      });
  }
 Category.remoteMethod('categorySubs', {
    accepts: {
      arg: 'id',
      type: 'string'
    },
    returns: {
      arg: 'ID',
      type: 'string'
    },
    http: {
      path: '/iteminfo',
      verb: 'get'
    }
  });

update

category.json

"relations": {
    "categorySubs": {
      "type": "hasMany",
      "model": "category_subs",
      "foreignKey": "catgory_id"
    }
  },

category_subs

"relations": {
    "posts": {
      "type": "hasMany",
      "model": "post",
      "foreignKey": "category_sub_id"
    }
  },

Upvotes: 0

Views: 187

Answers (1)

Parsaria
Parsaria

Reputation: 1099

I googled and I think you had to have deep look at https://loopback.io/doc/en/lb3/Include-filter.html

Upvotes: 1

Related Questions