monastery 3.0.6 → 3.0.8
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/readme.md +1 -0
- package/lib/collection.js +7 -7
- package/lib/index.js +2 -2
- package/lib/model.js +1 -1
- package/lib/util.js +3 -2
- package/package.json +1 -1
- package/test/crud.js +22 -1
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.8](https://github.com/boycce/monastery/compare/3.0.7...3.0.8) (2024-04-30)
|
|
6
|
+
|
|
7
|
+
### [3.0.7](https://github.com/boycce/monastery/compare/3.0.5...3.0.7) (2024-04-29)
|
|
8
|
+
|
|
5
9
|
### [3.0.6](https://github.com/boycce/monastery/compare/3.0.5...3.0.6) (2024-04-29)
|
|
6
10
|
|
|
7
11
|
### [3.0.5](https://github.com/boycce/monastery/compare/3.0.4...3.0.5) (2024-04-29)
|
package/docs/readme.md
CHANGED
|
@@ -92,6 +92,7 @@ You can view MongoDB's [compatibility table here](https://www.mongodb.com/docs/d
|
|
|
92
92
|
- db._client moved to db.client
|
|
93
93
|
- db._db moved to db.db
|
|
94
94
|
- db.catch/then() moved to db.onError/db.onOpen()
|
|
95
|
+
- next() is now redundant when returning promises from hooks, e.g. `afterFind: [async (data) => {...}]`
|
|
95
96
|
|
|
96
97
|
## Roadmap
|
|
97
98
|
|
package/lib/collection.js
CHANGED
|
@@ -114,7 +114,7 @@ Collection.prototype.drop = async function () {
|
|
|
114
114
|
await this.col.drop()
|
|
115
115
|
// this.col = null
|
|
116
116
|
} catch (err) {
|
|
117
|
-
if (err
|
|
117
|
+
if ((err||{}).message == 'ns not found') return 'ns not found'
|
|
118
118
|
throw err
|
|
119
119
|
}
|
|
120
120
|
}
|
|
@@ -186,7 +186,7 @@ Collection.prototype.find = async function (query, opts) {
|
|
|
186
186
|
Collection.prototype.findOne = async function (query, opts) {
|
|
187
187
|
const args = await this._middleware({ query, opts })
|
|
188
188
|
const docs = await this.col.find(args.query, args.opts).limit(1).toArray()
|
|
189
|
-
return docs
|
|
189
|
+
return (docs||{})[0] || null
|
|
190
190
|
}
|
|
191
191
|
|
|
192
192
|
Collection.prototype.findOneAndDelete = async function (query, opts) {
|
|
@@ -202,10 +202,10 @@ Collection.prototype.findOneAndUpdate = async function (query, update, opts) {
|
|
|
202
202
|
const args = await this._middleware({ query, update, opts })
|
|
203
203
|
let method = 'findOneAndUpdate'
|
|
204
204
|
|
|
205
|
-
if (typeof args.opts
|
|
205
|
+
if (typeof (args.opts||{}).returnDocument === 'undefined') {
|
|
206
206
|
args.opts.returnDocument = 'after'
|
|
207
207
|
}
|
|
208
|
-
if (typeof args.opts
|
|
208
|
+
if (typeof (args.opts||{}).returnOriginal !== 'undefined') {
|
|
209
209
|
this.manager.warn('The `returnOriginal` option is deprecated, use `returnDocument` instead.')
|
|
210
210
|
args.opts.returnDocument = args.opts.returnOriginal ? 'before' : 'after'
|
|
211
211
|
}
|
|
@@ -230,7 +230,7 @@ Collection.prototype.indexInformation = async function (opts) {
|
|
|
230
230
|
return await this.col.indexInformation(args.opts)
|
|
231
231
|
} catch (e) {
|
|
232
232
|
// col.indexInformation() throws an error if the collection is created yet...
|
|
233
|
-
if (e
|
|
233
|
+
if ((e||{}).message.match(/ns does not exist/)) return {}
|
|
234
234
|
else throw new Error(e)
|
|
235
235
|
}
|
|
236
236
|
}
|
|
@@ -241,7 +241,7 @@ Collection.prototype.indexes = async function (opts) {
|
|
|
241
241
|
return await this.col.indexes(args.opts)
|
|
242
242
|
} catch (e) {
|
|
243
243
|
// col.indexes() throws an error if the collection is created yet...
|
|
244
|
-
if (e
|
|
244
|
+
if ((e||{}).message.match(/ns does not exist/)) return []
|
|
245
245
|
else throw new Error(e)
|
|
246
246
|
}
|
|
247
247
|
}
|
|
@@ -317,7 +317,7 @@ Collection.prototype.update = async function (query, update, opts) {
|
|
|
317
317
|
}
|
|
318
318
|
|
|
319
319
|
const doc = await this.col[method](args.query, args.update, args.opts)
|
|
320
|
-
// return doc
|
|
320
|
+
// return (doc||{}).result || doc
|
|
321
321
|
return doc
|
|
322
322
|
}
|
|
323
323
|
|
package/lib/index.js
CHANGED
|
@@ -159,8 +159,8 @@ Manager.prototype.get = function(name, options) {
|
|
|
159
159
|
* @param {Object} [options] - options to pass to the collection
|
|
160
160
|
* @return {MongoDB Collection}
|
|
161
161
|
*/
|
|
162
|
-
if (!this.collections[name] || options
|
|
163
|
-
delete options
|
|
162
|
+
if (!this.collections[name] || (options||{}).cache === false) {
|
|
163
|
+
delete (options||{}).cache
|
|
164
164
|
this.collections[name] = new Collection(this, name, options)
|
|
165
165
|
}
|
|
166
166
|
return this.collections[name]
|
package/lib/model.js
CHANGED
|
@@ -263,7 +263,7 @@ Model.prototype._setupIndexes = async function(fields, opts={}) {
|
|
|
263
263
|
let textIndex = { name: 'text', key: {} }
|
|
264
264
|
|
|
265
265
|
// No db defined
|
|
266
|
-
if (!model.manager
|
|
266
|
+
if (!((model.manager||{})._state||{}).match(/^open/)) {
|
|
267
267
|
throw new Error(`Skipping createIndex on the '${model.name||''}' model, no mongodb connection found.`)
|
|
268
268
|
}
|
|
269
269
|
|
package/lib/util.js
CHANGED
|
@@ -328,8 +328,9 @@ module.exports = {
|
|
|
328
328
|
console.error(`Monastery ${info} error: you cannot return a promise AND call next()`)
|
|
329
329
|
return
|
|
330
330
|
}
|
|
331
|
+
current++
|
|
331
332
|
results.push(result)
|
|
332
|
-
if (!err &&
|
|
333
|
+
if (!err && current < tasks.length) callTask(current)
|
|
333
334
|
else done(err)
|
|
334
335
|
}
|
|
335
336
|
function done(err) {
|
|
@@ -345,7 +346,7 @@ module.exports = {
|
|
|
345
346
|
const next2 = next.bind(null, i)
|
|
346
347
|
const res = tasks[i](next2)
|
|
347
348
|
if (res instanceof Promise) {
|
|
348
|
-
res.then((result) => next2(null, result)).catch(next2)
|
|
349
|
+
res.then((result) => next2(null, result)).catch((e) => next2(e))
|
|
349
350
|
}
|
|
350
351
|
}
|
|
351
352
|
|
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.8",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": "github:boycce/monastery",
|
|
8
8
|
"homepage": "https://boycce.github.io/monastery/",
|
package/test/crud.js
CHANGED
|
@@ -902,6 +902,7 @@ test('hooks > async and next conflict', async () => {
|
|
|
902
902
|
fields: { age: { type: 'number'} },
|
|
903
903
|
afterFind: [
|
|
904
904
|
async (data, next) => {
|
|
905
|
+
data.age = data.age + 1
|
|
905
906
|
next() // should console an error for empty promise
|
|
906
907
|
},
|
|
907
908
|
async (data, next) => {
|
|
@@ -909,7 +910,7 @@ test('hooks > async and next conflict', async () => {
|
|
|
909
910
|
},
|
|
910
911
|
],
|
|
911
912
|
})
|
|
912
|
-
let user4 = db.model('
|
|
913
|
+
let user4 = db.model('user4', {
|
|
913
914
|
fields: { age: { type: 'number'} },
|
|
914
915
|
afterFind: [
|
|
915
916
|
async (data, next) => {
|
|
@@ -926,10 +927,27 @@ test('hooks > async and next conflict', async () => {
|
|
|
926
927
|
},
|
|
927
928
|
],
|
|
928
929
|
})
|
|
930
|
+
|
|
931
|
+
let user5 = db.model('user5', {
|
|
932
|
+
fields: { age: { type: 'number'} },
|
|
933
|
+
afterFind: [
|
|
934
|
+
async (data, next) => {
|
|
935
|
+
const promise = Promise.reject(new Error('An async error occurred with Martin3'))
|
|
936
|
+
next(new Error('An async error occurred with Martin3'))
|
|
937
|
+
return promise
|
|
938
|
+
},
|
|
939
|
+
async (data, next) => {
|
|
940
|
+
data.age = data.age + 1 // shouldn't be reached
|
|
941
|
+
},
|
|
942
|
+
],
|
|
943
|
+
})
|
|
944
|
+
|
|
945
|
+
|
|
929
946
|
let user1Doc = await user1.insert({ data: { age: 0 } })
|
|
930
947
|
let user2Doc = await user2.insert({ data: { age: 0 } })
|
|
931
948
|
let user3Doc = await user3.insert({ data: { age: 0 } })
|
|
932
949
|
let user4Doc = await user4.insert({ data: { age: 0 } })
|
|
950
|
+
let user5Doc = await user5.insert({ data: { age: 0 } })
|
|
933
951
|
|
|
934
952
|
const logSpy = jest.spyOn(console, 'error').mockImplementation(() => {})
|
|
935
953
|
|
|
@@ -946,4 +964,7 @@ test('hooks > async and next conflict', async () => {
|
|
|
946
964
|
await expect(user4.find({ query: user4Doc._id })).resolves.toEqual({ _id: expect.any(Object), age: 2 })
|
|
947
965
|
expect(logSpy).toHaveBeenCalledWith('Monastery afterFind error: you cannot return a promise AND call next()')
|
|
948
966
|
|
|
967
|
+
await expect(user5.find({ query: user5Doc._id })).rejects.toThrow('An async error occurred with Martin3')
|
|
968
|
+
expect(logSpy).toHaveBeenCalledWith('Monastery afterFind error: you cannot return a promise AND call next()')
|
|
969
|
+
|
|
949
970
|
})
|