monastery 2.2.3 → 3.0.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/.eslintrc.json +10 -1
- package/changelog.md +3 -1
- package/docs/_config.yml +2 -2
- package/docs/assets/imgs/monastery.jpg +0 -0
- package/docs/definition/index.md +1 -2
- package/docs/manager/index.md +19 -11
- package/docs/manager/model.md +1 -1
- package/docs/manager/models.md +2 -3
- package/docs/model/count.md +25 -0
- package/docs/model/find.md +5 -9
- package/docs/model/findOne.md +1 -1
- package/docs/model/findOneAndUpdate.md +1 -1
- package/docs/model/index.md +5 -30
- package/docs/model/insert.md +4 -6
- package/docs/model/rawMethods.md +290 -0
- package/docs/model/remove.md +4 -6
- package/docs/model/update.md +4 -6
- package/docs/readme.md +69 -48
- package/lib/collection.js +324 -0
- package/lib/index.js +207 -67
- package/lib/model-crud.js +605 -619
- package/lib/model-validate.js +227 -245
- package/lib/model.js +70 -91
- package/lib/rules.js +36 -35
- package/lib/util.js +69 -15
- package/package.json +12 -11
- package/plugins/images/index.js +11 -11
- package/test/blacklisting.js +506 -537
- package/test/collection.js +445 -0
- package/test/crud.js +810 -730
- package/test/index.test.js +26 -0
- package/test/manager.js +77 -0
- package/test/mock/blacklisting.js +23 -23
- package/test/model.js +611 -572
- package/test/plugin-images.js +880 -965
- package/test/populate.js +249 -262
- package/test/util.js +126 -45
- package/test/validate.js +1074 -1121
- package/test/virtuals.js +222 -227
- package/lib/monk-monkey-patches.js +0 -90
- package/test/monk.js +0 -53
- package/test/test.js +0 -38
package/.eslintrc.json
CHANGED
|
@@ -8,9 +8,11 @@
|
|
|
8
8
|
"eslint:recommended"
|
|
9
9
|
],
|
|
10
10
|
"globals": {
|
|
11
|
+
// jest globals
|
|
11
12
|
"test": true,
|
|
12
13
|
"expect": true,
|
|
13
|
-
"
|
|
14
|
+
"afterAll": true,
|
|
15
|
+
"beforeAll": true
|
|
14
16
|
},
|
|
15
17
|
"parserOptions": {
|
|
16
18
|
"ecmaFeatures": {
|
|
@@ -22,6 +24,13 @@
|
|
|
22
24
|
"plugins": [],
|
|
23
25
|
"rules": {
|
|
24
26
|
"brace-style": ["error", "1tbs", { "allowSingleLine": true }],
|
|
27
|
+
"comma-dangle": ["error", {
|
|
28
|
+
"arrays": "always-multiline",
|
|
29
|
+
"objects": "always-multiline",
|
|
30
|
+
"imports": "always-multiline",
|
|
31
|
+
"exports": "always-multiline",
|
|
32
|
+
"functions": "never"
|
|
33
|
+
}],
|
|
25
34
|
"max-len": ["error", { "code": 125, "ignorePattern": "^\\s*<(rect|path|line)\\s" }],
|
|
26
35
|
"no-prototype-builtins": "off",
|
|
27
36
|
"no-unused-vars": ["error", { "args": "none" }],
|
package/changelog.md
CHANGED
|
@@ -2,7 +2,9 @@
|
|
|
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
|
-
### [
|
|
5
|
+
### [3.0.1](https://github.com/boycce/monastery/compare/3.0.0...3.0.1) (2024-04-28)
|
|
6
|
+
|
|
7
|
+
## [3.0.0](https://github.com/boycce/monastery/compare/2.2.2...3.0.0) (2024-04-28)
|
|
6
8
|
|
|
7
9
|
### [2.2.2](https://github.com/boycce/monastery/compare/2.2.1...2.2.2) (2024-04-07)
|
|
8
10
|
|
package/docs/_config.yml
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
remote_theme: boycce/github-docs
|
|
2
2
|
title: Monastery
|
|
3
|
-
description: A
|
|
3
|
+
description: A simple, straightforward MongoDB ODM
|
|
4
4
|
github_url: "https://github.com/boycce/monastery"
|
|
5
5
|
port: 4001
|
|
6
6
|
basedir: "docs"
|
|
7
7
|
|
|
8
|
-
# Aux links for the
|
|
8
|
+
# Aux links for the navigation.
|
|
9
9
|
aux_links:
|
|
10
10
|
"Monastery on GitHub":
|
|
11
11
|
- "//github.com/boycce/monastery"
|
|
Binary file
|
package/docs/definition/index.md
CHANGED
|
@@ -161,8 +161,7 @@ fieldType: {
|
|
|
161
161
|
index: 'text'
|
|
162
162
|
|
|
163
163
|
// You can also pass an object if you need to use mongodb's index options
|
|
164
|
-
// https://
|
|
165
|
-
// https://mongodb.github.io/node-mongodb-native/2.1/api/Collection.html#createIndexes
|
|
164
|
+
// https://mongodb.github.io/node-mongodb-native/5.9/classes/Collection.html#createIndex
|
|
166
165
|
index: { type: 1, ...(any mongodb index option) },
|
|
167
166
|
|
|
168
167
|
}
|
package/docs/manager/index.md
CHANGED
|
@@ -6,26 +6,25 @@ has_children: true
|
|
|
6
6
|
|
|
7
7
|
# Manager
|
|
8
8
|
|
|
9
|
-
Monastery
|
|
9
|
+
Monastery manager constructor.
|
|
10
10
|
|
|
11
11
|
### Arguments
|
|
12
12
|
|
|
13
|
-
`uri` *(string\|array)*: A [mongo connection string URI](https://
|
|
13
|
+
`uri` *(string\|array)*: A [mongo connection string URI](https://www.mongodb.com/docs/v5.0/reference/connection-string/). Replica sets can be an array or comma separated.
|
|
14
14
|
|
|
15
15
|
[`options`] *(object)*:
|
|
16
16
|
- [`hideWarnings=false`] *(boolean)*: hide monastery warnings
|
|
17
17
|
- [`hideErrors=false`] *(boolean)*: hide monastery errors
|
|
18
18
|
- [`defaultObjects=false`] *(boolean)*: when [inserting](../model/insert.html#defaults-example), undefined embedded documents and arrays are defined
|
|
19
19
|
- [`nullObjects=false`] *(boolean)*: embedded documents and arrays can be set to null or an empty string (which gets converted to null). You can override this per field via `nullObject: true`.
|
|
20
|
+
- [`promise=false`] *(boolean)*: return a promise instead of the manager instance
|
|
20
21
|
- [`timestamps=true`] *(boolean)*: whether to use [`createdAt` and `updatedAt`](../definition), this can be overridden per operation
|
|
21
22
|
- [`useMilliseconds=false`] *(boolean)*: by default the `createdAt` and `updatedAt` fields that get created automatically use unix timestamps in seconds, set this to true to use milliseconds instead.
|
|
22
|
-
- [`mongo options`](
|
|
23
|
-
|
|
24
|
-
[`callback`] *(function)*: You may optionally specify a callback which will be called once the connection to the mongo database is opened or throws an error.
|
|
23
|
+
- [`mongo options`](https://mongodb.github.io/node-mongodb-native/5.9/interfaces/MongoClientOptions.html)...
|
|
25
24
|
|
|
26
25
|
### Returns
|
|
27
26
|
|
|
28
|
-
A
|
|
27
|
+
A manager instance.
|
|
29
28
|
|
|
30
29
|
### Example
|
|
31
30
|
|
|
@@ -40,16 +39,25 @@ const db = monastery('localhost/mydb,192.168.1.1') // replica set
|
|
|
40
39
|
```
|
|
41
40
|
|
|
42
41
|
```js
|
|
42
|
+
// You can wait for the connection (which is not required before calling methods)
|
|
43
43
|
import monastery from 'monastery'
|
|
44
|
-
monastery('localhost/mydb,192.168.1.1'
|
|
45
|
-
// db is the connected instance of the Manager
|
|
46
|
-
}).catch((err) => {
|
|
47
|
-
// error connecting to the database
|
|
48
|
-
})
|
|
44
|
+
const db = await monastery('localhost/mydb,192.168.1.1', { promise: true })
|
|
49
45
|
```
|
|
50
46
|
|
|
47
|
+
```js
|
|
48
|
+
// You can listen for connection errors using our `catch` hook
|
|
49
|
+
import monastery from 'monastery'
|
|
50
|
+
const db = monastery('localhost/mydb,192.168.1.1').catch(err => {})
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### Properties
|
|
54
|
+
|
|
55
|
+
- `manager.db`: Raw Mongo db instance
|
|
56
|
+
- `manager.client`: Raw Mongo client instance
|
|
57
|
+
|
|
51
58
|
### Methods
|
|
52
59
|
|
|
60
|
+
- `manager.catch(Function)`: Catches connection errors
|
|
53
61
|
- `manager.id(<String|ObjectId>)`: Create or convert a valid MongoDB ObjectId string into an ObjectId
|
|
54
62
|
- `manager.isId(String|ObjectId)`: Checks if the passed variable is a valid MongoDB ObjectId or ObjectId string
|
|
55
63
|
- `manager.model()`: [see model](./model.html)
|
package/docs/manager/model.md
CHANGED
package/docs/manager/models.md
CHANGED
|
@@ -13,10 +13,10 @@ Setup model definitions from a folder location
|
|
|
13
13
|
|
|
14
14
|
### Returns
|
|
15
15
|
|
|
16
|
-
A promise with an array of [model](../model) instances, the model instances will also be
|
|
16
|
+
A promise with an array of [model](../model) instances, the model instances will also be available at:
|
|
17
17
|
```js
|
|
18
18
|
db.{model-name}
|
|
19
|
-
db.
|
|
19
|
+
db.models.{model-name}
|
|
20
20
|
```
|
|
21
21
|
|
|
22
22
|
### Example
|
|
@@ -36,5 +36,4 @@ export default { // Make sure the model definition is exported as the default
|
|
|
36
36
|
|
|
37
37
|
```js
|
|
38
38
|
await db.models(__dirname + "models")
|
|
39
|
-
db.user.insert()
|
|
40
39
|
```
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: count
|
|
3
|
+
parent: Model
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# `model.count`
|
|
7
|
+
|
|
8
|
+
Count document(s) in a collection
|
|
9
|
+
|
|
10
|
+
### Arguments
|
|
11
|
+
|
|
12
|
+
`options` *(object)*
|
|
13
|
+
|
|
14
|
+
- `query` *(object\|id)*: [`MongoDB query document`](https://www.mongodb.com/docs/v5.0/tutorial/query-documents/), or id
|
|
15
|
+
- [[`any mongodb option`](https://mongodb.github.io/node-mongodb-native/5.9/classes/Collection.html#count)] *(any)*
|
|
16
|
+
|
|
17
|
+
### Returns
|
|
18
|
+
|
|
19
|
+
A promise
|
|
20
|
+
|
|
21
|
+
### Example
|
|
22
|
+
|
|
23
|
+
```js
|
|
24
|
+
await user.count({ query: { name: "Martin Luther" }}) // 4
|
|
25
|
+
```
|
package/docs/model/find.md
CHANGED
|
@@ -5,25 +5,23 @@ parent: Model
|
|
|
5
5
|
|
|
6
6
|
# `model.find`
|
|
7
7
|
|
|
8
|
-
Find document(s) in a collection and call the model hook: `afterFind`
|
|
8
|
+
Find document(s) in a collection, and call the model hook: `afterFind`
|
|
9
9
|
|
|
10
10
|
### Arguments
|
|
11
11
|
|
|
12
12
|
`options` *(object)*
|
|
13
13
|
|
|
14
|
-
- `query` *(object\|id)*: [`MongoDB query document`](https://www.mongodb.com/docs/
|
|
14
|
+
- `query` *(object\|id)*: [`MongoDB query document`](https://www.mongodb.com/docs/v5.0/tutorial/query-documents/), or id
|
|
15
15
|
- [[`blacklist`](#blacklisting)] *(array\|string\|false)*: augment `definition.findBL`. `false` will remove all blacklisting
|
|
16
16
|
- [`getSignedUrls`] *(boolean)*: get signed urls for all image objects
|
|
17
17
|
- [[`populate`](#populate)] *(array)*
|
|
18
18
|
- [`project`] *(string\|array\|object)*: return only these fields, ignores blacklisting
|
|
19
19
|
- [`sort`] *(string\|array\|object)*: same as the mongodb option, but allows string parsing e.g. 'name', 'name:1'
|
|
20
|
-
- [[`any mongodb option`](
|
|
21
|
-
|
|
22
|
-
[`callback`] *(function)*: pass instead of return a promise
|
|
20
|
+
- [[`any mongodb option`](https://mongodb.github.io/node-mongodb-native/5.9/classes/Collection.html#find)] *(any)*
|
|
23
21
|
|
|
24
22
|
### Returns
|
|
25
23
|
|
|
26
|
-
A promise
|
|
24
|
+
A promise
|
|
27
25
|
|
|
28
26
|
### Example
|
|
29
27
|
|
|
@@ -81,9 +79,7 @@ user.find({ query: {...}, populate: ['myBooks.book'] })
|
|
|
81
79
|
|
|
82
80
|
### Custom Populate Query
|
|
83
81
|
|
|
84
|
-
If you would like more control you can either use
|
|
85
|
-
[aggregate](https://automattic.github.io/monk/docs/collection/aggregate.html) function via
|
|
86
|
-
`user._aggregate`, or simply pass a MongoDB lookup object to populate. When passing a lookup object, the
|
|
82
|
+
If you would like more control you can either use Mongo's `aggregate` method via [`model._aggregate`](./...rawMethods), or simply pass a MongoDB lookup object to populate. When passing a lookup object, the
|
|
87
83
|
populated field still needs to be defined in `definition.fields` if you want to call any related hooks,
|
|
88
84
|
and prune any blacklisted fields. See the examples below,
|
|
89
85
|
|
package/docs/model/findOne.md
CHANGED
package/docs/model/index.md
CHANGED
|
@@ -8,35 +8,10 @@ has_children: true
|
|
|
8
8
|
|
|
9
9
|
Created via [`manager.model`](../manager/model).
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
### Properties
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
You can access the following properties from each model:
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
* model.[_distinct](https://automattic.github.io/monk/docs/collection/distinct.html)
|
|
19
|
-
* model.[_drop](https://automattic.github.io/monk/docs/collection/drop.html)
|
|
20
|
-
* model.[_dropIndex](https://automattic.github.io/monk/docs/collection/dropIndex.html)
|
|
21
|
-
* model.[_dropIndexes](https://automattic.github.io/monk/docs/collection/dropIndexes.html)
|
|
22
|
-
* model.[_ensureIndex](https://automattic.github.io/monk/docs/collection/ensureIndex.html)
|
|
23
|
-
* model.[_find](https://automattic.github.io/monk/docs/collection/find.html)
|
|
24
|
-
* model.[_findOne](https://automattic.github.io/monk/docs/collection/findOne.html)
|
|
25
|
-
* model.[_findOneAndDelete](https://automattic.github.io/monk/docs/collection/findOneAndDelete.html)
|
|
26
|
-
* model.[_findOneAndUpdate](https://automattic.github.io/monk/docs/collection/findOneAndUpdate.html)
|
|
27
|
-
* model.[_geoHaystackSearch](https://automattic.github.io/monk/docs/collection/geoHaystackSearch.html)
|
|
28
|
-
* model.[_geoNear](https://automattic.github.io/monk/docs/collection/geoNear.html)
|
|
29
|
-
* model.[_group](https://automattic.github.io/monk/docs/collection/group.html)
|
|
30
|
-
* model.[_indexes](https://automattic.github.io/monk/docs/collection/indexes.html)
|
|
31
|
-
* model.[_insert](https://automattic.github.io/monk/docs/collection/insert.html)
|
|
32
|
-
* model.[_mapReduce](https://automattic.github.io/monk/docs/collection/mapReduce.html)
|
|
33
|
-
* model.[_remove](https://automattic.github.io/monk/docs/collection/remove.html)
|
|
34
|
-
* model.[_stats](https://automattic.github.io/monk/docs/collection/stats.html)
|
|
35
|
-
* model.[_update](https://automattic.github.io/monk/docs/collection/update.html)
|
|
36
|
-
|
|
37
|
-
#### Monk collection
|
|
38
|
-
|
|
39
|
-
If you wish to access the raw monk collection, you can do so via:
|
|
40
|
-
```js
|
|
41
|
-
model._collection
|
|
42
|
-
```
|
|
15
|
+
- `model.manager`: Monastery manager instance
|
|
16
|
+
- `model.collection`: Monastery collection instance
|
|
17
|
+
- `model.collection.col`: Raw Mongo collection
|
package/docs/model/insert.md
CHANGED
|
@@ -16,19 +16,17 @@ Validate and insert document(s) in a collection and calls model hooks: `beforeIn
|
|
|
16
16
|
- [`project`] *(string\|array\|object)*: project these fields, ignores blacklisting
|
|
17
17
|
- [`skipValidation`] (string\|array\|boolean): skip validation for these field name(s), or `true` for all fields
|
|
18
18
|
- [`timestamps`] *(boolean)*: whether `createdAt` and `updatedAt` are automatically inserted, defaults to `manager.timestamps`
|
|
19
|
-
- [[`any mongodb option`](
|
|
20
|
-
|
|
21
|
-
[`callback`] *(function)*: pass instead of return a promise
|
|
19
|
+
- [[`any mongodb option`](https://mongodb.github.io/node-mongodb-native/5.9/classes/Collection.html#insertMany)] *(any)*
|
|
22
20
|
|
|
23
21
|
### Returns
|
|
24
22
|
|
|
25
|
-
A promise
|
|
23
|
+
A promise
|
|
26
24
|
|
|
27
25
|
### Example
|
|
28
26
|
|
|
29
27
|
```js
|
|
30
|
-
user.insert({ data: { name: 'Martin Luther' }})
|
|
31
|
-
user.insert({ data: [{ name: 'Martin Luther' }, { name: 'Bruce Lee' }]})
|
|
28
|
+
await user.insert({ data: { name: 'Martin Luther' }})
|
|
29
|
+
await user.insert({ data: [{ name: 'Martin Luther' }, { name: 'Bruce Lee' }]})
|
|
32
30
|
```
|
|
33
31
|
|
|
34
32
|
### Blacklisting
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: ...collection methods
|
|
3
|
+
parent: Model
|
|
4
|
+
nav_order: 9
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
# Collection Methods
|
|
8
|
+
|
|
9
|
+
These MongoDB collection methods are made available if you need to test or run any method without hooks or Monastery argument/return processing.
|
|
10
|
+
|
|
11
|
+
These methods can be accessed via `model._*`.
|
|
12
|
+
|
|
13
|
+
## Returns
|
|
14
|
+
|
|
15
|
+
All methods return a promise.
|
|
16
|
+
|
|
17
|
+
## Methods
|
|
18
|
+
|
|
19
|
+
* model.[_aggregate](#model_aggregate)
|
|
20
|
+
* model.[_bulkWrite](#model_bulkwrite)
|
|
21
|
+
* model.[_count](#model_count)
|
|
22
|
+
* model.[_createIndex](#model_createindex)
|
|
23
|
+
* model.[_createIndexes](#model_createindexes)
|
|
24
|
+
* model.[_distinct](#model_distinct)
|
|
25
|
+
* model.[_drop](#model_drop)
|
|
26
|
+
* model.[_dropIndex](#model_dropindex)
|
|
27
|
+
* model.[_dropIndexes](#model_dropindexes)
|
|
28
|
+
* model.[_find](#model_find)
|
|
29
|
+
* model.[_findOne](#model_findone)
|
|
30
|
+
* model.[_findOneAndDelete](#model_findoneanddelete)
|
|
31
|
+
* model.[_findOneAndUpdate](#model_findoneandupdate)
|
|
32
|
+
* model.[_indexes](#model_indexes)
|
|
33
|
+
* model.[_indexInformation](#model_indexinformation)
|
|
34
|
+
* model.[_insert](#model_insert)
|
|
35
|
+
* model.[_remove](#model_remove)
|
|
36
|
+
* model.[_stats](#model_stats)
|
|
37
|
+
* model.[_update](#model_update)
|
|
38
|
+
|
|
39
|
+
### [`model._aggregate`](https://mongodb.github.io/node-mongodb-native/5.9/classes/Collection.html#aggregate)
|
|
40
|
+
|
|
41
|
+
Execute an aggregation framework pipeline against the collection. Arguments:
|
|
42
|
+
|
|
43
|
+
1. `Pipeline` *(array)*
|
|
44
|
+
2. `[Options]` *(object)*
|
|
45
|
+
|
|
46
|
+
```js
|
|
47
|
+
await users.aggregate([
|
|
48
|
+
{ $project : { author : 1, tags : 1 }},
|
|
49
|
+
{ $unwind : "$tags" },
|
|
50
|
+
{ $group : { _id : { tags : "$tags" }}}
|
|
51
|
+
])
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### [`model._bulkWrite`](https://mongodb.github.io/node-mongodb-native/5.9/classes/Collection.html#bulkWrite)
|
|
55
|
+
|
|
56
|
+
Perform a bulkWrite operation without a fluent API. Arguments:
|
|
57
|
+
|
|
58
|
+
1. `Operations` *(array)* - Bulk operations to perform
|
|
59
|
+
2. `[Options]` *(object)*
|
|
60
|
+
|
|
61
|
+
Legal operation types are:
|
|
62
|
+
|
|
63
|
+
```js
|
|
64
|
+
await users._bulkWrite([
|
|
65
|
+
{ insertOne: { document: { a: 1 } } },
|
|
66
|
+
{ updateOne: { filter: {a:2}, update: {$set: {a:2}}, upsert:true } },
|
|
67
|
+
{ updateMany: { filter: {a:2}, update: {$set: {a:2}}, upsert:true } },
|
|
68
|
+
{ deleteOne: { filter: {c:1} } },
|
|
69
|
+
{ deleteMany: { filter: {c:1} } },
|
|
70
|
+
{ replaceOne: { filter: {c:3}, replacement: {c:4}, upsert:true}}
|
|
71
|
+
])
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### [`model._count`](https://mongodb.github.io/node-mongodb-native/5.9/classes/Collection.html#count)
|
|
75
|
+
|
|
76
|
+
Returns the count of documents that would match a find() query. The method uses collection.countDocuments() in the mongo driver. Arguments:
|
|
77
|
+
|
|
78
|
+
1. `[Filter]` *(string\|objectId\|object)*
|
|
79
|
+
2. `[Options]` *(object)*
|
|
80
|
+
|
|
81
|
+
```js
|
|
82
|
+
await users._count({ name: 'foo' })
|
|
83
|
+
await users._count('id') // a bit useless but consistent with the rest of the API
|
|
84
|
+
await users._count()
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
If you need to get a fast order of magnitude of the count of all documents in your collection, you can use the estimate option.
|
|
88
|
+
|
|
89
|
+
```js
|
|
90
|
+
await users._count({}, { estimate: true }) // Filter is ignored
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### [`model._createIndex`](https://mongodb.github.io/node-mongodb-native/5.9/classes/Collection.html#createIndex)
|
|
94
|
+
|
|
95
|
+
Creates an index on the db and collection collection. Arguments:
|
|
96
|
+
|
|
97
|
+
1. `IndexSpec` *(string\|array\|object)* - The field name or index specification to create an index for
|
|
98
|
+
2. `[Options]` *(object)*
|
|
99
|
+
|
|
100
|
+
```js
|
|
101
|
+
await users.createIndex({ a: 1, b: -1 });
|
|
102
|
+
// Alternate syntax for { c: 1, d: -1 } that ensures order of indexes
|
|
103
|
+
await users.createIndex([ [c, 1], [d, -1] ]);
|
|
104
|
+
// Equivalent to { e: 1 }
|
|
105
|
+
await users.createIndex('e');
|
|
106
|
+
// Equivalent to { f: 1, g: 1 }
|
|
107
|
+
await users.createIndex(['f', 'g'])
|
|
108
|
+
// Equivalent to { h: 1, i: -1 }
|
|
109
|
+
await users.createIndex([ { h: 1 }, { i: -1 } ]);
|
|
110
|
+
// Equivalent to { j: 1, k: -1, l: 2d }
|
|
111
|
+
await users.createIndex(['j', ['k', -1], { l: '2d' }])
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### [`model._createIndexes`](https://mongodb.github.io/node-mongodb-native/5.9/classes/Collection.html#createIndexes)
|
|
115
|
+
|
|
116
|
+
Creates multiple indexes in the collection. Arguments:
|
|
117
|
+
|
|
118
|
+
1. `IndexSpec` *(array<IndexSpec>)*- The field names or index specifications to create an indexes for
|
|
119
|
+
2. `[Options]` *(object)*
|
|
120
|
+
|
|
121
|
+
```js
|
|
122
|
+
await users.createIndexes([
|
|
123
|
+
// Simple index on field fizz
|
|
124
|
+
{ key: { fizz: 1 } }
|
|
125
|
+
// wildcard index
|
|
126
|
+
{ key: { '$**': 1 } },
|
|
127
|
+
// named index on darmok and jalad
|
|
128
|
+
{ key: { darmok: 1, jalad: -1 } name: 'tanagra' }
|
|
129
|
+
])
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### [`model._distinct`](https://mongodb.github.io/node-mongodb-native/5.9/classes/Collection.html#distinct)
|
|
133
|
+
|
|
134
|
+
Returns a list of distinct values for the given key across a collection. Arguments:
|
|
135
|
+
|
|
136
|
+
1. `Key` *(string)*
|
|
137
|
+
2. `[Filter]` *(string\|objectId\|object)*
|
|
138
|
+
3. `[Options]` *(object)*
|
|
139
|
+
|
|
140
|
+
```js
|
|
141
|
+
await users._distinct('name')
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### [`model._drop`](https://mongodb.github.io/node-mongodb-native/5.9/classes/Collection.html#drop)
|
|
145
|
+
|
|
146
|
+
Drops the entire collection. Arguments: `None`
|
|
147
|
+
|
|
148
|
+
```js
|
|
149
|
+
await users._drop()
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### [`model._dropIndex`](https://mongodb.github.io/node-mongodb-native/5.9/classes/Collection.html#dropIndex)
|
|
153
|
+
|
|
154
|
+
Drops an index on the collection. Arguments:
|
|
155
|
+
|
|
156
|
+
1. `IndexName` *(string)*
|
|
157
|
+
2. `[Options]` *(object)*
|
|
158
|
+
|
|
159
|
+
```js
|
|
160
|
+
await users._dropIndex('name')
|
|
161
|
+
await users._dropIndex('name.last')
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### [`model._dropIndexes`](https://mongodb.github.io/node-mongodb-native/5.9/classes/Collection.html#dropIndexes)
|
|
165
|
+
|
|
166
|
+
Drops all indexes from the collection. Arguments: `None`
|
|
167
|
+
|
|
168
|
+
```js
|
|
169
|
+
await users._dropIndexes()
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
### [`model._find`](https://mongodb.github.io/node-mongodb-native/5.9/classes/Collection.html#find)
|
|
173
|
+
|
|
174
|
+
Finds documents in the collection. Arguments:
|
|
175
|
+
|
|
176
|
+
1. `[Filter]` *(string\|objectId\|object)*
|
|
177
|
+
2. `[Options]` *(object)*
|
|
178
|
+
|
|
179
|
+
```js
|
|
180
|
+
await users._find()
|
|
181
|
+
await users._find({ name: 'John' })
|
|
182
|
+
await users._find({}, { projection: { name: 1 } }) // only the name field will be selected
|
|
183
|
+
await users._find({}, { rawCursor: true }) // returns raw mongo cursor
|
|
184
|
+
await users._find({}, {
|
|
185
|
+
stream: ((doc, { close, pause, resume }) => {
|
|
186
|
+
// the users are streaming here
|
|
187
|
+
// call `close()` to stop the stream
|
|
188
|
+
})
|
|
189
|
+
})
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
### [`model._findOne`](https://mongodb.github.io/node-mongodb-native/5.9/classes/Collection.html#findOne)
|
|
193
|
+
|
|
194
|
+
Fetches the first document that matches the filter. Arguments:
|
|
195
|
+
|
|
196
|
+
1. `[Filter]` *(string\|objectId\|object)*
|
|
197
|
+
2. `[Options]` *(object)*
|
|
198
|
+
|
|
199
|
+
```js
|
|
200
|
+
await users._findOne({ name: 'John' })
|
|
201
|
+
await users._findOne({}, { projection: { name: 1 } }) // only the name field will be selected
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
### [`model._findOneAndDelete`](https://mongodb.github.io/node-mongodb-native/5.9/classes/Collection.html#findOneAndDelete)
|
|
205
|
+
|
|
206
|
+
Find a document and delete it in one atomic operation. Requires a write lock for the duration of the operation. Arguments:
|
|
207
|
+
|
|
208
|
+
1. `[Filter]` *(string\|objectId\|object)*
|
|
209
|
+
2. `[Options]` *(object)*
|
|
210
|
+
|
|
211
|
+
```js
|
|
212
|
+
await users._findOneAndDelete({ name: 'John' })
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
### [`model._findOneAndUpdate`](https://mongodb.github.io/node-mongodb-native/5.9/classes/Collection.html#findOneAndUpdate)
|
|
216
|
+
|
|
217
|
+
Find a document and update it in one atomic operation. Requires a write lock for the duration of the operation. Arguments:
|
|
218
|
+
|
|
219
|
+
1. `Filter` *(string\|objectId\|object)*
|
|
220
|
+
2. `Update` *(object)*
|
|
221
|
+
3. `[Options]` *(object)*
|
|
222
|
+
|
|
223
|
+
```js
|
|
224
|
+
await users._findOneAndUpdate({ name: 'John' }, { $set: { age: 30 } })
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
### [`model._indexInformation`](https://mongodb.github.io/node-mongodb-native/5.9/classes/Collection.html#indexInformation)
|
|
228
|
+
|
|
229
|
+
Retrieves this collections index info. Argument(s):
|
|
230
|
+
|
|
231
|
+
1. `[Options]` *(object)*
|
|
232
|
+
|
|
233
|
+
```js
|
|
234
|
+
await users._indexInformation()
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
### [`model._indexes`](https://mongodb.github.io/node-mongodb-native/5.9/classes/Collection.html#indexes)
|
|
238
|
+
|
|
239
|
+
Lists all indexes on the collection. Argument(s):
|
|
240
|
+
|
|
241
|
+
1. `[Options]` *(object)*
|
|
242
|
+
|
|
243
|
+
```js
|
|
244
|
+
await users._indexes()
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
### [`model._insert`](https://mongodb.github.io/node-mongodb-native/5.9/classes/Collection.html#insertMany)
|
|
248
|
+
|
|
249
|
+
Inserts one or more documents into the collection. Arguments:
|
|
250
|
+
|
|
251
|
+
1. `Doc` *(object\|array)* - one or many documents
|
|
252
|
+
2. `[Options]` *(object)*
|
|
253
|
+
|
|
254
|
+
```js
|
|
255
|
+
await users._insert({ name: 'John', age: 30 })
|
|
256
|
+
await users._insert([{ name: 'John', age: 30 }, { name: 'Bill', age: 32 }])
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
### [`model._remove`](https://mongodb.github.io/node-mongodb-native/5.9/classes/Collection.html#deleteMany)
|
|
260
|
+
|
|
261
|
+
Removes one or more document(s) from the collection. Arguments:
|
|
262
|
+
|
|
263
|
+
1. `Filter` *(string\|objectId\|object)*
|
|
264
|
+
2. `[Options]` *(object)*
|
|
265
|
+
|
|
266
|
+
```js
|
|
267
|
+
await users._remove({ name: 'John' })
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
### [`model._stats`](https://mongodb.github.io/node-mongodb-native/5.9/classes/Collection.html#stats)
|
|
271
|
+
|
|
272
|
+
Returns statistics about the collection. Arguments:
|
|
273
|
+
|
|
274
|
+
1. `[Options]` *(object)*
|
|
275
|
+
|
|
276
|
+
```js
|
|
277
|
+
await users._stats()
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
### [`model._update`](https://mongodb.github.io/node-mongodb-native/5.9/classes/Collection.html#updateMany)
|
|
281
|
+
|
|
282
|
+
Updates one or more document(s) from the collection. Arguments:
|
|
283
|
+
|
|
284
|
+
1. `Filter` *(string\|objectId\|object)*
|
|
285
|
+
1. `Update` *(object)*
|
|
286
|
+
2. `[Options]` *(object)*
|
|
287
|
+
|
|
288
|
+
```js
|
|
289
|
+
await users._update({ name: 'John' }, { $set: { age: 30 } })
|
|
290
|
+
```
|
package/docs/model/remove.md
CHANGED
|
@@ -11,19 +11,17 @@ Remove document(s) in a collection and calls model hooks: `beforeRemove`, `afte
|
|
|
11
11
|
|
|
12
12
|
`options` *(object)*
|
|
13
13
|
|
|
14
|
-
- `query` *(object\|id)*: [`MongoDB query document`](https://www.mongodb.com/docs/
|
|
14
|
+
- `query` *(object\|id)*: [`MongoDB query document`](https://www.mongodb.com/docs/v5.0/tutorial/query-documents/), or id
|
|
15
15
|
- [`sort`] *(string\|object\|array)*: same as the mongodb option, but allows for string parsing e.g. 'name', 'name:1'
|
|
16
16
|
- [`multi`] *(boolean)*: set to false remove only the first document that match the query criteria
|
|
17
|
-
- [[`any mongodb option`](
|
|
18
|
-
|
|
19
|
-
[`callback`] *(function)*: pass instead of return a promise
|
|
17
|
+
- [[`any mongodb option`](https://mongodb.github.io/node-mongodb-native/5.9/classes/Collection.html#deleteMany)] *(any)*
|
|
20
18
|
|
|
21
19
|
### Returns
|
|
22
20
|
|
|
23
|
-
A promise
|
|
21
|
+
A promise
|
|
24
22
|
|
|
25
23
|
### Example
|
|
26
24
|
|
|
27
25
|
```js
|
|
28
|
-
user.remove({ query: { name: "Martin Luther" }})
|
|
26
|
+
await user.remove({ query: { name: "Martin Luther" }})
|
|
29
27
|
```
|
package/docs/model/update.md
CHANGED
|
@@ -11,25 +11,23 @@ Update document(s) in a collection and calls model hooks: `beforeUpdate`, `afte
|
|
|
11
11
|
|
|
12
12
|
`options` *(object)*
|
|
13
13
|
|
|
14
|
-
- `query` *(object\|id)*: [`MongoDB query document`](https://www.mongodb.com/docs/
|
|
14
|
+
- `query` *(object\|id)*: [`MongoDB query document`](https://www.mongodb.com/docs/v5.0/tutorial/query-documents/), or id
|
|
15
15
|
- [`data`](#data) *(object)* - data that's validated against the model fields (always wrapped in `{ $set: .. }`)
|
|
16
16
|
- [[`blacklist`](#blacklisting)]*(array\|string\|false)*: augment `definition.updateBL`. `false` will remove all blacklisting
|
|
17
17
|
- [`project`] *(string\|array\|object)*: project these fields, ignores blacklisting
|
|
18
18
|
- [`skipValidation`] (string\|array\|boolean): skip validation for these field name(s), or `true` for all fields
|
|
19
19
|
- [`sort`] *(string\|object\|array)*: same as the mongodb option, but allows for string parsing e.g. 'name', 'name:1'
|
|
20
20
|
- [`timestamps`] *(boolean)*: whether `updatedAt` is automatically updated, defaults to the `manager.timestamps` value
|
|
21
|
-
- [[`any mongodb option`](
|
|
22
|
-
|
|
23
|
-
[`callback`] *(function)*: pass instead of return a promise
|
|
21
|
+
- [[`any mongodb option`](https://mongodb.github.io/node-mongodb-native/5.9/classes/Collection.html#updateMany)] *(any)*
|
|
24
22
|
|
|
25
23
|
### Returns
|
|
26
24
|
|
|
27
|
-
A promise
|
|
25
|
+
A promise
|
|
28
26
|
|
|
29
27
|
### Example
|
|
30
28
|
|
|
31
29
|
```js
|
|
32
|
-
user.update({ query: { name: 'foo' }, data: { name: 'bar' }})
|
|
30
|
+
await user.update({ query: { name: 'foo' }, data: { name: 'bar' }})
|
|
33
31
|
```
|
|
34
32
|
|
|
35
33
|
### Data
|