Reputation: 2021
In sails I need function to get rows by referenced table. Something like
Child.find({ parent.age: 30 })
or in SQL language
SELECT child.* FROM child JOIN parent ON child.parent_id = parent.id WHERE parent.age = 30
So far I have written uggly function which does the work in 3 steps:
get_children_in_celebration: function(req, res) {
// 1. Get parend ids
Parent.find({ select: ['id'], age: 30}).exec(function(err_parents, res_parents) {
// 2. Collect them to array suitable for next query
var parent_ids = [];
for(var i = 0; i < res_parents.length; i++) {
parent_ids[i] = res_parents[i].id;
}
// Get their children
Child.find({ parent_id: parent_ids }).exec(function(err_children, res_children) {
return res.json(res_children);
});
});
},
The first query returns ~5000 parents and therefore together it means heavy load for db. Does sails offer any nicer solution?
Upvotes: 0
Views: 107
Reputation: 422
No, Sails does not support filtering on associated table. I had the similar issue, had used raw queries for the same.
Model.query("select * from table", function (error, response) {})
Upvotes: 1