MicCarr MicCarr - 25 days ago 7
Node.js Question

How to sort by sum of field of a relation on sails.js?

I searched a lot about sorting elements by sum of votes (in another model), like I do in SQL here :

SELECT item.* FROM item
LEFT JOIN (
SELECT
vote.item,
SUM(vote.value) AS vote.rating
FROM vote
GROUP BY vote.item
) AS res ON item.id = vote.item
ORDER BY res.rating DESC


Is there a way to do it via waterline methods ?

Answer

I think you can't do the left join with simple waterline methods, but you can use the .query method to execute your raw SQL syntax.