monastery 3.0.14 → 3.0.15

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,8 @@
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
+ ### [3.0.15](https://github.com/boycce/monastery/compare/3.0.14...3.0.15) (2024-05-01)
6
+
5
7
  ### [3.0.14](https://github.com/boycce/monastery/compare/3.0.13...3.0.14) (2024-05-01)
6
8
 
7
9
  ### [3.0.13](https://github.com/boycce/monastery/compare/3.0.12...3.0.13) (2024-05-01)
package/lib/model-crud.js CHANGED
@@ -48,9 +48,9 @@ Model.prototype.insert = async function (opts) {
48
48
  let data = await this.validate(opts.data || {}, opts) // was { ...opts }
49
49
 
50
50
  // Insert
51
- await util.runSeries(this.beforeInsert.map(f => f.bind(opts, data)), `${this.name}.beforeInsert`)
51
+ await util.runSeries.call(this, this.beforeInsert.map(f => f.bind(opts, data)), 'beforeInsert')
52
52
  let response = await this._insert(data, util.omit(opts, this._queryOptions))
53
- await util.runSeries(this.afterInsert.map(f => f.bind(opts, response)), `${this.name}.afterInsert`)
53
+ await util.runSeries.call(this, this.afterInsert.map(f => f.bind(opts, response)), 'afterInsert')
54
54
 
55
55
  // Success/error
56
56
  if (opts.req && opts.respond) opts.req.res.json(response)
@@ -267,7 +267,7 @@ Model.prototype.update = async function (opts, type='update') {
267
267
  }
268
268
 
269
269
  // Hook: beforeUpdate (has access to original, non-validated opts.data)
270
- await util.runSeries(this.beforeUpdate.map(f => f.bind(opts, data||{})), `${this.name}.beforeUpdate`)
270
+ await util.runSeries.call(this, this.beforeUpdate.map(f => f.bind(opts, data||{})), 'beforeUpdate')
271
271
 
272
272
  if (data && operators['$set']) {
273
273
  this.info(`'$set' fields take precedence over the data fields for \`${this.name}.${type}()\``)
@@ -298,7 +298,7 @@ Model.prototype.update = async function (opts, type='update') {
298
298
 
299
299
  // Hook: afterUpdate (doesn't have access to validated data)
300
300
  if (response) {
301
- await util.runSeries(this.afterUpdate.map(f => f.bind(opts, response)), `${this.name}.afterUpdate`)
301
+ await util.runSeries.call(this, this.afterUpdate.map(f => f.bind(opts, response)), 'afterUpdate')
302
302
  }
303
303
 
304
304
  // Hook: afterFind if findOneAndUpdate
@@ -331,9 +331,9 @@ Model.prototype.remove = async function (opts) {
331
331
  opts = await this._queryObject(opts, 'remove')
332
332
 
333
333
  // Remove
334
- await util.runSeries(this.beforeRemove.map(f => f.bind(opts)), `${this.name}.beforeRemove`)
334
+ await util.runSeries.call(this, this.beforeRemove.map(f => f.bind(opts)), 'beforeRemove')
335
335
  let response = await this._remove(opts.query, util.omit(opts, this._queryOptions))
336
- await util.runSeries(this.afterRemove.map(f => f.bind(response)), `${this.name}.afterRemove`)
336
+ await util.runSeries.call(this, this.afterRemove.map(f => f.bind(response)), 'afterRemove')
337
337
 
338
338
  // Success
339
339
  if (opts.req && opts.respond) opts.req.res.json(response)
@@ -591,7 +591,7 @@ Model.prototype._processAfterFind = function (data, projection={}, afterFindCont
591
591
  callbackSeries.push(fn.bind(afterFindContext, item.dataRef))
592
592
  }
593
593
  }
594
- return util.runSeries(callbackSeries, 'afterFind').then(() => data)
594
+ return util.runSeries.call(this, callbackSeries, 'afterFind').then(() => data)
595
595
  }
596
596
 
597
597
  Model.prototype._recurseAndFindModels = function (parentPath, schemaFields, dataArr) {
@@ -30,7 +30,7 @@ Model.prototype.validate = async function (data, opts) {
30
30
 
31
31
  // Hook: beforeValidate
32
32
 
33
- await util.runSeries(this.beforeValidate.map(f => f.bind(opts, data)), `${this.name}.beforeValidate`)
33
+ await util.runSeries.call(this, this.beforeValidate.map(f => f.bind(opts, data)), 'beforeValidate')
34
34
 
35
35
  // Recurse and validate fields
36
36
  let response = util.toArray(data).map(item => {
package/lib/util.js CHANGED
@@ -309,23 +309,25 @@ module.exports = {
309
309
  return variable
310
310
  },
311
311
 
312
- runSeries: function(tasks, info, cb) {
312
+ runSeries: function(tasks, hookName, cb) {
313
313
  /*
314
314
  * Runs functions in series and calls the cb when done
315
315
  * @param {function(err, result)[]} tasks - array of functions
316
- * @param {object} <info> - data to pass to the error
316
+ * @param {string} <hookName> - e.g. 'afterFind'
317
317
  * @param {function(err, results[])} <cb>
318
318
  * @return promise
319
+ * @this Model
319
320
  * @source https://github.com/feross/run-series
320
321
  */
321
322
  let current = 0
322
323
  let results = []
323
324
  let isSync = true
325
+ let caller = hookName == 'afterFind' ? 'afterFind' : this.name + '.' + hookName
324
326
 
325
327
  return new Promise((res, rej) => {
326
- function next(i, err, result) { // aka next(err, data)
328
+ const next = (i, err, result) => { // aka next(err, data)
327
329
  if (i !== current) {
328
- console.error(`Monastery ${info} error: you cannot return a promise AND call next()`)
330
+ this.manager.error(`Monastery ${caller} error: you cannot return a promise AND call next()`)
329
331
  return
330
332
  }
331
333
  current++
@@ -333,16 +335,16 @@ module.exports = {
333
335
  if (!err && current < tasks.length) callTask(current)
334
336
  else done(err)
335
337
  }
336
- function done(err) {
338
+ const done = (err) => {
337
339
  if (isSync) process.nextTick(() => end(err))
338
340
  else end(err)
339
341
  }
340
- function end(err) {
342
+ const end = (err) => {
341
343
  if (cb) cb(err, results)
342
344
  if (err) rej(err)
343
345
  else res(results)
344
346
  }
345
- function callTask(i) {
347
+ const callTask = (i) => {
346
348
  const next2 = next.bind(null, i)
347
349
  const res = tasks[i](next2)
348
350
  if (res instanceof Promise) {
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "monastery",
3
3
  "description": "⛪ A simple, straightforward MongoDB ODM",
4
4
  "author": "Ricky Boyce",
5
- "version": "3.0.14",
5
+ "version": "3.0.15",
6
6
  "license": "MIT",
7
7
  "repository": "github:boycce/monastery",
8
8
  "homepage": "https://boycce.github.io/monastery/",
package/test/crud.js CHANGED
@@ -859,7 +859,8 @@ test('hooks > async', async () => {
859
859
  })
860
860
 
861
861
  test('hooks > async and next conflict', async () => {
862
- let user1 = db.model('user', {
862
+ const db2 = monastery('127.0.0.1/monastery', { timestamps: false })
863
+ let user1 = db2.model('user', {
863
864
  fields: { age: { type: 'number'} },
864
865
  afterFind: [
865
866
  async (data, next) => {
@@ -881,7 +882,7 @@ test('hooks > async and next conflict', async () => {
881
882
  },
882
883
  ],
883
884
  })
884
- let user2 = db.model('user2', {
885
+ let user2 = db2.model('user2', {
885
886
  fields: { age: { type: 'number'} },
886
887
  afterFind: [
887
888
  async (data, next) => {
@@ -898,7 +899,7 @@ test('hooks > async and next conflict', async () => {
898
899
  },
899
900
  ],
900
901
  })
901
- let user3 = db.model('user3', {
902
+ let user3 = db2.model('user3', {
902
903
  fields: { age: { type: 'number'} },
903
904
  afterFind: [
904
905
  async (data, next) => {
@@ -910,7 +911,7 @@ test('hooks > async and next conflict', async () => {
910
911
  },
911
912
  ],
912
913
  })
913
- let user4 = db.model('user4', {
914
+ let user4 = db2.model('user4', {
914
915
  fields: { age: { type: 'number'} },
915
916
  afterFind: [
916
917
  async (data, next) => {
@@ -928,7 +929,7 @@ test('hooks > async and next conflict', async () => {
928
929
  ],
929
930
  })
930
931
 
931
- let user5 = db.model('user5', {
932
+ let user5 = db2.model('user5', {
932
933
  fields: { age: { type: 'number'} },
933
934
  afterFind: [
934
935
  async (data, next) => {
@@ -949,7 +950,7 @@ test('hooks > async and next conflict', async () => {
949
950
  let user4Doc = await user4.insert({ data: { age: 0 } })
950
951
  let user5Doc = await user5.insert({ data: { age: 0 } })
951
952
 
952
- const logSpy = jest.spyOn(console, 'error').mockImplementation(() => {})
953
+ const logSpy = jest.spyOn(db2, 'error').mockImplementation(() => {})
953
954
 
954
955
  // Only increment twice
955
956
  await expect(user1.find({ query: user1Doc._id })).resolves.toEqual({ _id: expect.any(Object), age: 2 })
@@ -967,4 +968,5 @@ test('hooks > async and next conflict', async () => {
967
968
  await expect(user5.find({ query: user5Doc._id })).rejects.toThrow('An async error occurred with Martin3')
968
969
  expect(logSpy).toHaveBeenCalledWith('Monastery afterFind error: you cannot return a promise AND call next()')
969
970
 
971
+ db2.close()
970
972
  })