monastery 1.32.1 → 1.32.4

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/changelog.md CHANGED
@@ -2,6 +2,22 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ### [1.32.4](https://github.com/boycce/monastery/compare/1.32.3...1.32.4) (2022-03-07)
6
+
7
+ ### [1.32.3](https://github.com/boycce/monastery/compare/1.32.2...1.32.3) (2022-03-07)
8
+
9
+
10
+ ### Bug Fixes
11
+
12
+ * fileSize warning ([3824b23](https://github.com/boycce/monastery/commit/3824b23883fad508e081d2a2bce1c340ec2775dc))
13
+
14
+ ### [1.32.2](https://github.com/boycce/monastery/compare/1.32.1...1.32.2) (2022-03-04)
15
+
16
+
17
+ ### Bug Fixes
18
+
19
+ * missing original non-validated this.data in beforeUpdate ([4b4002d](https://github.com/boycce/monastery/commit/4b4002d3d4d2609025cbb22cacecf948252be38b))
20
+
5
21
  ### [1.32.1](https://github.com/boycce/monastery/compare/1.32.0...1.32.1) (2022-03-01)
6
22
 
7
23
 
package/lib/model-crud.js CHANGED
@@ -28,11 +28,11 @@ module.exports = {
28
28
  ])
29
29
 
30
30
  // Validate
31
- opts.data = await this.validate(opts.data||{}, { ...opts })
31
+ let data = await this.validate(opts.data||{}, { ...opts })
32
32
 
33
33
  // Insert
34
- await util.runSeries(this.beforeInsert.map(f => f.bind(opts, opts.data)))
35
- let response = await this._insert(opts.data, options)
34
+ await util.runSeries(this.beforeInsert.map(f => f.bind(opts, data)))
35
+ let response = await this._insert(data, options)
36
36
  await util.runSeries(this.afterInsert.map(f => f.bind(opts, response)))
37
37
 
38
38
  // Success/error
@@ -193,31 +193,32 @@ module.exports = {
193
193
  }
194
194
  try {
195
195
  opts = await this._queryObject(opts, 'update')
196
+ let data = opts.data
196
197
  let response = null
197
198
  let operators = util.pluck(opts, [/^\$/])
198
199
  let options = util.omit(opts, ['data', 'query', 'respond', 'validateUndefined', 'skipValidation', 'blacklist'])
199
200
 
200
201
  // Validate
201
- if (util.isDefined(opts.data)) opts.data = await this.validate(opts.data, { ...opts })
202
- if (!util.isDefined(opts.data) && util.isEmpty(operators)) {
202
+ if (util.isDefined(data)) data = await this.validate(opts.data, { ...opts })
203
+ if (!util.isDefined(data) && util.isEmpty(operators)) {
203
204
  throw new Error(`Please pass an update operator to ${this.name}.update(), e.g. data, $unset, etc`)
204
205
  }
205
- if (util.isDefined(opts.data) && (!opts.data || util.isEmpty(opts.data))) {
206
+ if (util.isDefined(data) && (!data || util.isEmpty(data))) {
206
207
  throw new Error(`No valid data passed to ${this.name}.update({ data: .. })`)
207
208
  }
208
- // Hook: beforeUpdate
209
- await util.runSeries(this.beforeUpdate.map(f => f.bind(opts, opts.data||{})))
210
- if (opts.data && operators['$set']) {
209
+ // Hook: beforeUpdate (has access to original, non-validated opts.data)
210
+ await util.runSeries(this.beforeUpdate.map(f => f.bind(opts, data||{})))
211
+ if (data && operators['$set']) {
211
212
  this.warn(`'$set' fields take precedence over the data fields for \`${this.name}.update()\``)
212
213
  }
213
- if (opts.data || operators['$set']) {
214
- operators['$set'] = { ...opts.data, ...(operators['$set'] || {}) }
214
+ if (data || operators['$set']) {
215
+ operators['$set'] = { ...data, ...(operators['$set'] || {}) }
215
216
  }
216
217
  // Update
217
218
  let update = await this._update(opts.query, operators, options)
218
219
  if (update.n) response = Object.assign(Object.create({ _output: update }), operators['$set']||{})
219
220
 
220
- // Hook: afterUpdate
221
+ // Hook: afterUpdate (doesn't have access to validated data)
221
222
  if (update.n) await util.runSeries(this.afterUpdate.map(f => f.bind(opts, response)))
222
223
 
223
224
  // Success
@@ -291,7 +291,7 @@ module.exports = {
291
291
 
292
292
  _ignoredRules: [ // todo: change name? i.e. 'specialFields'
293
293
  // Need to remove filesize and formats..
294
- 'default', 'defaultOverride', 'filename', 'filesize', 'formats', 'image', 'index', 'insertOnly',
294
+ 'default', 'defaultOverride', 'filename', 'filesize', 'fileSize', 'formats', 'image', 'index', 'insertOnly',
295
295
  'model', 'nullObject', 'params', 'getSignedUrl', 'timestampField', 'type', 'virtual'
296
296
  ]
297
297
 
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "monastery",
3
3
  "description": "⛪ A straight forward MongoDB ODM built around Monk",
4
4
  "author": "Ricky Boyce",
5
- "version": "1.32.1",
5
+ "version": "1.32.4",
6
6
  "license": "MIT",
7
7
  "repository": "github:boycce/monastery",
8
8
  "homepage": "https://boycce.github.io/monastery/",
package/test/crud.js CHANGED
@@ -3,6 +3,7 @@
3
3
  module.exports = function(monastery, opendb) {
4
4
 
5
5
  test('basic operator calls', async () => {
6
+ // Todo: take out and group query/id parsing tests
6
7
  let db = (await opendb(null)).db
7
8
  let user = db.model('user', {
8
9
  fields: {
@@ -53,20 +54,23 @@ module.exports = function(monastery, opendb) {
53
54
  let find6 = await user.find(inserted2[0]._id.toString())
54
55
  expect(find6).toEqual({ _id: inserted2[0]._id, name: 'Martin Luther1' })
55
56
 
56
- // Missing parameters
57
- await expect(user.find()).rejects.toThrow('Please pass an object or MongoId to options.query')
58
- await expect(user.find(undefined)).rejects.toThrow('Please pass an object or MongoId to options.query')
59
- await expect(user.find({})).rejects.toThrow('Please pass an object or MongoId to options.query')
60
- await expect(user.find({ query: null })).rejects.toThrow('Please pass an object or MongoId to options.query')
61
- await expect(user.find({ query: undefined })).rejects.toThrow('Please pass an object or MongoId to options.query')
62
- await expect(user.find({ query: { _id: undefined }}))
63
- .rejects.toThrow('Please pass an object or MongoId to options.query')
64
- await expect(user.find(1)).rejects.toThrow('Please pass an object or MongoId to options.query')
65
-
66
- // Bad MongoID
67
- await expect(user.find({ query: '' })).resolves.toEqual(null)
68
- await expect(user.find('bad-id')).resolves.toEqual(null)
57
+ // Bad ids
58
+ let badIdOrQuery = 'Please pass an object or MongoId to options.query'
59
+ await expect(user.find()).rejects.toThrow(badIdOrQuery)
60
+ await expect(user.find(1)).rejects.toThrow(badIdOrQuery)
61
+ await expect(user.find(null)).rejects.toThrow(badIdOrQuery)
62
+ await expect(user.find(undefined)).rejects.toThrow(badIdOrQuery)
63
+ await expect(user.find({})).rejects.toThrow(badIdOrQuery)
64
+ await expect(user.find({ query: null })).rejects.toThrow(badIdOrQuery)
65
+ await expect(user.find({ query: undefined })).rejects.toThrow(badIdOrQuery)
66
+ await expect(user.find({ query: { _id: undefined }})).rejects.toThrow(badIdOrQuery)
67
+
68
+ // Parseable
69
69
  await expect(user.find('')).resolves.toEqual(null)
70
+ await expect(user.find('invalid-id')).resolves.toEqual(null)
71
+ await expect(user.find({ query: '' })).resolves.toEqual(null)
72
+ await expect(user.find({ query: { _id: '' }})).resolves.toEqual(null)
73
+ await expect(user.find({ query: { _id: null }})).resolves.toEqual([]) // should throw error
70
74
 
71
75
  // FindOne (query id)
72
76
  let findOne = await user.findOne({ query: inserted._id })
@@ -516,6 +520,26 @@ module.exports = function(monastery, opendb) {
516
520
  last: 'Luther'
517
521
  })
518
522
 
523
+ // beforeUpdate/beforeInsert should have access to the original non-validated data
524
+ let user2 = db.model('user2', {
525
+ fields: {
526
+ first: { type: 'string' },
527
+ },
528
+ beforeInsert: [function (data, next) {
529
+ if (this.data.bad === true && !data.bad) next(new Error('error1'))
530
+ else next()
531
+ }],
532
+ beforeUpdate: [function (data, next) {
533
+ if (this.data.bad === true && !data.bad) next(new Error('error2'))
534
+ else next()
535
+ }],
536
+ })
537
+ let userDoc2 = await user2._insert({ first: 'M' })
538
+ await expect(user2.insert({ data: { first: 'M' } })).resolves.toMatchObject({ first: 'M' })
539
+ await expect(user2.insert({ data: { first: 'M', bad: true } })).rejects.toThrow('error1')
540
+ await expect(user2.update({ query: userDoc2._id, data: { first: 'M', } })).resolves.toEqual({ first: 'M' })
541
+ await expect(user2.update({ query: userDoc2._id, data: { first: 'M', bad: true } })).rejects.toThrow('error2')
542
+
519
543
  db.close()
520
544
  })
521
545
 
@@ -417,7 +417,7 @@ module.exports = function(monastery, opendb) {
417
417
  imageSvgBad: { type: 'image' },
418
418
  imageSvgGood: { type: 'image', formats: ['svg'] },
419
419
  imageSvgAny: { type: 'image', formats: ['any'] },
420
- imageSize1: { type: 'image', fileSize: 1000 * 100 },
420
+ imageSize1: { type: 'image', filesize: 1000 * 100 },
421
421
  imageSize2: { type: 'image' },
422
422
  }})
423
423