From 007059ed2483009043b5569e4d95049139878798 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Fri, 21 Jul 2017 05:31:27 +0200 Subject: html.. --- lib/db/crud.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'lib/db/crud.js') 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) -- cgit v1.2.3-70-g09d2