Loopback Filter Based On Related Model Properties

不想你离开。 提交于 2019-12-07 08:37:07

问题


I want to be able to filter based on the properties of related models. For example, I have a Class object with a Building object. I want to get a list of classes that take place in a certain building.

This filter

{  
   "include":"building",
   "scope":{  
      "where":{  
         "name":"warehouse"
      }
   }
}

returns all classes, but only includes building if its name is "warehouse". What I want is for the where clause on building name to apply to the whole filter so that I only get the class if it's building has that name.

Is there any way to do what I want?

Thanks!


回答1:


You can do this in code, see include with filters in the docs.

I'm not sure about the JSON but I think it should look more like this:

  include: {
    relation: 'building',
    scope: {
      where: {name: 'warehouse'}
    }
  }



回答2:


At the moment this is not possible. The issue has been described in this topic; https://github.com/strongloop/loopback/issues/517

It looks like Strongloop is not going to implement this feature in the near future.




回答3:


Whenever you want to use it on API CALL , you can follow a model like this one and adapt it to your context.

//Here (as filter) , we get just the most recent message of a chat

{
 "include" : {
   "relation" :  "messages"  , 
   "scope" : 
     { 
       "order" : "createdAt DESC" , 
       "limit":1, 
       "skip":0
     }
   }
 }


来源:https://stackoverflow.com/questions/29321614/loopback-filter-based-on-related-model-properties

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!