express-toolkit 0.8.0 → 0.8.1

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.
Files changed (2) hide show
  1. package/package.json +3 -2
  2. package/src/controller.js +14 -6
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "express-toolkit",
3
- "version": "0.8.0",
3
+ "version": "0.8.1",
4
4
  "files": [
5
5
  "index.js",
6
6
  "src/"
@@ -52,6 +52,7 @@
52
52
  "mongodb-memory-server": "^8.4.0",
53
53
  "nyc": "^15.1.0",
54
54
  "sinon": "^7.5.0",
55
- "supertest": "^4.0.2"
55
+ "supertest": "^4.0.2",
56
+ "uuid": "^8.3.2"
56
57
  }
57
58
  }
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