express-toolkit 0.8.0 → 0.8.3

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.
package/README.md CHANGED
@@ -19,6 +19,7 @@ Tiny little utilities for reducing expressjs boilerplate code when building simp
19
19
  - [Default endpoints](#default-endpoints)
20
20
  - [Disable endpoints](#disable-endpoints)
21
21
  - [Sorting](#sorting)
22
+ - [Pagination](#pagination)
22
23
  - [Projection](#projection)
23
24
  - [Custom primary key](#custom-primary-key)
24
25
  - [Hooks](#hooks)
@@ -237,6 +238,12 @@ For example, the following request would sort dinosaurs by age, oldest to younge
237
238
  ```http
238
239
  GET /dinosaurs?sortby=age&sortorder=DESC
239
240
  ```
241
+ ## Pagination
242
+ GET endpoints support result pagination through `skip` and `limit` parameters:
243
+ - `skip <Number>` tells the endpoint how many results to skip
244
+ - `limit <Number` tells the endpoint how many results to include in the response
245
+
246
+ To implement a pagination scheme, you can leverage these two parameters in the following way: Suppose you want to return `R` results per page and you want to return page number `P`, you just need to set limit to `R` and skip to `(P-1)*R`
240
247
 
241
248
  ## Projection
242
249
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "express-toolkit",
3
- "version": "0.8.0",
3
+ "version": "0.8.3",
4
4
  "files": [
5
5
  "index.js",
6
6
  "src/"
@@ -16,6 +16,7 @@
16
16
  "test": "nyc --reporter=text ava test/*.spec.js --verbose",
17
17
  "test-ci": "npx nyc --reporter=lcov ava test/*.spec.js --verbose",
18
18
  "commit": "npx git-cz",
19
+ "documentation": "docsify init ./docs",
19
20
  "pre-commit": "npm run lint && npm run test",
20
21
  "changelog": "npx easy-changelog --out=./CHANGELOG.md",
21
22
  "release": "sh scripts/release.sh"
@@ -34,13 +35,13 @@
34
35
  "express"
35
36
  ],
36
37
  "dependencies": {
37
- "express": "^4.17.3",
38
- "json8-patch": "^1.0.4",
39
- "mongoose": "^5.12.3",
38
+ "express": "^4.18.1",
39
+ "json8-patch": "^1.0.6",
40
+ "mongoose": "^6.5.1",
40
41
  "throwable-http-errors": "^2.1.0"
41
42
  },
42
43
  "devDependencies": {
43
- "ava": "^3.8.1",
44
+ "ava": "^4.3.1",
44
45
  "body-parser": "^1.19.0",
45
46
  "eslint": "^7.32.0",
46
47
  "eslint-config-standard": "^16.0.3",
@@ -52,6 +53,7 @@
52
53
  "mongodb-memory-server": "^8.4.0",
53
54
  "nyc": "^15.1.0",
54
55
  "sinon": "^7.5.0",
55
- "supertest": "^4.0.2"
56
+ "supertest": "^4.0.2",
57
+ "uuid": "^8.3.2"
56
58
  }
57
59
  }
package/src/controller.js CHANGED
@@ -20,11 +20,13 @@ class Controller {
20
20
  * @param {String} [config.id] The attribute to use as primary key for findById, updateById and deleteById. Defaults to _id.
21
21
  * @param {Number} [config.defaultSkipValue] The default skip value to be used in find() queries
22
22
  * @param {Number} [config.defaultLimitValue] The default skip value to be used in find() queries
23
+ * @param {Number} [config.useUpdateOne] If set to true, updateById method will use mongoose's Model.updateOne() instead of Document.save()
23
24
  */
24
25
  constructor (config) {
25
26
  this.Model = config.model
26
27
  this.id = config.id || '_id'
27
28
  this.name = config.name
29
+ this.useUpdateOne = Boolean(config.useUpdateOne) || false
28
30
  this.defaultSkipValue = config.defaultSkipValue || DEFAULT_SKIP_VALUE
29
31
  this.defaultLimitValue = config.defaultLimitValue || DEFAULT_LIMIT_VALUE
30
32
  this.__hooks = {}
@@ -37,10 +39,10 @@ class Controller {
37
39
  async find (query) {
38
40
  let skip = this.defaultSkipValue
39
41
  let limit = this.defaultLimitValue
40
- if (query.hasOwnProperty('limit')) {
42
+ if (Object.prototype.hasOwnProperty.call(query, 'limit')) {
41
43
  limit = Number(query.limit)
42
44
  }
43
- if (query.hasOwnProperty('skip')) {
45
+ if (Object.prototype.hasOwnProperty.call(query, 'skip')) {
44
46
  skip = Number(query.skip)
45
47
  }
46
48
 
@@ -180,7 +182,7 @@ class Controller {
180
182
  const instances = await this.Model.find(query)
181
183
 
182
184
  for (const instance of instances) {
183
- for (var k in update) {
185
+ for (const k in update) {
184
186
  instance.set(k, update[k])
185
187
  }
186
188
 
@@ -207,7 +209,7 @@ class Controller {
207
209
  throw new Errors.NotFound()
208
210
  }
209
211
 
210
- for (var k in update) {
212
+ for (const k in update) {
211
213
  instance.set(k, update[k])
212
214
  }
213
215
 
@@ -216,7 +218,13 @@ class Controller {
216
218
  throw new Errors.BadRequest(validationError.message)
217
219
  }
218
220
 
219
- return instance.save()
221
+ if (this.useUpdateOne) {
222
+ await this.Model.updateOne({ [this.id]: id }, update)
223
+
224
+ return instance.toJSON()
225
+ } else {
226
+ return instance.save()
227
+ }
220
228
  }
221
229
 
222
230
  /**
@@ -233,7 +241,7 @@ class Controller {
233
241
  throw new Errors.NotFound()
234
242
  }
235
243
  // Primary identifiers cannot be replaced
236
- replacement['_id'] = instance._id
244
+ replacement._id = instance._id
237
245
  replacement[this.id] = instance[this.id]
238
246
  instance.overwrite(replacement)
239
247