var _ = require('lodash');
var actionUtil = require('sails/lib/hooks/blueprints/actionUtil');

var takeAlias = _.partial(_.pluck, _, 'alias');
var populateAlias = function (model, alias) {
  return model.populate(alias);
};

/**
 * Find Records
 * GET /:model
 *
 * An API call to find and return model instances from the data adapter using the specified criteria.
 * If an id was specified, just the instance with that unique id will be returned.
 */
module.exports = function (req, res) {
  _.set(req.options, 'criteria.blacklist', ['fields', 'populate', 'limit', 'skip', 'page', 'sort']);

  var fields = req.param('fields') ? req.param('fields').replace(/ /g, '').split(',') : [];
  var populate = req.param('populate') ? req.param('populate').replace(/ /g, '').split(',') : [];
  var Model = actionUtil.parseModel(req);
  var where = actionUtil.parseCriteria(req);
  var limit = actionUtil.parseLimit(req);
  var skip = (req.param('page') - 1) * limit || actionUtil.parseSkip(req);
  var sort = actionUtil.parseSort(req);
  var query = Model.find(null, fields.length > 0 ? {select: fields} : null).where(where).limit(limit).skip(skip).sort(sort);
  var findQuery = _.reduce(_.intersection(populate, takeAlias(Model.associations)), populateAlias, query);

  findQuery
    .then(function (records) {
      return [records, null, null, {
        criteria: where,
        limit: limit,
        start: skip,
        end: skip + limit,
        total: records.length
      }];
    })
    .spread(res.ok)
    .catch(res.serverError);
};

@@ -16,24 +16,59 @@
 ('lodash');%0A
+var Promise = require('bluebird');%0A
 var actionUt
@@ -1291,30 +1291,98 @@
 y);%0A
-%0A  findQuery%0A    .then
+  var countQuery = Model.count(where);%0A%0A  Promise.all(%5BfindQuery, countQuery%5D)%0A    .spread
 (fun
@@ -1395,16 +1395,23 @@
 (records
+, count
 ) %7B%0A    
@@ -1557,22 +1557,13 @@
 al: 
-records.length
+count
 %0A   

Return count request to database