diff options
| author | Jules Laplace <julescarbon@gmail.com> | 2017-07-21 05:31:27 +0200 |
|---|---|---|
| committer | Jules Laplace <julescarbon@gmail.com> | 2017-07-21 05:31:27 +0200 |
| commit | 007059ed2483009043b5569e4d95049139878798 (patch) | |
| tree | 484398ccda5174b6af55a8ca75510e6e69b4bb1f /lib/db/crud.js | |
| parent | 55b2b2c464064393f8ce7bd2245bdd89bbffbeec (diff) | |
html..
Diffstat (limited to 'lib/db/crud.js')
| -rw-r--r-- | lib/db/crud.js | 8 |
1 files changed, 7 insertions, 1 deletions
diff --git a/lib/db/crud.js b/lib/db/crud.js index c0e0e2c..948e50d 100644 --- a/lib/db/crud.js +++ b/lib/db/crud.js @@ -4,6 +4,7 @@ module.exports = function(model) { return model.query( (qb) => { const limit = q.limit || 100 const offset = q.offset || 0 + const orderBy = q.orderBy || 'id desc' if (limit) { delete q.limit } @@ -11,7 +12,12 @@ module.exports = function(model) { delete q.offset } if (Object.keys(q).length > 0) qb.where(q) - qb.orderBy("id", "desc") + if (orderBy) { + const ob = orderBy.split(" ") + const ob_field = ob[0] || 'id' + const ob_dir = ob[1] || 'desc' + qb.orderBy(ob_field, ob_dir) + } if (limit) qb.limit( limit ) if (offset) qb.offset( offset ) // console.log(qb) |
