monastery 1.32.1 → 1.32.2
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 +7 -0
- package/lib/model-crud.js +13 -12
- package/package.json +1 -1
- package/test/crud.js +20 -0
package/changelog.md
CHANGED
|
@@ -2,6 +2,13 @@
|
|
|
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.2](https://github.com/boycce/monastery/compare/1.32.1...1.32.2) (2022-03-04)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
* missing original non-validated this.data in beforeUpdate ([4b4002d](https://github.com/boycce/monastery/commit/4b4002d3d4d2609025cbb22cacecf948252be38b))
|
|
11
|
+
|
|
5
12
|
### [1.32.1](https://github.com/boycce/monastery/compare/1.32.0...1.32.1) (2022-03-01)
|
|
6
13
|
|
|
7
14
|
|
package/lib/model-crud.js
CHANGED
|
@@ -28,11 +28,11 @@ module.exports = {
|
|
|
28
28
|
])
|
|
29
29
|
|
|
30
30
|
// Validate
|
|
31
|
-
|
|
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,
|
|
35
|
-
let response = await this._insert(
|
|
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(
|
|
202
|
-
if (!util.isDefined(
|
|
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(
|
|
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,
|
|
210
|
-
if (
|
|
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 (
|
|
214
|
-
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
|
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.
|
|
5
|
+
"version": "1.32.2",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": "github:boycce/monastery",
|
|
8
8
|
"homepage": "https://boycce.github.io/monastery/",
|
package/test/crud.js
CHANGED
|
@@ -516,6 +516,26 @@ module.exports = function(monastery, opendb) {
|
|
|
516
516
|
last: 'Luther'
|
|
517
517
|
})
|
|
518
518
|
|
|
519
|
+
// beforeUpdate/beforeInsert should have access to the original non-validated data
|
|
520
|
+
let user2 = db.model('user2', {
|
|
521
|
+
fields: {
|
|
522
|
+
first: { type: 'string' },
|
|
523
|
+
},
|
|
524
|
+
beforeInsert: [function (data, next) {
|
|
525
|
+
if (this.data.bad === true && !data.bad) next(new Error('error1'))
|
|
526
|
+
else next()
|
|
527
|
+
}],
|
|
528
|
+
beforeUpdate: [function (data, next) {
|
|
529
|
+
if (this.data.bad === true && !data.bad) next(new Error('error2'))
|
|
530
|
+
else next()
|
|
531
|
+
}],
|
|
532
|
+
})
|
|
533
|
+
let userDoc2 = await user2._insert({ first: 'M' })
|
|
534
|
+
await expect(user2.insert({ data: { first: 'M' } })).resolves.toMatchObject({ first: 'M' })
|
|
535
|
+
await expect(user2.insert({ data: { first: 'M', bad: true } })).rejects.toThrow('error1')
|
|
536
|
+
await expect(user2.update({ query: userDoc2._id, data: { first: 'M', } })).resolves.toEqual({ first: 'M' })
|
|
537
|
+
await expect(user2.update({ query: userDoc2._id, data: { first: 'M', bad: true } })).rejects.toThrow('error2')
|
|
538
|
+
|
|
519
539
|
db.close()
|
|
520
540
|
})
|
|
521
541
|
|