Make a join query in loopback.io

旧巷老猫 提交于 2019-12-19 09:47:18

问题


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.

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

stories : id, title, noteId

notes : id , desc

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'
            }
        }
    );
};

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.


回答1:


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.

For example:

Note relation:

"relations": {
  "stories": {
    "type": "hasMany",
    "model": "Story",
    "foreignKey": "noteId"
  }
},

Query:

Note.find({include: ['stories']}, function(err, data) { ... });


来源:https://stackoverflow.com/questions/32231795/make-a-join-query-in-loopback-io

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