Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find and count all in Sails.js + Waterline

Is there a way to do select query and count_all query via single method? For pagination purposes we need to know total number of items so we can calculate and show number of pages.

like image 893
Kristian Ačkar Avatar asked Jun 09 '15 12:06

Kristian Ačkar


2 Answers

getLength: function(req, res) {
    Posts.find({}).exec(function(err, items){

        return items.length;
    });
}
like image 60
Matan Gubkin Avatar answered Nov 10 '22 12:11

Matan Gubkin


Check out Sails.Js - How I do pagination in sails.Js for pagination in Waterline.

To get the total number of items, you can use:

Post.count().exec(function (err, nbOfInstances) {
    if(err) return res.negociate(err);

    return res.ok(nbOfInstances);
});
like image 2
Yann Bertrand Avatar answered Nov 10 '22 12:11

Yann Bertrand