monastery 3.0.3 → 3.0.5
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 +4 -0
- package/docs/manager/index.md +13 -14
- package/lib/index.js +44 -18
- package/package.json +1 -1
- package/test/index.test.js +2 -1
- package/test/manager.js +43 -10
package/changelog.md
CHANGED
|
@@ -2,6 +2,10 @@
|
|
|
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.5](https://github.com/boycce/monastery/compare/3.0.4...3.0.5) (2024-04-29)
|
|
6
|
+
|
|
7
|
+
### [3.0.4](https://github.com/boycce/monastery/compare/3.0.3...3.0.4) (2024-04-29)
|
|
8
|
+
|
|
5
9
|
### [3.0.3](https://github.com/boycce/monastery/compare/3.0.2...3.0.3) (2024-04-28)
|
|
6
10
|
|
|
7
11
|
### [3.0.2](https://github.com/boycce/monastery/compare/3.0.1...3.0.2) (2024-04-28)
|
package/docs/manager/index.md
CHANGED
|
@@ -30,24 +30,22 @@ A manager instance.
|
|
|
30
30
|
|
|
31
31
|
```js
|
|
32
32
|
import monastery from 'monastery'
|
|
33
|
-
const db = monastery('localhost/mydb', options)
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
```js
|
|
37
|
-
import monastery from 'monastery'
|
|
38
|
-
const db = monastery('localhost/mydb,192.168.1.1') // replica set
|
|
39
|
-
```
|
|
40
33
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
34
|
+
const db = monastery('localhost/mydb', options)
|
|
35
|
+
// replica set
|
|
36
|
+
const db = monastery('localhost/mydb,192.168.1.1')
|
|
37
|
+
// you can wait for the connection (which is not required before calling methods)
|
|
44
38
|
const db = await monastery('localhost/mydb,192.168.1.1', { promise: true })
|
|
45
39
|
```
|
|
46
40
|
|
|
41
|
+
You can also listen for errors or successful connection using these hooks
|
|
47
42
|
```js
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
43
|
+
db.onOpen((manager) => {
|
|
44
|
+
// manager.client is connected...
|
|
45
|
+
})
|
|
46
|
+
db.onError((err) => {
|
|
47
|
+
// connection error
|
|
48
|
+
})
|
|
51
49
|
```
|
|
52
50
|
|
|
53
51
|
### Properties
|
|
@@ -57,11 +55,12 @@ const db = monastery('localhost/mydb,192.168.1.1').catch(err => {})
|
|
|
57
55
|
|
|
58
56
|
### Methods
|
|
59
57
|
|
|
60
|
-
- `manager.catch(Function)`: Catches connection errors
|
|
61
58
|
- `manager.id(<String|ObjectId>)`: Create or convert a valid MongoDB ObjectId string into an ObjectId
|
|
62
59
|
- `manager.isId(String|ObjectId)`: Checks if the passed variable is a valid MongoDB ObjectId or ObjectId string
|
|
63
60
|
- `manager.model()`: [see model](./model.html)
|
|
64
61
|
- `manager.models()`: [see models](./models.html)
|
|
62
|
+
- `manager.onError(Function)`: Catches connection errors
|
|
63
|
+
- `manager.onOpen(Function)`: Triggers on successful connection
|
|
65
64
|
|
|
66
65
|
### Dates
|
|
67
66
|
|
package/lib/index.js
CHANGED
|
@@ -44,6 +44,7 @@ function Manager(uri, opts) {
|
|
|
44
44
|
}, {})
|
|
45
45
|
|
|
46
46
|
// Add properties
|
|
47
|
+
this.uri = uri
|
|
47
48
|
this.info = debug('monastery:info') // debug doesn't allow debuggers to be enabled by default
|
|
48
49
|
this.warn = debug('monastery:warn' + (opts.hideWarnings ? '' : '*'))
|
|
49
50
|
this.error = debug('monastery:error' + (opts.hideErrors ? '' : '*'))
|
|
@@ -53,11 +54,11 @@ function Manager(uri, opts) {
|
|
|
53
54
|
this._openQueue = []
|
|
54
55
|
|
|
55
56
|
// Create a new MongoDB Client
|
|
56
|
-
if (typeof uri == 'string' || Array.isArray(uri)) {
|
|
57
|
-
uri = this.connectionString(uri, opts.databaseName)
|
|
58
|
-
this.client = new MongoClient(uri, mongoOpts)
|
|
57
|
+
if (typeof this.uri == 'string' || Array.isArray(this.uri)) {
|
|
58
|
+
this.uri = this.connectionString(this.uri, opts.databaseName)
|
|
59
|
+
this.client = new MongoClient(this.uri, mongoOpts)
|
|
59
60
|
} else {
|
|
60
|
-
this.client = uri
|
|
61
|
+
this.client = this.uri
|
|
61
62
|
}
|
|
62
63
|
|
|
63
64
|
// Listen to MongoDB events
|
|
@@ -93,17 +94,6 @@ Manager.prototype.arrayWithSchema = function(array, schema) {
|
|
|
93
94
|
return array
|
|
94
95
|
}
|
|
95
96
|
|
|
96
|
-
Manager.prototype.catch = function(fn) {
|
|
97
|
-
/**
|
|
98
|
-
* Catch any errors that occur during the opening of the database, and still return the manager
|
|
99
|
-
* @param {Function} fn
|
|
100
|
-
* @return {Manager}
|
|
101
|
-
*/
|
|
102
|
-
this.catching = true
|
|
103
|
-
this.on('error', fn)
|
|
104
|
-
return this
|
|
105
|
-
}
|
|
106
|
-
|
|
107
97
|
Manager.prototype.close = async function() {
|
|
108
98
|
/**
|
|
109
99
|
* Close the database connection, gracefully
|
|
@@ -214,10 +204,46 @@ Manager.prototype.models = async function(pathname, waitForIndexes) {
|
|
|
214
204
|
return out
|
|
215
205
|
}
|
|
216
206
|
|
|
207
|
+
Manager.prototype.onError = function(fn) {
|
|
208
|
+
/**
|
|
209
|
+
* Called when an error occurs when trying to connect to the MongoClient.
|
|
210
|
+
* @param {Function} fn
|
|
211
|
+
* @return {Promise}
|
|
212
|
+
*/
|
|
213
|
+
return new Promise((resolve, reject) => {
|
|
214
|
+
this.on('error', (err) => {
|
|
215
|
+
resolve(err)
|
|
216
|
+
})
|
|
217
|
+
}).then((err) => {
|
|
218
|
+
return fn(err)
|
|
219
|
+
})
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
Manager.prototype.onOpen = function(fn) {
|
|
223
|
+
/**
|
|
224
|
+
* Called when a successful MongoClient connection has been made.
|
|
225
|
+
* @param {Function} fn
|
|
226
|
+
* @return {Promise(manager)}
|
|
227
|
+
*/
|
|
228
|
+
return new Promise((resolve, reject) => {
|
|
229
|
+
if (this._state == 'open') {
|
|
230
|
+
resolve(this)
|
|
231
|
+
}
|
|
232
|
+
this.on('open', () => {
|
|
233
|
+
resolve(this)
|
|
234
|
+
})
|
|
235
|
+
this.on('error', (err) => {
|
|
236
|
+
reject(err)
|
|
237
|
+
})
|
|
238
|
+
}).then((err) => { // If `then` is not chained, the error will be thrown, detached!
|
|
239
|
+
return fn(err)
|
|
240
|
+
})
|
|
241
|
+
}
|
|
242
|
+
|
|
217
243
|
Manager.prototype.open = async function() {
|
|
218
244
|
/**
|
|
219
245
|
* Connect to the database
|
|
220
|
-
* @return {Promise}
|
|
246
|
+
* @return {Promise(manager)}
|
|
221
247
|
*/
|
|
222
248
|
try {
|
|
223
249
|
this._state = 'opening'
|
|
@@ -232,8 +258,8 @@ Manager.prototype.open = async function() {
|
|
|
232
258
|
if (this.listeners('error').length > 0) {
|
|
233
259
|
this.emit('error', err)
|
|
234
260
|
}
|
|
235
|
-
if (
|
|
236
|
-
throw err
|
|
261
|
+
if (this.opts.promise || this.listeners('error').length == 0) {
|
|
262
|
+
throw new Error(err)
|
|
237
263
|
}
|
|
238
264
|
}
|
|
239
265
|
}
|
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.
|
|
5
|
+
"version": "3.0.5",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": "github:boycce/monastery",
|
|
8
8
|
"homepage": "https://boycce.github.io/monastery/",
|
package/test/index.test.js
CHANGED
|
@@ -10,6 +10,8 @@
|
|
|
10
10
|
* - expect().toEqual: strict deep match
|
|
11
11
|
* - expect().toMatchObject: received object can have random properties (try not to use since its not strict)
|
|
12
12
|
* - expect.objectContaining:
|
|
13
|
+
* - try/catch blocks: don't use - it doesn't work with resolves/rejects
|
|
14
|
+
* - promise testing: await expect(PromiseHere).resolves.toEqual
|
|
13
15
|
*/
|
|
14
16
|
|
|
15
17
|
/* Run tests sequentially */
|
|
@@ -19,7 +21,6 @@ require('./collection.js')
|
|
|
19
21
|
require('./model.js')
|
|
20
22
|
require('./crud.js')
|
|
21
23
|
require('./blacklisting.js')
|
|
22
|
-
|
|
23
24
|
require('./populate.js')
|
|
24
25
|
require('./validate.js')
|
|
25
26
|
require('./plugin-images.js')
|
package/test/manager.js
CHANGED
|
@@ -16,33 +16,66 @@ test('manager > uri error', async () => {
|
|
|
16
16
|
expect(() => monastery('', {})).toThrow('No connection URI provided.')
|
|
17
17
|
})
|
|
18
18
|
|
|
19
|
-
test('manager >
|
|
19
|
+
test('manager > onError', async () => {
|
|
20
20
|
// Bad port (thrown by MongoDB)
|
|
21
|
-
const db = monastery('localhost:1234/monastery', { serverSelectionTimeoutMS:
|
|
22
|
-
|
|
21
|
+
const db = monastery('localhost:1234/monastery', { serverSelectionTimeoutMS: 500 })
|
|
22
|
+
let error, isAPromise
|
|
23
|
+
await db.onError((res) => { error = res.message }).then(() => { isAPromise = true })
|
|
24
|
+
expect(error).toEqual('connect ECONNREFUSED 127.0.0.1:1234')
|
|
25
|
+
expect(isAPromise).toEqual(true)
|
|
26
|
+
db.close()
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
test('manager > onOpen', async () => {
|
|
30
|
+
const db = monastery('localhost/monastery', { serverSelectionTimeoutMS: 500 })
|
|
31
|
+
|
|
32
|
+
let manager1
|
|
33
|
+
await db.onOpen((res) => { manager1 = res })
|
|
34
|
+
expect(manager1.open).toEqual(expect.any(Function))
|
|
35
|
+
|
|
36
|
+
// Wait until after the client has been connected
|
|
37
|
+
await new Promise((resolve, reject) => {
|
|
38
|
+
setTimeout(() => {
|
|
39
|
+
resolve()
|
|
40
|
+
}, 1000)
|
|
23
41
|
})
|
|
24
|
-
|
|
25
|
-
|
|
42
|
+
|
|
43
|
+
// This should still run after the client has been connected
|
|
44
|
+
let manager2
|
|
45
|
+
let isAPromise
|
|
46
|
+
await db.onOpen((res) => { manager2 = res }).then(() => { isAPromise = true })
|
|
47
|
+
expect(manager2.open).toEqual(expect.any(Function))
|
|
48
|
+
expect(isAPromise).toEqual(true)
|
|
49
|
+
db.close()
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
test('manager > onOpen error', async () => {
|
|
53
|
+
// Bad port (thrown by MongoDB)
|
|
54
|
+
let manager
|
|
55
|
+
const db = monastery('localhost:1234/monastery', { serverSelectionTimeoutMS: 500 })
|
|
56
|
+
await expect(db.onOpen((res) => { manager = res })).rejects.toThrow('connect ECONNREFUSED 127.0.0.1:1234')
|
|
57
|
+
expect(manager).toEqual(undefined)
|
|
58
|
+
expect(db).toEqual(expect.any(Object))
|
|
26
59
|
db.close()
|
|
27
60
|
})
|
|
28
61
|
|
|
29
62
|
test('manager > return a promise', async () => {
|
|
30
|
-
const db = await monastery('localhost/monastery', { serverSelectionTimeoutMS:
|
|
63
|
+
const db = await monastery('localhost/monastery', { serverSelectionTimeoutMS: 500, promise: true })
|
|
31
64
|
expect(db).toEqual(expect.any(Object))
|
|
32
65
|
db.close()
|
|
33
66
|
})
|
|
34
67
|
|
|
35
68
|
test('manager > return a promise with uri error', async () => {
|
|
36
|
-
await expect(monastery('badlocalhost/monastery', { serverSelectionTimeoutMS:
|
|
69
|
+
await expect(monastery('badlocalhost/monastery', { serverSelectionTimeoutMS: 500, promise: true }))
|
|
37
70
|
.rejects.toThrow('getaddrinfo EAI_AGAIN badlocalhost')
|
|
38
71
|
})
|
|
39
72
|
|
|
40
73
|
test('manager > reuse MongoDB Client', async () => {
|
|
41
74
|
const mongoClient = new MongoClient('mongodb://localhost/monastery', {})
|
|
42
|
-
const db = await monastery(mongoClient, { serverSelectionTimeoutMS:
|
|
75
|
+
const db = await monastery(mongoClient, { serverSelectionTimeoutMS: 500, promise: true })
|
|
43
76
|
expect(db).toEqual(expect.any(Object))
|
|
44
77
|
expect(db.client).toEqual(expect.any(Object))
|
|
45
|
-
expect(db.
|
|
78
|
+
expect(db.isId).toEqual(expect.any(Function))
|
|
46
79
|
db.close()
|
|
47
80
|
})
|
|
48
81
|
|
|
@@ -68,7 +101,7 @@ test('manager > events', async () => {
|
|
|
68
101
|
})
|
|
69
102
|
|
|
70
103
|
test('Manager > get collection', async () => {
|
|
71
|
-
const manager = monastery('localhost/monastery', { serverSelectionTimeoutMS:
|
|
104
|
+
const manager = monastery('localhost/monastery', { serverSelectionTimeoutMS: 500 })
|
|
72
105
|
// Basic aggregate command
|
|
73
106
|
expect(await manager.get('non-collection').aggregate([], {})).toEqual([])
|
|
74
107
|
// Basic find command
|