expressa 2.0.8 → 2.0.10

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.10",
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
@@ -53,6 +53,19 @@ exports.validateSchema = function(collection, doc) {
53
53
  return true
54
54
  }
55
55
 
56
+ exports.getSchemaProperty = function(collection, path) {
57
+ const parts = path.split('.')
58
+ let obj = schemas[collection]
59
+ for (const part of parts) {
60
+ if (obj.properties && obj.properties[part]) {
61
+ obj = obj.properties[part]
62
+ } else {
63
+ return
64
+ }
65
+ }
66
+ return obj
67
+ }
68
+
56
69
  exports.orderBy = function (data, orderby) {
57
70
  data.sort(function compare (a, b) {
58
71
  for (let i = 0; i < orderby.length; i++) {
@@ -185,6 +198,20 @@ exports.mongoUpdate = function(doc, update) {
185
198
  return mongoQuery(doc, {}, update)
186
199
  }
187
200
 
201
+ exports.mongoToPostgres = mongoToPostgres
202
+
203
+ // paging requires orderby to include a field that is known to be unique and constant
204
+ // to ensure consistent results from database with offset and limit. Chosen field should
205
+ // also represent a preferred ordering when doing final sort
206
+ exports.orderByForPagedRequests = function(orderby, finalSortField = 'meta.created') {
207
+ orderby = exports.normalizeOrderBy(orderby || {})
208
+ const allFields = orderby.map(([field]) => field)
209
+ if (!allFields.includes(finalSortField)) {
210
+ orderby.push([finalSortField, 1])
211
+ }
212
+ return orderby
213
+ }
214
+
188
215
  exports.normalizeOrderBy = function(orderby) {
189
216
  if (Array.isArray(orderby)) {
190
217
  orderby = orderby.map(function (ordering) {