Praveen Kumar
Praveen Kumar

Reputation: 927

How to do where condition on include relation in Loopback

I want to get the result with include relation with where condition on include model.

return this.htcApi.find({
  include: [
    {
      relation: 'nmos',
      scope: {
        include: 'countries',
      },
    },
  ],
  where: { name:'Welcome', "nmos.name":'agile'}
});

This where is condition work for name of htc model not for noms module.

I want query like

Select htc.*, nmos.* FROM htc LEFT JOIN nmos ON nmos.id = htc.n_id where htc.name = 'abc' and nmos.name = 'abc';

How can add where condition on the "relation" table?

Upvotes: 1

Views: 951

Answers (2)

Shubham
Shubham

Reputation: 170

Simply you need to add where clause in 'scope' object which lies inside the 'include' object. So the code would be like :

    return this.htcApi.find({
    include: [
      {
        relation: 'nmos',
        scope: {
          include: 'countries',
          where:{name:'agile'}
        },
      },
    ],
    where: { name:'Welcome'}
  });

Upvotes: 1

Orlyyn
Orlyyn

Reputation: 2606

In your query, you just need to add the property where within the scope property, like this:

return this.htcApi.find({
  include: [
    {
      relation: 'nmos',
      scope: {
        include: 'countries',
        where: {
          and [
            { id: htc.n_id },
            { name: 'abc' },
          ],
        },
      },
    },
  ],
  where: { name: 'abc' }
});

This should return the htcApi objects named 'abc' with the related nmos objects that have the name 'abc' and the id 'n_id'.

Upvotes: 0

Related Questions