aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/utils.js
blob: a961e8c5ba4b07fd47f1a19fa409f00e8c038975 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
'use strict'

const parallel = require('async/parallel')

const utils = {
  listForApiWithCount: listForApiWithCount
}

function listForApiWithCount (query, start, count, sort, callback) {
  const self = this

  parallel([
    function (asyncCallback) {
      self.find(query).skip(start).limit(count).sort(sort).exec(asyncCallback)
    },
    function (asyncCallback) {
      self.count(query, asyncCallback)
    }
  ], function (err, results) {
    if (err) return callback(err)

    const data = results[0]
    const total = results[1]
    return callback(null, data, total)
  })
}

// ---------------------------------------------------------------------------

module.exports = utils