sails.jswaterline

Sails 1.0 Group By


Now that groupBy is deprecated, how can I mimic a SQL command like SELECT COUNT(*) FROM table GROUP BY xxx using the Waterline ORM ?

This page recommends using .sum() and .avg() but these methods are for number-type columns. Here I want to be able to count the rows of grouped columns, whatever type it is.


Solution

  • I think for specific groupBy query, you've got two choices.

    The first one is a 2 step action.

    1) Select all the unique element "group by field" you've got in the database.

    2) Then count the record for each unique group by field element.

    The second one is to use .sendNativeQuery() wich allow you to send a native SQL query to the datastore (you can use this only if you use a real SQL server and not the embedded Sails.JS database)

    sendNativeQuery() Documentation