I have two models: Node and NodeMessage. They share a many-to-many relationship (There are messages in the nodes and a message can be in more than one node)
Node
module.exports = {
attributes: {
[...]
// Messages within the node
messages: {
collection: 'NodeMessage',
via: 'nodes'
},
[...]
}
NodeMessage
module.exports = {
attributes: {
[...]
// Nodes where this message appear
nodes: {
collection: 'Node',
via: 'messages'
},
// Users who liked this message
likes: {
collection: 'User',
via: 'likes'
}
[...]
}
}
It's a basic Many-to-Many sails relationship. Now I want to get all the messages contained in a group of Nodes. I mean, lets take 1 node and its 8 neighbors (9 in total): I want to get all the messages of these nodes.
I have been able to retrieve these messages by querying the nodes and using populate (messages), but I need to populate the likes attribute of these messages, which I can't.
The solution I am considering is to get all the IDs of these not populated messages and make another query so as to get them, but this would mean 2 queries and that wouldn't make me happy :(
Any suggestions?
Thanks in advance.