assai 2.0.0 → 2.1.0
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/README.md +148 -148
- package/dist/src/factories/create_mongo_collection.d.mts +55 -11
- package/dist/src/usecases/mongo/operation/aggregate.d.mts +16 -0
- package/dist/src/usecases/mongo/operation/bulk_write.d.mts +15 -0
- package/dist/src/usecases/mongo/operation/find_one_and_delete.d.mts +16 -0
- package/dist/src/usecases/mongo/operation/find_one_and_replace.d.mts +18 -0
- package/dist/src/usecases/mongo/operation/find_one_and_update.d.mts +18 -0
- package/dist/src/usecases/mongo/operation/index.d.mts +5 -0
- package/index.mjs +2 -2
- package/jsconfig.json +17 -20
- package/jsconfig.prod.json +12 -12
- package/package.json +40 -39
- package/src/__test/manage_mock_database.mjs +24 -24
- package/src/__test/manage_mock_registry.mjs +54 -54
- package/src/__test/mock_get_collection.mjs +20 -20
- package/src/data/errors/operation_fail.mjs +9 -9
- package/src/data/errors/unsupported_error.mjs +9 -9
- package/src/data/interfaces/mongo_operator.mjs +2 -2
- package/src/data/interfaces/native_collection.mjs +27 -27
- package/src/factories/create_mock_collection.mjs +173 -173
- package/src/factories/create_mongo_collection.mjs +224 -181
- package/src/factories/index.mjs +1 -1
- package/src/types.ts +31 -31
- package/src/usecases/index.mjs +1 -1
- package/src/usecases/mongo/generate_new_id.mjs +4 -4
- package/src/usecases/mongo/index.mjs +3 -3
- package/src/usecases/mongo/mongo_client.mjs +36 -36
- package/src/usecases/mongo/operation/additional/delete_one_or_throw.mjs +17 -17
- package/src/usecases/mongo/operation/additional/index.mjs +1 -1
- package/src/usecases/mongo/operation/aggregate.mjs +26 -0
- package/src/usecases/mongo/operation/bulk_write.mjs +54 -0
- package/src/usecases/mongo/operation/count.mjs +16 -16
- package/src/usecases/mongo/operation/delete_many.mjs +15 -15
- package/src/usecases/mongo/operation/delete_one.mjs +16 -16
- package/src/usecases/mongo/operation/find.mjs +34 -34
- package/src/usecases/mongo/operation/find_one.mjs +32 -32
- package/src/usecases/mongo/operation/find_one_and_delete.mjs +23 -0
- package/src/usecases/mongo/operation/find_one_and_replace.mjs +24 -0
- package/src/usecases/mongo/operation/find_one_and_update.mjs +40 -0
- package/src/usecases/mongo/operation/index.mjs +15 -10
- package/src/usecases/mongo/operation/insert_many.mjs +39 -39
- package/src/usecases/mongo/operation/insert_one.mjs +32 -32
- package/src/usecases/mongo/operation/update_many.mjs +39 -39
- package/src/usecases/mongo/operation/update_one.mjs +40 -40
- package/src/usecases/mongo/transformers/id/index.mjs +4 -4
- package/src/usecases/mongo/transformers/id/rename_find_options.mjs +12 -12
- package/src/usecases/mongo/transformers/id/rename_to_dev_id.mjs +13 -13
- package/src/usecases/mongo/transformers/id/rename_to_mongo_id.mjs +13 -13
- package/src/usecases/mongo/transformers/index.mjs +2 -2
- package/src/usecases/mongo/transformers/input_transformer.mjs +32 -32
- package/src/usecases/mongo/transformers/object_id/ids_into_strings.mjs +29 -29
- package/src/usecases/mongo/transformers/object_id/index.mjs +3 -3
- package/src/usecases/mongo/transformers/object_id/strings_into_id.mjs +44 -44
- package/src/usecases/mongo/transformers/output_transformer.mjs +18 -18
- package/src/usecases/mongo/transformers/timestamps.mjs +27 -22
package/README.md
CHANGED
|
@@ -1,148 +1,148 @@
|
|
|
1
|
-
This is a small package to improve some things that you, as a developer, have to deal with when working with mongo, like:
|
|
2
|
-
|
|
3
|
-
- [\_id](##_id)
|
|
4
|
-
- [ObjectId](##ObjectId)
|
|
5
|
-
- [projection](##projection)
|
|
6
|
-
- [Connection String](##connection-string)
|
|
7
|
-
- [Client Instance](##client-instance)
|
|
8
|
-
|
|
9
|
-
# Example
|
|
10
|
-
|
|
11
|
-
```js
|
|
12
|
-
import { createMongoCollection } from 'assai'
|
|
13
|
-
|
|
14
|
-
/** @type {import('assai').ICollection<user>} */
|
|
15
|
-
const collection = await createMongoCollection()
|
|
16
|
-
const docs = await collection.find({}, { limit: 10 })
|
|
17
|
-
/**
|
|
18
|
-
* [{id: "507f1f77bcf86cd799439011", name: "Mario"}, ...]
|
|
19
|
-
*/
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
## \_id
|
|
23
|
-
|
|
24
|
-
Ever wanted to use just "id" in your collections instead of "\_id"?
|
|
25
|
-
|
|
26
|
-
This package does just that!
|
|
27
|
-
|
|
28
|
-
Every data that enters the database can, optionally, have an "id". In this case, before sending the data to the native mongodb driver, the object will rename this property to "\_id", which mongodb understands. This operation will be applied to `insertOne` and `insertMany` methods.
|
|
29
|
-
|
|
30
|
-
Also, the methods `updateOne`, `updateMany`, `deleteOne`, `deleteMany`, `findOne` and `find` will also rename the field "id" to "\_id".
|
|
31
|
-
|
|
32
|
-
## ObjectId
|
|
33
|
-
|
|
34
|
-
Another thing that is related to "\_id" fields are the `ObjectId`s.
|
|
35
|
-
|
|
36
|
-
The issue is that your application can before unnecessarily verbose. To fix that, the package will automatically convert all objectId strings into a ObjectId under the hood and all objectIds that will come from your collection, will be converted to strings.
|
|
37
|
-
|
|
38
|
-
```js
|
|
39
|
-
await collection.insertOne({
|
|
40
|
-
name: 'Matteo',
|
|
41
|
-
groupId: '507f1f77bcf86cd799439011', // This will be stored as an ObjectId
|
|
42
|
-
})
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
Every time you need a new id, you can call the `generateNewId` method:
|
|
46
|
-
|
|
47
|
-
```js
|
|
48
|
-
import { mongo } from 'assai'
|
|
49
|
-
|
|
50
|
-
const myStringId = mongo.generateNewId()
|
|
51
|
-
```
|
|
52
|
-
|
|
53
|
-
One example this could be useful is if have an API endpoint that accepts structured data. And you use these values to query the database. Like so:
|
|
54
|
-
|
|
55
|
-
```js
|
|
56
|
-
// Client code
|
|
57
|
-
const response = await axios.post('/posts', {
|
|
58
|
-
userId: '507f1f77bcf86cd799439011',
|
|
59
|
-
})
|
|
60
|
-
```
|
|
61
|
-
|
|
62
|
-
This is fine, but you will have to convert a `string` into a `ObjectId` for each field value that is an objectId in your collection. Even though this is easy to do, you could forget somewhere.
|
|
63
|
-
|
|
64
|
-
Instead of carrying this risk, you can use the object as-is and the conversion will be made automatically.
|
|
65
|
-
|
|
66
|
-
## projection
|
|
67
|
-
|
|
68
|
-
The projection from the native mongodb driver is fine as it is. But there is one thing that is annoying: it can cause your program to fail.
|
|
69
|
-
|
|
70
|
-
To be honest, this behavior makes some sense because this usually comes from a mistake the developer made. But this is not always the case and it goes against how mongodb behave: they avoid throwing an exception when possible.
|
|
71
|
-
|
|
72
|
-
For that reason, you won't see this error while using this package:
|
|
73
|
-
|
|
74
|
-
```
|
|
75
|
-
Cannot do exclusion on field '...' in inclusion projection
|
|
76
|
-
```
|
|
77
|
-
|
|
78
|
-
Making projections like that valid:
|
|
79
|
-
|
|
80
|
-
```json
|
|
81
|
-
{
|
|
82
|
-
"name": true,
|
|
83
|
-
"createdAt": false
|
|
84
|
-
}
|
|
85
|
-
```
|
|
86
|
-
|
|
87
|
-
## Connection String
|
|
88
|
-
|
|
89
|
-
A default environment variable is assumed: DATABASE_URL.
|
|
90
|
-
|
|
91
|
-
Which makes it easier to start a connection:
|
|
92
|
-
|
|
93
|
-
```js
|
|
94
|
-
const database = await createMongoCollection('myCollection')
|
|
95
|
-
```
|
|
96
|
-
|
|
97
|
-
This will read the value from `process.env.DATABASE_URL`.
|
|
98
|
-
|
|
99
|
-
You can still pass a custom connection string:
|
|
100
|
-
|
|
101
|
-
```js
|
|
102
|
-
const database = await createMongoCollection('myCollection', {
|
|
103
|
-
connectionString: 'my connection string',
|
|
104
|
-
})
|
|
105
|
-
```
|
|
106
|
-
|
|
107
|
-
## Client Instance
|
|
108
|
-
|
|
109
|
-
If you ever worked with serverless, you will notice that you shouldn't open and close a connection everytime your function runs. You need to cache it. The package does this caching for you by default.
|
|
110
|
-
|
|
111
|
-
You could also do this for simplicity, so instead of:
|
|
112
|
-
|
|
113
|
-
```js
|
|
114
|
-
// db.js
|
|
115
|
-
let cachedClient = null
|
|
116
|
-
export async function getDb () {
|
|
117
|
-
if (cachedClient == null) {
|
|
118
|
-
const client = await MongoClient.connect('...')
|
|
119
|
-
cachedClient = client
|
|
120
|
-
}
|
|
121
|
-
return cachedClient.db()
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
// router.js
|
|
125
|
-
import { getDb } from 'db.js'
|
|
126
|
-
|
|
127
|
-
router.post('/', (req, res) => {
|
|
128
|
-
const db = await getDb()
|
|
129
|
-
const col = db.collection('myCollection')
|
|
130
|
-
await col.insertOne({
|
|
131
|
-
name: req.body.name,
|
|
132
|
-
})
|
|
133
|
-
})
|
|
134
|
-
```
|
|
135
|
-
|
|
136
|
-
You can simply write:
|
|
137
|
-
|
|
138
|
-
```js
|
|
139
|
-
router.post('/', (req, res) => {
|
|
140
|
-
const col = createMongoCollection('myCollection')
|
|
141
|
-
// Here the connection is opened only if it is not opened already.
|
|
142
|
-
// Further calls to this route won't open a connection.
|
|
143
|
-
await col.insertOne({
|
|
144
|
-
name: req.body.name,
|
|
145
|
-
})
|
|
146
|
-
// ...
|
|
147
|
-
})
|
|
148
|
-
```
|
|
1
|
+
This is a small package to improve some things that you, as a developer, have to deal with when working with mongo, like:
|
|
2
|
+
|
|
3
|
+
- [\_id](##_id)
|
|
4
|
+
- [ObjectId](##ObjectId)
|
|
5
|
+
- [projection](##projection)
|
|
6
|
+
- [Connection String](##connection-string)
|
|
7
|
+
- [Client Instance](##client-instance)
|
|
8
|
+
|
|
9
|
+
# Example
|
|
10
|
+
|
|
11
|
+
```js
|
|
12
|
+
import { createMongoCollection } from 'assai'
|
|
13
|
+
|
|
14
|
+
/** @type {import('assai').ICollection<user>} */
|
|
15
|
+
const collection = await createMongoCollection()
|
|
16
|
+
const docs = await collection.find({}, { limit: 10 })
|
|
17
|
+
/**
|
|
18
|
+
* [{id: "507f1f77bcf86cd799439011", name: "Mario"}, ...]
|
|
19
|
+
*/
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## \_id
|
|
23
|
+
|
|
24
|
+
Ever wanted to use just "id" in your collections instead of "\_id"?
|
|
25
|
+
|
|
26
|
+
This package does just that!
|
|
27
|
+
|
|
28
|
+
Every data that enters the database can, optionally, have an "id". In this case, before sending the data to the native mongodb driver, the object will rename this property to "\_id", which mongodb understands. This operation will be applied to `insertOne` and `insertMany` methods.
|
|
29
|
+
|
|
30
|
+
Also, the methods `updateOne`, `updateMany`, `deleteOne`, `deleteMany`, `findOne` and `find` will also rename the field "id" to "\_id".
|
|
31
|
+
|
|
32
|
+
## ObjectId
|
|
33
|
+
|
|
34
|
+
Another thing that is related to "\_id" fields are the `ObjectId`s.
|
|
35
|
+
|
|
36
|
+
The issue is that your application can before unnecessarily verbose. To fix that, the package will automatically convert all objectId strings into a ObjectId under the hood and all objectIds that will come from your collection, will be converted to strings.
|
|
37
|
+
|
|
38
|
+
```js
|
|
39
|
+
await collection.insertOne({
|
|
40
|
+
name: 'Matteo',
|
|
41
|
+
groupId: '507f1f77bcf86cd799439011', // This will be stored as an ObjectId
|
|
42
|
+
})
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Every time you need a new id, you can call the `generateNewId` method:
|
|
46
|
+
|
|
47
|
+
```js
|
|
48
|
+
import { mongo } from 'assai'
|
|
49
|
+
|
|
50
|
+
const myStringId = mongo.generateNewId()
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
One example this could be useful is if have an API endpoint that accepts structured data. And you use these values to query the database. Like so:
|
|
54
|
+
|
|
55
|
+
```js
|
|
56
|
+
// Client code
|
|
57
|
+
const response = await axios.post('/posts', {
|
|
58
|
+
userId: '507f1f77bcf86cd799439011',
|
|
59
|
+
})
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
This is fine, but you will have to convert a `string` into a `ObjectId` for each field value that is an objectId in your collection. Even though this is easy to do, you could forget somewhere.
|
|
63
|
+
|
|
64
|
+
Instead of carrying this risk, you can use the object as-is and the conversion will be made automatically.
|
|
65
|
+
|
|
66
|
+
## projection
|
|
67
|
+
|
|
68
|
+
The projection from the native mongodb driver is fine as it is. But there is one thing that is annoying: it can cause your program to fail.
|
|
69
|
+
|
|
70
|
+
To be honest, this behavior makes some sense because this usually comes from a mistake the developer made. But this is not always the case and it goes against how mongodb behave: they avoid throwing an exception when possible.
|
|
71
|
+
|
|
72
|
+
For that reason, you won't see this error while using this package:
|
|
73
|
+
|
|
74
|
+
```
|
|
75
|
+
Cannot do exclusion on field '...' in inclusion projection
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Making projections like that valid:
|
|
79
|
+
|
|
80
|
+
```json
|
|
81
|
+
{
|
|
82
|
+
"name": true,
|
|
83
|
+
"createdAt": false
|
|
84
|
+
}
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## Connection String
|
|
88
|
+
|
|
89
|
+
A default environment variable is assumed: DATABASE_URL.
|
|
90
|
+
|
|
91
|
+
Which makes it easier to start a connection:
|
|
92
|
+
|
|
93
|
+
```js
|
|
94
|
+
const database = await createMongoCollection('myCollection')
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
This will read the value from `process.env.DATABASE_URL`.
|
|
98
|
+
|
|
99
|
+
You can still pass a custom connection string:
|
|
100
|
+
|
|
101
|
+
```js
|
|
102
|
+
const database = await createMongoCollection('myCollection', {
|
|
103
|
+
connectionString: 'my connection string',
|
|
104
|
+
})
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## Client Instance
|
|
108
|
+
|
|
109
|
+
If you ever worked with serverless, you will notice that you shouldn't open and close a connection everytime your function runs. You need to cache it. The package does this caching for you by default.
|
|
110
|
+
|
|
111
|
+
You could also do this for simplicity, so instead of:
|
|
112
|
+
|
|
113
|
+
```js
|
|
114
|
+
// db.js
|
|
115
|
+
let cachedClient = null
|
|
116
|
+
export async function getDb () {
|
|
117
|
+
if (cachedClient == null) {
|
|
118
|
+
const client = await MongoClient.connect('...')
|
|
119
|
+
cachedClient = client
|
|
120
|
+
}
|
|
121
|
+
return cachedClient.db()
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// router.js
|
|
125
|
+
import { getDb } from 'db.js'
|
|
126
|
+
|
|
127
|
+
router.post('/', (req, res) => {
|
|
128
|
+
const db = await getDb()
|
|
129
|
+
const col = db.collection('myCollection')
|
|
130
|
+
await col.insertOne({
|
|
131
|
+
name: req.body.name,
|
|
132
|
+
})
|
|
133
|
+
})
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
You can simply write:
|
|
137
|
+
|
|
138
|
+
```js
|
|
139
|
+
router.post('/', (req, res) => {
|
|
140
|
+
const col = createMongoCollection('myCollection')
|
|
141
|
+
// Here the connection is opened only if it is not opened already.
|
|
142
|
+
// Further calls to this route won't open a connection.
|
|
143
|
+
await col.insertOne({
|
|
144
|
+
name: req.body.name,
|
|
145
|
+
})
|
|
146
|
+
// ...
|
|
147
|
+
})
|
|
148
|
+
```
|
|
@@ -4,8 +4,7 @@
|
|
|
4
4
|
* This method will read the string DATABASE_URL to create a connection. If you have it in another
|
|
5
5
|
* location, you will need to pass it at `connectionString` property inside the options parameter.
|
|
6
6
|
*
|
|
7
|
-
* The connection is cached by default. Use `collectionGetter`
|
|
8
|
-
* customize this behavior.
|
|
7
|
+
* The connection is cached by default. Use `collectionGetter` to customize this behavior.
|
|
9
8
|
* @template {import('../types.js').MongoDocument} T
|
|
10
9
|
* @param {string} name
|
|
11
10
|
* @param {IcreateCollectionOptions<T>} [options]
|
|
@@ -73,6 +72,44 @@ export function createMongoCollection<T extends import("../types.js").MongoDocum
|
|
|
73
72
|
* @param {import('mongodb').UpdateOptions} [options]
|
|
74
73
|
*/
|
|
75
74
|
updateMany: (query: import("mongodb").Filter<T>, update: import("mongodb").UpdateFilter<T>, options?: import("mongodb").UpdateOptions | undefined) => Promise<import("mongodb").UpdateResult<T>>;
|
|
75
|
+
/**
|
|
76
|
+
* @param {import('mongodb').Document[]} pipeline
|
|
77
|
+
* @param {import('mongodb').AggregateOptions} [options]
|
|
78
|
+
* @returns {Promise<T[]>}
|
|
79
|
+
*/
|
|
80
|
+
aggregate: (pipeline: import('mongodb').Document[], options?: import("mongodb").AggregateOptions | undefined) => Promise<T[]>;
|
|
81
|
+
/**
|
|
82
|
+
* @param {import('mongodb').AnyBulkWriteOperation<T>[]} operations
|
|
83
|
+
* @param {import('mongodb').BulkWriteOptions} [options]
|
|
84
|
+
*/
|
|
85
|
+
bulkWrite: (operations: import("mongodb").AnyBulkWriteOperation<T>[], options?: import("mongodb").BulkWriteOptions | undefined) => Promise<import("mongodb").BulkWriteResult>;
|
|
86
|
+
/**
|
|
87
|
+
* Atomically finds a document, applies the update, and returns the document.
|
|
88
|
+
* By default returns the document as it was **before** the update.
|
|
89
|
+
* Pass `{ returnDocument: 'after' }` in options to get the updated version.
|
|
90
|
+
* @param {import('mongodb').Filter<T>} query
|
|
91
|
+
* @param {import('mongodb').UpdateFilter<T>} update
|
|
92
|
+
* @param {import('mongodb').FindOneAndUpdateOptions} [options]
|
|
93
|
+
* @returns {Promise<T | null>}
|
|
94
|
+
*/
|
|
95
|
+
findOneAndUpdate: (query: import("mongodb").Filter<T>, update: import("mongodb").UpdateFilter<T>, options?: import("mongodb").FindOneAndUpdateOptions | undefined) => Promise<T | null>;
|
|
96
|
+
/**
|
|
97
|
+
* Atomically finds a document, deletes it, and returns the deleted document.
|
|
98
|
+
* @param {import('mongodb').Filter<T>} query
|
|
99
|
+
* @param {import('mongodb').FindOneAndDeleteOptions} [options]
|
|
100
|
+
* @returns {Promise<T | null>}
|
|
101
|
+
*/
|
|
102
|
+
findOneAndDelete: (query: import("mongodb").Filter<T>, options?: import("mongodb").FindOneAndDeleteOptions | undefined) => Promise<T | null>;
|
|
103
|
+
/**
|
|
104
|
+
* Atomically finds a document, replaces it with `replacement`, and returns a document.
|
|
105
|
+
* By default returns the document as it was **before** the replacement.
|
|
106
|
+
* Pass `{ returnDocument: 'after' }` in options to get the new version.
|
|
107
|
+
* @param {import('mongodb').Filter<T>} query
|
|
108
|
+
* @param {import('mongodb').WithoutId<T>} replacement
|
|
109
|
+
* @param {import('mongodb').FindOneAndReplaceOptions} [options]
|
|
110
|
+
* @returns {Promise<T | null>}
|
|
111
|
+
*/
|
|
112
|
+
findOneAndReplace: (query: import("mongodb").Filter<T>, replacement: import("mongodb").WithoutId<T>, options?: import("mongodb").FindOneAndReplaceOptions | undefined) => Promise<T | null>;
|
|
76
113
|
};
|
|
77
114
|
export type IcollectionGetter<T extends import("../types.js").MongoDocument> = () => Promise<Collection<T>>;
|
|
78
115
|
export type IcreateCollectionOptions<T extends import("../types.js").MongoDocument> = {
|
|
@@ -89,15 +126,6 @@ export type IcreateCollectionOptions<T extends import("../types.js").MongoDocume
|
|
|
89
126
|
* ```
|
|
90
127
|
*/
|
|
91
128
|
collectionGetter?: IcollectionGetter<T> | undefined;
|
|
92
|
-
/**
|
|
93
|
-
* Same as `collectionGetter`. But the object is cached.
|
|
94
|
-
*
|
|
95
|
-
* Example:
|
|
96
|
-
* ```js
|
|
97
|
-
* cachableCollectionGetter: async () => await getMyCollection()
|
|
98
|
-
* ```
|
|
99
|
-
*/
|
|
100
|
-
cachableCollectionGetter?: IcollectionGetter<T> | undefined;
|
|
101
129
|
/**
|
|
102
130
|
* A custom connection string. If none is given, `process.env.DATABASE_URL` will be used.
|
|
103
131
|
*/
|
|
@@ -110,10 +138,26 @@ export type IcreateCollectionOptions<T extends import("../types.js").MongoDocume
|
|
|
110
138
|
* The options used when initializing the client.
|
|
111
139
|
*/
|
|
112
140
|
options?: import("mongodb").DbOptions | undefined;
|
|
141
|
+
/**
|
|
142
|
+
* Configured how the package manages 'createdAt'
|
|
143
|
+
* and 'updatedAt'.
|
|
144
|
+
*/
|
|
113
145
|
timestamps?: ItimestampConfiguration | undefined;
|
|
114
146
|
};
|
|
115
147
|
export type ItimestampConfiguration = {
|
|
148
|
+
/**
|
|
149
|
+
* Changes how the 'createdAt' field is managed:
|
|
150
|
+
* - 'fromId': the field is generated when you use `find` or `findOne` and is calculated using _id.
|
|
151
|
+
* But it does not exist in the database.
|
|
152
|
+
* - 'generate': the field is generated when you use `insertOne` or `insertMany`. The value exist in
|
|
153
|
+
* the database.
|
|
154
|
+
* - 'none': no value is generated at any time.
|
|
155
|
+
*/
|
|
116
156
|
createdAt: 'fromId' | 'generate' | 'none';
|
|
157
|
+
/**
|
|
158
|
+
* - 'generate': the value is generated when you use `insertOne` or `insertMany` and is updated on
|
|
159
|
+
* `updateOne` and `updateMany`.
|
|
160
|
+
*/
|
|
117
161
|
updatedAt: 'generate' | 'none';
|
|
118
162
|
};
|
|
119
163
|
export type ICollection<T extends import("../types.js").MongoDocument> = Awaited<ReturnType<typeof createMongoCollection<T>>>;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @template {import('../../../types.js').MongoDocument} T
|
|
3
|
+
* @param {object} parameter
|
|
4
|
+
* @param {() => Promise<Collection<T>>} parameter.getCollection
|
|
5
|
+
* @param {import('mongodb').Document[]} parameter.pipeline
|
|
6
|
+
* @param {import('mongodb').AggregateOptions} [parameter.options]
|
|
7
|
+
* @param {import('../../../factories/create_mongo_collection.mjs').IcreateCollectionOptions<T>} [parameter.collectionOptions]
|
|
8
|
+
* @returns {Promise<T[]>}
|
|
9
|
+
*/
|
|
10
|
+
export function aggregate<T extends import("../../../types.js").MongoDocument>({ getCollection, pipeline, options, collectionOptions }: {
|
|
11
|
+
getCollection: () => Promise<Collection<T>>;
|
|
12
|
+
pipeline: import('mongodb').Document[];
|
|
13
|
+
options?: import("mongodb").AggregateOptions | undefined;
|
|
14
|
+
collectionOptions?: import("../../../factories/create_mongo_collection.mjs").IcreateCollectionOptions<T> | undefined;
|
|
15
|
+
}): Promise<T[]>;
|
|
16
|
+
import { Collection } from 'mongodb';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @template {import('../../../types.js').MongoDocument} T
|
|
3
|
+
* @param {object} parameter
|
|
4
|
+
* @param {() => Promise<Collection<T>>} parameter.getCollection
|
|
5
|
+
* @param {import('mongodb').AnyBulkWriteOperation<T>[]} parameter.operations
|
|
6
|
+
* @param {import('mongodb').BulkWriteOptions} [parameter.options]
|
|
7
|
+
* @param {import('../../../factories/create_mongo_collection.mjs').IcreateCollectionOptions<T>} [parameter.collectionOptions]
|
|
8
|
+
*/
|
|
9
|
+
export function bulkWrite<T extends import("../../../types.js").MongoDocument>({ getCollection, operations, options, collectionOptions }: {
|
|
10
|
+
getCollection: () => Promise<Collection<T>>;
|
|
11
|
+
operations: import("mongodb").AnyBulkWriteOperation<T>[];
|
|
12
|
+
options?: import("mongodb").BulkWriteOptions | undefined;
|
|
13
|
+
collectionOptions?: import("../../../factories/create_mongo_collection.mjs").IcreateCollectionOptions<T> | undefined;
|
|
14
|
+
}): Promise<import("mongodb").BulkWriteResult>;
|
|
15
|
+
import { Collection } from 'mongodb';
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @template {import('../../../types.js').MongoDocument} T
|
|
3
|
+
* @param {object} param
|
|
4
|
+
* @param {import('mongodb').Filter<T>} param.query
|
|
5
|
+
* @param {import('mongodb').FindOneAndDeleteOptions} [param.options]
|
|
6
|
+
* @param {() => Promise<Collection<T>>} param.getCollection
|
|
7
|
+
* @param {import('../../../factories/create_mongo_collection.mjs').IcreateCollectionOptions<T>} [param.collectionOptions]
|
|
8
|
+
* @returns {Promise<T | null>}
|
|
9
|
+
*/
|
|
10
|
+
export function findOneAndDelete<T extends import("../../../types.js").MongoDocument>({ query, options, collectionOptions, getCollection }: {
|
|
11
|
+
query: import("mongodb").Filter<T>;
|
|
12
|
+
options?: import("mongodb").FindOneAndDeleteOptions | undefined;
|
|
13
|
+
getCollection: () => Promise<Collection<T>>;
|
|
14
|
+
collectionOptions?: import("../../../factories/create_mongo_collection.mjs").IcreateCollectionOptions<T> | undefined;
|
|
15
|
+
}): Promise<T | null>;
|
|
16
|
+
import { Collection } from 'mongodb';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @template {import('../../../types.js').MongoDocument} T
|
|
3
|
+
* @param {object} param
|
|
4
|
+
* @param {import('mongodb').Filter<T>} param.query
|
|
5
|
+
* @param {import('mongodb').WithoutId<T>} param.replacement
|
|
6
|
+
* @param {import('mongodb').FindOneAndReplaceOptions} [param.options]
|
|
7
|
+
* @param {() => Promise<Collection<T>>} param.getCollection
|
|
8
|
+
* @param {import('../../../factories/create_mongo_collection.mjs').IcreateCollectionOptions<T>} [param.collectionOptions]
|
|
9
|
+
* @returns {Promise<T | null>}
|
|
10
|
+
*/
|
|
11
|
+
export function findOneAndReplace<T extends import("../../../types.js").MongoDocument>({ query, replacement, options, collectionOptions, getCollection }: {
|
|
12
|
+
query: import("mongodb").Filter<T>;
|
|
13
|
+
replacement: import("mongodb").WithoutId<T>;
|
|
14
|
+
options?: import("mongodb").FindOneAndReplaceOptions | undefined;
|
|
15
|
+
getCollection: () => Promise<Collection<T>>;
|
|
16
|
+
collectionOptions?: import("../../../factories/create_mongo_collection.mjs").IcreateCollectionOptions<T> | undefined;
|
|
17
|
+
}): Promise<T | null>;
|
|
18
|
+
import { Collection } from 'mongodb';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @template {import('../../../types.js').MongoDocument} T
|
|
3
|
+
* @param {object} param
|
|
4
|
+
* @param {import('mongodb').Filter<T>} param.query
|
|
5
|
+
* @param {import('mongodb').UpdateFilter<T>} param.update
|
|
6
|
+
* @param {import('mongodb').FindOneAndUpdateOptions} [param.options]
|
|
7
|
+
* @param {() => Promise<Collection<T>>} param.getCollection
|
|
8
|
+
* @param {import('../../../factories/create_mongo_collection.mjs').IcreateCollectionOptions<T>} [param.collectionOptions]
|
|
9
|
+
* @returns {Promise<T | null>}
|
|
10
|
+
*/
|
|
11
|
+
export function findOneAndUpdate<T extends import("../../../types.js").MongoDocument>({ query, update, options, collectionOptions, getCollection }: {
|
|
12
|
+
query: import("mongodb").Filter<T>;
|
|
13
|
+
update: import("mongodb").UpdateFilter<T>;
|
|
14
|
+
options?: import("mongodb").FindOneAndUpdateOptions | undefined;
|
|
15
|
+
getCollection: () => Promise<Collection<T>>;
|
|
16
|
+
collectionOptions?: import("../../../factories/create_mongo_collection.mjs").IcreateCollectionOptions<T> | undefined;
|
|
17
|
+
}): Promise<T | null>;
|
|
18
|
+
import { Collection } from 'mongodb';
|
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
export * from "./additional/index.mjs";
|
|
2
|
+
export * from "./aggregate.mjs";
|
|
3
|
+
export * from "./bulk_write.mjs";
|
|
2
4
|
export * from "./count.mjs";
|
|
3
5
|
export * from "./delete_many.mjs";
|
|
4
6
|
export * from "./delete_one.mjs";
|
|
5
7
|
export * from "./find.mjs";
|
|
6
8
|
export * from "./find_one.mjs";
|
|
9
|
+
export * from "./find_one_and_delete.mjs";
|
|
10
|
+
export * from "./find_one_and_replace.mjs";
|
|
11
|
+
export * from "./find_one_and_update.mjs";
|
|
7
12
|
export * from "./insert_many.mjs";
|
|
8
13
|
export * from "./insert_one.mjs";
|
|
9
14
|
export * from "./update_many.mjs";
|
package/index.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from './src/factories/index.mjs'
|
|
2
|
-
export * from './src/usecases/index.mjs'
|
|
1
|
+
export * from './src/factories/index.mjs'
|
|
2
|
+
export * from './src/usecases/index.mjs'
|
package/jsconfig.json
CHANGED
|
@@ -1,20 +1,17 @@
|
|
|
1
|
-
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
"outDir": "dist"
|
|
19
|
-
},
|
|
20
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"allowJs": true,
|
|
4
|
+
"checkJs": true,
|
|
5
|
+
"strictNullChecks": true,
|
|
6
|
+
"module": "nodenext",
|
|
7
|
+
"target": "ES2022",
|
|
8
|
+
"moduleResolution": "nodenext",
|
|
9
|
+
"sourceMap": true,
|
|
10
|
+
"declaration": true,
|
|
11
|
+
"strictPropertyInitialization": true,
|
|
12
|
+
"lib": ["ES2022"],
|
|
13
|
+
"noEmit": false,
|
|
14
|
+
"emitDeclarationOnly": true,
|
|
15
|
+
"outDir": "dist"
|
|
16
|
+
}
|
|
17
|
+
}
|
package/jsconfig.prod.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "./jsconfig.json",
|
|
3
|
-
"include": [
|
|
4
|
-
"./src/**/*.mjs",
|
|
5
|
-
"./index.mjs"
|
|
6
|
-
],
|
|
7
|
-
"exclude": [
|
|
8
|
-
"dist",
|
|
9
|
-
"test/**/*",
|
|
10
|
-
"src/**/*.test.mjs",
|
|
11
|
-
"node_modules"
|
|
12
|
-
]
|
|
1
|
+
{
|
|
2
|
+
"extends": "./jsconfig.json",
|
|
3
|
+
"include": [
|
|
4
|
+
"./src/**/*.mjs",
|
|
5
|
+
"./index.mjs"
|
|
6
|
+
],
|
|
7
|
+
"exclude": [
|
|
8
|
+
"dist",
|
|
9
|
+
"test/**/*",
|
|
10
|
+
"src/**/*.test.mjs",
|
|
11
|
+
"node_modules"
|
|
12
|
+
]
|
|
13
13
|
}
|
package/package.json
CHANGED
|
@@ -1,39 +1,40 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "assai",
|
|
3
|
-
"version": "2.
|
|
4
|
-
"repository": {
|
|
5
|
-
"url": "https://github.com/TimeLord2010/assai",
|
|
6
|
-
"type": "git"
|
|
7
|
-
},
|
|
8
|
-
"description": "A simple mongodb wrapper to make mongo even easier to work with.",
|
|
9
|
-
"main": "index.mjs",
|
|
10
|
-
"types": "dist/index.d.mts",
|
|
11
|
-
"scripts": {
|
|
12
|
-
"prepare": "rm -rf dist && tsc -p ./jsconfig.prod.json",
|
|
13
|
-
"start": "node --env-file=.env index.mjs",
|
|
14
|
-
"test": "node --test"
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
"
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
"
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
"jsconfig.
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
"@
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
|
|
39
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "assai",
|
|
3
|
+
"version": "2.1.0",
|
|
4
|
+
"repository": {
|
|
5
|
+
"url": "https://github.com/TimeLord2010/assai",
|
|
6
|
+
"type": "git"
|
|
7
|
+
},
|
|
8
|
+
"description": "A simple mongodb wrapper to make mongo even easier to work with.",
|
|
9
|
+
"main": "index.mjs",
|
|
10
|
+
"types": "dist/index.d.mts",
|
|
11
|
+
"scripts": {
|
|
12
|
+
"prepare": "rm -rf dist && tsc -p ./jsconfig.prod.json",
|
|
13
|
+
"start": "node --env-file=.env index.mjs",
|
|
14
|
+
"test": "node --test",
|
|
15
|
+
"check": "tsc -p jsconfig.json"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"mongodb",
|
|
19
|
+
"orm"
|
|
20
|
+
],
|
|
21
|
+
"files": [
|
|
22
|
+
"index.mjs",
|
|
23
|
+
"src/",
|
|
24
|
+
"dist",
|
|
25
|
+
"!src/**/*.test.mjs",
|
|
26
|
+
"jsconfig.json",
|
|
27
|
+
"jsconfig.prod.json"
|
|
28
|
+
],
|
|
29
|
+
"author": "Vinícius Gabriel",
|
|
30
|
+
"license": "ISC",
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"mongodb": "^7.0.0"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@faker-js/faker": "^8.4.1",
|
|
36
|
+
"@types/node": "^20.12.7",
|
|
37
|
+
"mongodb-memory-server": "^10.0.0",
|
|
38
|
+
"typescript": "^5.4.5"
|
|
39
|
+
}
|
|
40
|
+
}
|