The original answer is technically correct and useful, but I wanted to find a way to programmatically apply the orderBy() function several times, here is the actual solution I went with for the link:
var sortArray = [ {'field': 'title', 'direction': 'asc'}, {'field': 'id', 'direction': 'desc'} ]; knex .select() .table('products') .modify(function(queryBuilder) { _.each(sortArray, function(sort) { queryBuilder.orderBy(sort.field, sort.direction); }); })
Knex offers a change function that allows you to work directly with queryBuilder. The array iterator then calls orderBy() several times.
Ian jones
source share