MicCarr MicCarr - 9 months ago 66
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
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 Source

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.