expressa 2.0.8 → 2.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -101,7 +101,7 @@ exports.get = async function (req) {
101
101
  }
102
102
  req.query.pageitems = parseInt(req.query.pageitems) || parseInt(req.query.limit) || 10
103
103
  req.query.offset = util.getDatabaseOffset(req.query.page, req.query.pageitems)
104
- req.query.orderby = req.query.orderby || util.normalizeOrderBy({ 'meta.created': 1 }) // paging requires orderby to ensure consistent results with offset and limit
104
+ req.query.orderby = util.orderByForPagedRequests(req.query.orderby)
105
105
  delete req.query.limit
106
106
  delete req.query.skip
107
107
  let pageData, totalItems
@@ -72,7 +72,7 @@
72
72
  "sass-loader": "7.0.3",
73
73
  "script-ext-html-webpack-plugin": "^2.1.5",
74
74
  "semver": "5.5.0",
75
- "shelljs": "0.8.2",
75
+ "shelljs": "0.8.5",
76
76
  "svg-sprite-loader": "3.8.0",
77
77
  "svgo": "^1.3.2",
78
78
  "terser-webpack-plugin": "^2.2.3",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "expressa",
3
- "version": "2.0.8",
3
+ "version": "2.0.9",
4
4
  "description": "API framework using JSON schemas",
5
5
  "main": "index.js",
6
6
  "repository": "https://github.com/thomas4019/expressa",
package/util.js CHANGED
@@ -185,6 +185,18 @@ exports.mongoUpdate = function(doc, update) {
185
185
  return mongoQuery(doc, {}, update)
186
186
  }
187
187
 
188
+ // paging requires orderby to include a field that is known to be unique and constant
189
+ // to ensure consistent results from database with offset and limit. Chosen field should
190
+ // also represent a preferred ordering when doing final sort
191
+ exports.orderByForPagedRequests = function(orderby, finalSortField = 'meta.created') {
192
+ orderby = exports.normalizeOrderBy(orderby || {})
193
+ const allFields = orderby.map(([field]) => field)
194
+ if (!allFields.includes(finalSortField)) {
195
+ orderby.push([finalSortField, 1])
196
+ }
197
+ return orderby
198
+ }
199
+
188
200
  exports.normalizeOrderBy = function(orderby) {
189
201
  if (Array.isArray(orderby)) {
190
202
  orderby = orderby.map(function (ordering) {