floatyourboat
floatyourboat

Reputation: 83

LoopBack Relationships

This is my first time dealing with a NoSQL form of database and I'm a little confused about "relationships" in document-oriented databases. I'm using LoopBack & AngularJS.

I have a model page that has many page as it's children (i.e. menu items and submenus).

A page model is as follows:

"properties": {
 "name": {
   "type": "string",
   "required": true
 },
 "slug": {
   "type": "string"
 },
 "link": {
   "type": "string"
 },
 "createdAt": {
   "type": "date",
   "required": true
 },
 "children": {
   "type": [
     "object"
   ]
 }
},

with

"relations": {
 "children": {
   "type": "hasMany",
   "model": "page",
   "foreignKey": "parentId"
 }
},

My confusion is that whenever I explore the LoopBack API, and then get the parent pages, I don't see the children property populated. But, doing a get to see the children of a parent (using the parent's id) turns out fine - I can see the parentId populated with it's parent.

My question is if this is normal when dealing with NoSQL/document-oriented databases, or am I doing something wrong?

Thanks a lot!

Upvotes: 1

Views: 1287

Answers (3)

ChamalPradeep
ChamalPradeep

Reputation: 429

Sometimes LoopBack queries are getting messy when its come to relationships and parallel queries.

I think this is simply a query issue in LoopBack.

As I realized this is like Parent Page object can have multiple Children page objects.

We can use following query statement to retrieve related parent page and children pages.


    app.models.Page.find({
            where: {
              parentId : 'yourPageId'
            },
            include: {
              relation: 'Children'
            }

          }, function (err, pages) {
            if (err) {
              cb(err);
            } else { 
              console.log("you will get related children pages alone with parent page")
              cb(null, pages);
            }
    });

Hope this will work...!!

Upvotes: 3

Acapulco
Acapulco

Reputation: 3523

I believe you might be missing the other direction of the relationship.

You can do a belongsTo relation in the children to specify that it belongs to a parent.

That should enable you to see both directions' methods in the explorer.

Also, the foreingKey parentId should be set on the children, not on the parent. I.e. leave foreignKey empty on the parent relations definition, and instead use it on the children relations definition.

So on one hand, in the model you will have inside the relations field:

 "children": {
   "type": "hasMany",
   "model": "page",
   "foreignKey": ""
 },

And also

"parent": {
    "type": "belongsTo",
    "model": "page",
    "foreignKey": "parentId"
 },

plus any other relations you have.

I have tested this and it works, although I have used two different models for the relation instead of only one.

I.e. I use

ModelA hasMany ModelB and ModelB belongsTo ModelA

instead of

ModelA hasMany ModelA and ModelA belongsTo ModelA

Upvotes: 1

Diego Varangot
Diego Varangot

Reputation: 1

In mongoose when you want to get the page's "children" populated you need to specifically ask for it using:

Pages.find({})
  .populate('children')
  .exec(function(err, pages){
     //..
  })

Looking on LoopBack's page (http://loopback.io/doc/en/lb2/Querying-related-models.html) there is something similar called "include", you should try doing:

Page.find({include:'children'}, function() {
  //...
});

Let me know if that worked!

Upvotes: 0

Related Questions