monastery 3.0.3 → 3.0.4
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 +2 -0
- package/docs/manager/index.md +13 -14
- package/lib/index.js +41 -18
- package/package.json +1 -1
- package/test/index.test.js +2 -1
- package/test/manager.js +28 -11
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.4](https://github.com/boycce/monastery/compare/3.0.3...3.0.4) (2024-04-29)
|
|
6
|
+
|
|
5
7
|
### [3.0.3](https://github.com/boycce/monastery/compare/3.0.2...3.0.3) (2024-04-28)
|
|
6
8
|
|
|
7
9
|
### [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,43 @@ 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
|
+
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
|
+
this.on('open', () => {
|
|
230
|
+
resolve(this)
|
|
231
|
+
})
|
|
232
|
+
this.on('error', (err) => {
|
|
233
|
+
reject(err)
|
|
234
|
+
})
|
|
235
|
+
}).then((err) => { // If `then` is not chained, the error will be thrown, detached!
|
|
236
|
+
fn(err)
|
|
237
|
+
})
|
|
238
|
+
}
|
|
239
|
+
|
|
217
240
|
Manager.prototype.open = async function() {
|
|
218
241
|
/**
|
|
219
242
|
* Connect to the database
|
|
220
|
-
* @return {Promise}
|
|
243
|
+
* @return {Promise(manager)}
|
|
221
244
|
*/
|
|
222
245
|
try {
|
|
223
246
|
this._state = 'opening'
|
|
@@ -232,8 +255,8 @@ Manager.prototype.open = async function() {
|
|
|
232
255
|
if (this.listeners('error').length > 0) {
|
|
233
256
|
this.emit('error', err)
|
|
234
257
|
}
|
|
235
|
-
if (
|
|
236
|
-
throw err
|
|
258
|
+
if (this.opts.promise || this.listeners('error').length == 0) {
|
|
259
|
+
throw new Error(err)
|
|
237
260
|
}
|
|
238
261
|
}
|
|
239
262
|
}
|
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.4",
|
|
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,50 @@ 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
|
-
|
|
22
|
-
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
|
|
21
|
+
let error
|
|
22
|
+
const db = monastery('localhost:1234/monastery', { serverSelectionTimeoutMS: 500 })
|
|
23
|
+
await db.onError((res) => { error = res.message })
|
|
24
|
+
expect(error).toEqual('connect ECONNREFUSED 127.0.0.1:1234')
|
|
25
|
+
db.close()
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
test('manager > onOpen', async () => {
|
|
29
|
+
let manager
|
|
30
|
+
const db = monastery('localhost/monastery', { serverSelectionTimeoutMS: 500 })
|
|
31
|
+
await db.onOpen((res) => { manager = res })
|
|
32
|
+
expect(manager.open).toEqual(expect.any(Function))
|
|
33
|
+
db.close()
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
test('manager > onOpen error', async () => {
|
|
37
|
+
// Bad port (thrown by MongoDB)
|
|
38
|
+
let manager
|
|
39
|
+
const db = monastery('localhost:1234/monastery', { serverSelectionTimeoutMS: 500 })
|
|
40
|
+
await expect(db.onOpen((res) => { manager = res })).rejects.toThrow('connect ECONNREFUSED 127.0.0.1:1234')
|
|
41
|
+
expect(manager).toEqual(undefined)
|
|
42
|
+
expect(db).toEqual(expect.any(Object))
|
|
26
43
|
db.close()
|
|
27
44
|
})
|
|
28
45
|
|
|
29
46
|
test('manager > return a promise', async () => {
|
|
30
|
-
const db = await monastery('localhost/monastery', { serverSelectionTimeoutMS:
|
|
47
|
+
const db = await monastery('localhost/monastery', { serverSelectionTimeoutMS: 500, promise: true })
|
|
31
48
|
expect(db).toEqual(expect.any(Object))
|
|
32
49
|
db.close()
|
|
33
50
|
})
|
|
34
51
|
|
|
35
52
|
test('manager > return a promise with uri error', async () => {
|
|
36
|
-
await expect(monastery('badlocalhost/monastery', { serverSelectionTimeoutMS:
|
|
53
|
+
await expect(monastery('badlocalhost/monastery', { serverSelectionTimeoutMS: 500, promise: true }))
|
|
37
54
|
.rejects.toThrow('getaddrinfo EAI_AGAIN badlocalhost')
|
|
38
55
|
})
|
|
39
56
|
|
|
40
57
|
test('manager > reuse MongoDB Client', async () => {
|
|
41
58
|
const mongoClient = new MongoClient('mongodb://localhost/monastery', {})
|
|
42
|
-
const db = await monastery(mongoClient, { serverSelectionTimeoutMS:
|
|
59
|
+
const db = await monastery(mongoClient, { serverSelectionTimeoutMS: 500, promise: true })
|
|
43
60
|
expect(db).toEqual(expect.any(Object))
|
|
44
61
|
expect(db.client).toEqual(expect.any(Object))
|
|
45
|
-
expect(db.
|
|
62
|
+
expect(db.isId).toEqual(expect.any(Function))
|
|
46
63
|
db.close()
|
|
47
64
|
})
|
|
48
65
|
|
|
@@ -68,7 +85,7 @@ test('manager > events', async () => {
|
|
|
68
85
|
})
|
|
69
86
|
|
|
70
87
|
test('Manager > get collection', async () => {
|
|
71
|
-
const manager = monastery('localhost/monastery', { serverSelectionTimeoutMS:
|
|
88
|
+
const manager = monastery('localhost/monastery', { serverSelectionTimeoutMS: 500 })
|
|
72
89
|
// Basic aggregate command
|
|
73
90
|
expect(await manager.get('non-collection').aggregate([], {})).toEqual([])
|
|
74
91
|
// Basic find command
|