monastery 1.39.0 → 1.39.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.
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
+ ### [1.39.1](https://github.com/boycce/monastery/compare/1.39.0...1.39.1) (2022-09-12)
6
+
5
7
  ## [1.39.0](https://github.com/boycce/monastery/compare/1.38.3...1.39.0) (2022-09-11)
6
8
 
7
9
  ### [1.38.3](https://github.com/boycce/monastery/compare/1.38.2...1.38.3) (2022-08-17)
package/lib/index.js CHANGED
@@ -2,9 +2,11 @@ let fs = require('fs')
2
2
  let debug = require('debug')
3
3
  let monk = require('monk')
4
4
  let util = require('./util')
5
+ let path = require('path')
5
6
 
6
7
  // Apply monk monkey patches
7
8
  monk.manager.prototype.open = require('./monk-monkey-patches').open
9
+ monk.manager.prototype.then = require('./monk-monkey-patches').then
8
10
  monk.Collection.prototype.findOneAndUpdate = require('./monk-monkey-patches').findOneAndUpdate
9
11
 
10
12
  module.exports = function(uri, opts, fn) {
@@ -75,22 +77,23 @@ let arrayWithSchema = function(array, schema) {
75
77
  return array
76
78
  }
77
79
 
78
- let models = async function(path) {
80
+ let models = async function(pathname) {
79
81
  /**
80
82
  * Setup model definitions from a folder location
81
83
  * @param {string} pathname
82
84
  * @return {Promise(object)} - e.g. { user: , article: , .. }
83
85
  * @this Manager
84
86
  */
85
- let models = {}
86
- if (!path || typeof path !== 'string') {
87
+ let out = {}
88
+ if (!pathname || typeof pathname !== 'string') {
87
89
  throw 'The path must be a valid pathname'
88
90
  }
89
- await fs.readdirSync(path).forEach(async filename => {
90
- let definition = await import(require('path').join(path, filename))
91
- // let definition = require(require('path').join(path, filename)).default
91
+ let filenames = fs.readdirSync(pathname)
92
+ for (let filename of filenames) {
93
+ let filepath = path.join(pathname, filename)
94
+ let definition = await import(filepath)
92
95
  let name = filename.replace('.js', '')
93
- models[name] = this.model(name, definition)
94
- })
95
- return models
96
+ out[name] = this.model(name, definition)
97
+ }
98
+ return out
96
99
  }
@@ -1,4 +1,9 @@
1
1
  let MongoClient = require('mongodb').MongoClient
2
+ let STATE = {
3
+ CLOSED: 'closed',
4
+ OPENING: 'opening',
5
+ OPEN: 'open'
6
+ }
2
7
 
3
8
  module.exports.open = function(uri, opts, fn) {
4
9
  /*
@@ -7,11 +12,6 @@ module.exports.open = function(uri, opts, fn) {
7
12
  * @see https://www.mongodb.com/community/forums/t/node-44612-deprecationwarning-listening-to-events-on-
8
13
  the-db-class-has-been-deprecated-and-will-be-removed-in-the-next-major-version/15849/4
9
14
  */
10
- var STATE = {
11
- CLOSED: 'closed',
12
- OPENING: 'opening',
13
- OPEN: 'open'
14
- }
15
15
  MongoClient.connect(uri, opts, function (err, client) {
16
16
  // this = Manager
17
17
  if (err) {
@@ -39,6 +39,23 @@ module.exports.open = function(uri, opts, fn) {
39
39
  }.bind(this))
40
40
  }
41
41
 
42
+ module.exports.then = function (fn) {
43
+ /*
44
+ * Unfortuantly db.then doesn't run after the promise has been fulfilled, this fixes this issue.
45
+ * @see https://github.com/Automattic/monk/blob/master/lib/manager.js#L194
46
+ */
47
+ return new Promise(function (resolve, reject) {
48
+ if (this._state == STATE.OPEN) {
49
+ resolve()
50
+ } else if (this._state == STATE.CLOSED) {
51
+ reject()
52
+ } else {
53
+ this.once('open', resolve)
54
+ this.once('error-opening', reject)
55
+ }
56
+ }.bind(this)).then(fn.bind(null, this))
57
+ }
58
+
42
59
  module.exports.findOneAndUpdate = function(query, update, opts, fn) {
43
60
  /*
44
61
  * Monkey patch to use returnDocument
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.39.0",
5
+ "version": "1.39.1",
6
6
  "license": "MIT",
7
7
  "repository": "github:boycce/monastery",
8
8
  "homepage": "https://boycce.github.io/monastery/",