在loopback.io中进行联接查询 [英] Make a join query in loopback.io
问题描述
我正在尝试使用loopback.io构建一个简单的应用程序,以此作为我学习的过程.我已经建立了项目,创建的模型和api运行正常.
I am trying to build a simple application using loopback.io as process of my learning. I have set up the project, created models and apis are working fine.
现在,我正在尝试创建一个自定义api,该API可以通过进行联接查询来从两个不同的模型中获取数据.所以我有两个模型
Now I am trying to create a custom api which can get the data from two different models by making a join query. So i have a two models
故事:
id, title, noteId
注释:id , desc
我的Storys.js文件为
i have stories.js file as
module.exports = function(Stories) {
Stories.list = function(cb) {
// make a join query
};
Stories.remoteMethod(
'list', {
http: {
path: '/list',
verb: 'get'
},
returns: {
arg: 'list',
type: 'array'
}
}
);
};
通常我会加入php api,但在这里我有点困惑.我可以在这里将原始查询传递给数据库还是回送有一些不同的方法来实现这一点.任何帮助将不胜感激.
In general i will make a join in php api but here i am bit confused.Can i pass a raw query to database here or does loopback has some different way of achieving this. Any help would be appreciated.
推荐答案
您不需要传递sql查询.您可以使用查询数据 .strongloop.com/display/LB/PersistedModel + class"rel =" nofollow> PersistedModel 使用
You don't need to pass sql query. You can query data using PersistedModel find method by using include filter
要使用包含过滤器,您必须创建模型关系.
In order to use include filter you have to create model relation.
例如:
注释关系:
"relations": {
"stories": {
"type": "hasMany",
"model": "Story",
"foreignKey": "noteId"
}
},
查询:
Note.find({include: ['stories']}, function(err, data) { ... });
这篇关于在loopback.io中进行联接查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!