Reputation: 163
I am trying to find an efficient way to retrieve results which spread across several models. In my example I have three models:
Video
which stores video data including the creator of a videoUser
which stores user dataFollower
which stores which users follow which other usersA simplified version of my models looks like this:
// Video.js
module.exports = {
attributes: {
title: {
type: 'string'
},
creator: {
model: 'User'
}
}
};
// User.js
module.exports = {
attributes: {
user_name: {
type: 'string'
}
}
};
// Follower.js
module.exports = {
attributes: {
user: {
model: 'User'
},
follower: {
model: 'User'
}
}
};
I realize that I could store the many to many association as a Collection
in User
directly, but I also store other values along with it such as when a user started to follow another user and from which destination he did so. In addition, I store lots of similar data and I would like to keep the core Models like Video
and User
lean.
So now I am trying to get the 10 latest videos (the creation datetime is stored in the createdAt
attribute automatically added by waterline) which were created by a user which a given user follows.
Starting with this given user (let's call him queryingUser
) I am trying to get all the users he follows and use them as input to search for videos. I imagine a call to look like this:
Follower.find({where: {follower: queryingUser} , select: ['user']}).exec(function (err, users){
var userArray = [];
for (var i=0; i< users.length; i++) userArray.push(users[i].user);
Video.find({ where: {creator: userArray}, limit: 10, sort: 'createdAt DESC' }).exec(function (err, videos){
// videos should contain the desired result
});
});
I think according to the waterline docs a call like this should work, however, it doesn't seem to retrieve any results.
Am I doing something wrong? Is there a different approach which would lead to the desired result in an efficient way?
Upvotes: 1
Views: 87
Reputation: 163
Never mind, it does actually work. I made a mistake elsewhere in my code, but after I fixed it I got the example to work. Maybe it is still helpful for someone else.
Upvotes: 1