如何使用loopback.io在REST中包含相关实体 [英] How to include related entities in REST with loopback.io
问题描述
我正在使用Strongloop的环回工具来创建REST服务.我想知道如何定义请求模型时要返回的相关实体.
I'm using Strongloop's loopback tool to create a REST service. I'm wondering how to define what related entities to return when requesting a model.
我在文档中看到您可以发送诸如GET /api/members?filter[include]=posts
之类的请求,并且该请求将返回相关的帖子模型,我发现您可以发出诸如GET /api/members?filter[include]=posts&filter[include]=comments
之类的请求来获取帖子和评论,但是这里有一个在代码或生成的json文件中定义您想要某种关系的方法,该方法总是在请求模型时返回的?
I see in the docs that you can send a request like GET /api/members?filter[include]=posts
and that will return the related post models, and I discovered that you can make a request like GET /api/members?filter[include]=posts&filter[include]=comments
to get posts and comments, but is there a way to define either in code or the generated json file that you'd like a certain relation to always be returned when requesting a model?
推荐答案
预设的过滤器属性称为默认范围.我们有一个待处理的拉取请求来支持这一点.请参阅 https://github.com/strongloop/loopback-datasource-juggler/pull /296 .
The preset filter properties are referred as default scope. We have a pending pull request to support that. Please see https://github.com/strongloop/loopback-datasource-juggler/pull/296.
作为发布此功能之前的解决方法,您可以使用beforeRemote挂钩使用默认范围更新过滤器对象.请参见 http://docs.strongloop.com/display/LB/Defining+remote+钩子.
As a workaround before the feature is released, you can use beforeRemote hooks to update the filter object with your default scope. See http://docs.strongloop.com/display/LB/Defining+remote+hooks.
这篇关于如何使用loopback.io在REST中包含相关实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!