monastery 1.38.3 → 1.39.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 CHANGED
@@ -2,6 +2,12 @@
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.2](https://github.com/boycce/monastery/compare/1.39.1...1.39.2) (2022-09-12)
6
+
7
+ ### [1.39.1](https://github.com/boycce/monastery/compare/1.39.0...1.39.1) (2022-09-12)
8
+
9
+ ## [1.39.0](https://github.com/boycce/monastery/compare/1.38.3...1.39.0) (2022-09-11)
10
+
5
11
  ### [1.38.3](https://github.com/boycce/monastery/compare/1.38.2...1.38.3) (2022-08-17)
6
12
 
7
13
  ### [1.38.2](https://github.com/boycce/monastery/compare/1.38.1...1.38.2) (2022-07-31)
@@ -13,7 +13,7 @@ Setup model definitions from a folder location
13
13
 
14
14
  ### Returns
15
15
 
16
- An array of [model](../model) instances, the model instances will also be avaliable at:
16
+ A promise with an array of [model](../model) instances, the model instances will also be avaliable at:
17
17
  ```js
18
18
  db.{model-name}
19
19
  db.model.{model-name}
@@ -35,6 +35,6 @@ export default {
35
35
  ```
36
36
 
37
37
  ```js
38
- db.models(__dirname + "models")
38
+ await db.models(__dirname + "models")
39
39
  db.user.insert()
40
40
  ```
package/lib/index.js CHANGED
@@ -1,9 +1,12 @@
1
+ let fs = require('fs')
1
2
  let debug = require('debug')
2
3
  let monk = require('monk')
3
4
  let util = require('./util')
5
+ let path = require('path')
4
6
 
5
7
  // Apply monk monkey patches
6
8
  monk.manager.prototype.open = require('./monk-monkey-patches').open
9
+ monk.manager.prototype.then = require('./monk-monkey-patches').then
7
10
  monk.Collection.prototype.findOneAndUpdate = require('./monk-monkey-patches').findOneAndUpdate
8
11
 
9
12
  module.exports = function(uri, opts, fn) {
@@ -74,21 +77,23 @@ let arrayWithSchema = function(array, schema) {
74
77
  return array
75
78
  }
76
79
 
77
- let models = function(path) {
80
+ let models = async function(pathname) {
78
81
  /**
79
82
  * Setup model definitions from a folder location
80
83
  * @param {string} pathname
81
- * @return {object} - e.g. { user: , article: , .. }
84
+ * @return {Promise(object)} - e.g. { user: , article: , .. }
82
85
  * @this Manager
83
86
  */
84
- let models = {}
85
- if (!path || typeof path !== 'string') {
87
+ let out = {}
88
+ if (!pathname || typeof pathname !== 'string') {
86
89
  throw 'The path must be a valid pathname'
87
90
  }
88
- require('fs').readdirSync(path).forEach(filename => {
89
- 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)).default
90
95
  let name = filename.replace('.js', '')
91
- models[name] = this.model(name, definition)
92
- })
93
- return models
96
+ out[name] = this.model(name, definition)
97
+ }
98
+ return out
94
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.38.3",
5
+ "version": "1.39.2",
6
6
  "license": "MIT",
7
7
  "repository": "github:boycce/monastery",
8
8
  "homepage": "https://boycce.github.io/monastery/",