diff options
Diffstat (limited to 'server/models/utils.js')
-rw-r--r-- | server/models/utils.js | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/server/models/utils.js b/server/models/utils.js new file mode 100644 index 000000000..a961e8c5b --- /dev/null +++ b/server/models/utils.js | |||
@@ -0,0 +1,30 @@ | |||
1 | 'use strict' | ||
2 | |||
3 | const parallel = require('async/parallel') | ||
4 | |||
5 | const utils = { | ||
6 | listForApiWithCount: listForApiWithCount | ||
7 | } | ||
8 | |||
9 | function listForApiWithCount (query, start, count, sort, callback) { | ||
10 | const self = this | ||
11 | |||
12 | parallel([ | ||
13 | function (asyncCallback) { | ||
14 | self.find(query).skip(start).limit(count).sort(sort).exec(asyncCallback) | ||
15 | }, | ||
16 | function (asyncCallback) { | ||
17 | self.count(query, asyncCallback) | ||
18 | } | ||
19 | ], function (err, results) { | ||
20 | if (err) return callback(err) | ||
21 | |||
22 | const data = results[0] | ||
23 | const total = results[1] | ||
24 | return callback(null, data, total) | ||
25 | }) | ||
26 | } | ||
27 | |||
28 | // --------------------------------------------------------------------------- | ||
29 | |||
30 | module.exports = utils | ||