DomincJune
DomincJune

Reputation: 949

Make a join query in loopback.io

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.

Upvotes: 3

Views: 9629

Answers (1)

A.Z.
A.Z.

Reputation: 1648

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) { ... });

Upvotes: 4

Related Questions