1

I post here , because I recently start a development project with sails and MongoDB 3.0.2.

I am a beginner with this framework and his ORM (aka waterline).

In my project I've two classes : listeners and titles, and I need to do this relations :

  • N Listeners can add to her favorite N titles -> Many to Many.

This is the sample of my classes :

Listeners :

module.exports = {
 schema   : true,
  autoPK    : false,
  attributes: {
    name : { 
     type: 'string' ,
      required : true,
      string : true,
      notNull:true
    },

    email : { 
     type: 'email' ,
      required : true,
      email :true,
      notNull:true,
      unique: true,
      primaryKey:true
    },

    password : { 
     type: 'string',
      required : true,
      string:true,
      minLength : 6,
      notNull:true
    },

    favorites:{
      collection :"title",
      via : "bookmarkedBy",
      dominant: true
    }
  //....
   addFavoriteTitle:function(userID,titleID,callback){
    User.findOne(userID).exec(function(error, user) {
     if(!error && user){
    
      user.favorites.add(titleID);
      user.save(function(error) {});
      callback(error,user);
    }  else{
     callback(error,user);
   }
   });
  },
}

Titles :

module.exports = {
 schema   : true,
 autoPK : false,
 attributes: {

  title : { 
      type: 'string',
      required : true,
      //string : true,
      notNull:true, 
      unique : true,
      primaryKey:true
     },

     artist : { 
      type: 'string',
      required : true,
      string : true,
      notNull:true
     },

     artwork : { 
      type: 'string'
     },
     playing:{
      type:'boolean',
      defaultsTo:false
     },
     bookmarkedBy:{
      collection : "user",
      via :  "favorites"
     },
 },
    //.....

 
  }

I followed the official documention of sails, but I don't see / undestand :

How to recover the information on a title from the information contained in the relationship: " favorites" of the user class .

And how do from the relationship " bookmarkedBy " Class Title , to find / access information on a user who has the title in his "favorites".

So, I fail to use the relationship between the two classes , even according to the documentation.

Can you help me ?

outstore
  • 175
  • 2
  • 13
  • 1
    It may be useful for you to read about `populate()` method: http://sailsjs.org/#!/documentation/reference/waterline/populated-values . So... User.findOne(userId).populate('favorites').then.... And access to `foundUser.favorites`. Same in the opposite direction. – Fran Dios May 13 '15 at 06:55

0 Answers0