assai 1.1.1 → 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 +23 -22
- package/dist/src/factories/create_mongo_collection.d.mts +60 -18
- package/dist/src/types.d.ts +1 -1
- 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.d.mts +3 -1
- package/dist/src/usecases/mongo/operation/find_one.d.mts +4 -1
- 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/dist/src/usecases/mongo/operation/insert_many.d.mts +3 -1
- package/dist/src/usecases/mongo/operation/insert_one.d.mts +3 -1
- package/dist/src/usecases/mongo/operation/update_many.d.mts +3 -1
- package/dist/src/usecases/mongo/operation/update_one.d.mts +4 -1
- package/dist/src/usecases/mongo/transformers/id/rename_find_options.d.mts +3 -4
- package/dist/src/usecases/mongo/transformers/input_transformer.d.mts +10 -0
- package/dist/src/usecases/mongo/transformers/output_transformer.d.mts +10 -0
- package/dist/src/usecases/mongo/transformers/timestamps.d.mts +6 -0
- package/jsconfig.json +16 -19
- package/package.json +5 -4
- package/src/factories/create_mongo_collection.mjs +75 -68
- package/src/types.ts +1 -15
- package/src/usecases/mongo/operation/aggregate.mjs +26 -0
- package/src/usecases/mongo/operation/bulk_write.mjs +54 -0
- package/src/usecases/mongo/operation/find.mjs +7 -6
- package/src/usecases/mongo/operation/find_one.mjs +9 -2
- 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 +5 -0
- package/src/usecases/mongo/operation/insert_many.mjs +7 -4
- package/src/usecases/mongo/operation/insert_one.mjs +10 -4
- package/src/usecases/mongo/operation/update_many.mjs +22 -1
- package/src/usecases/mongo/operation/update_one.mjs +23 -1
- package/src/usecases/mongo/transformers/input_transformer.mjs +33 -0
- package/src/usecases/mongo/transformers/output_transformer.mjs +19 -0
- package/src/usecases/mongo/transformers/timestamps.mjs +28 -0
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
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
2
|
|
|
3
|
-
- [_id](##_id)
|
|
3
|
+
- [\_id](##_id)
|
|
4
4
|
- [ObjectId](##ObjectId)
|
|
5
5
|
- [projection](##projection)
|
|
6
6
|
- [Connection String](##connection-string)
|
|
@@ -9,35 +9,36 @@ This is a small package to improve some things that you, as a developer, have to
|
|
|
9
9
|
# Example
|
|
10
10
|
|
|
11
11
|
```js
|
|
12
|
-
import { createMongoCollection } from
|
|
12
|
+
import { createMongoCollection } from 'assai'
|
|
13
13
|
|
|
14
|
+
/** @type {import('assai').ICollection<user>} */
|
|
14
15
|
const collection = await createMongoCollection()
|
|
15
|
-
const docs = await collection.find({}, {limit: 10})
|
|
16
|
+
const docs = await collection.find({}, { limit: 10 })
|
|
16
17
|
/**
|
|
17
18
|
* [{id: "507f1f77bcf86cd799439011", name: "Mario"}, ...]
|
|
18
19
|
*/
|
|
19
20
|
```
|
|
20
21
|
|
|
21
|
-
## _id
|
|
22
|
+
## \_id
|
|
22
23
|
|
|
23
|
-
Ever wanted to use just "id" in your collections instead of "_id"?
|
|
24
|
+
Ever wanted to use just "id" in your collections instead of "\_id"?
|
|
24
25
|
|
|
25
26
|
This package does just that!
|
|
26
27
|
|
|
27
|
-
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.
|
|
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.
|
|
28
29
|
|
|
29
|
-
Also, the methods `updateOne`, `updateMany`, `deleteOne`, `deleteMany`, `findOne` and `find` will also rename the field "id" to "_id".
|
|
30
|
+
Also, the methods `updateOne`, `updateMany`, `deleteOne`, `deleteMany`, `findOne` and `find` will also rename the field "id" to "\_id".
|
|
30
31
|
|
|
31
32
|
## ObjectId
|
|
32
33
|
|
|
33
|
-
Another thing that is related to "_id" fields are the `ObjectId`s.
|
|
34
|
+
Another thing that is related to "\_id" fields are the `ObjectId`s.
|
|
34
35
|
|
|
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.
|
|
36
37
|
|
|
37
38
|
```js
|
|
38
39
|
await collection.insertOne({
|
|
39
|
-
|
|
40
|
-
|
|
40
|
+
name: 'Matteo',
|
|
41
|
+
groupId: '507f1f77bcf86cd799439011', // This will be stored as an ObjectId
|
|
41
42
|
})
|
|
42
43
|
```
|
|
43
44
|
|
|
@@ -54,7 +55,7 @@ One example this could be useful is if have an API endpoint that accepts structu
|
|
|
54
55
|
```js
|
|
55
56
|
// Client code
|
|
56
57
|
const response = await axios.post('/posts', {
|
|
57
|
-
|
|
58
|
+
userId: '507f1f77bcf86cd799439011',
|
|
58
59
|
})
|
|
59
60
|
```
|
|
60
61
|
|
|
@@ -66,18 +67,20 @@ Instead of carrying this risk, you can use the object as-is and the conversion w
|
|
|
66
67
|
|
|
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.
|
|
68
69
|
|
|
69
|
-
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
|
|
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.
|
|
70
71
|
|
|
71
72
|
For that reason, you won't see this error while using this package:
|
|
73
|
+
|
|
72
74
|
```
|
|
73
75
|
Cannot do exclusion on field '...' in inclusion projection
|
|
74
76
|
```
|
|
75
77
|
|
|
76
78
|
Making projections like that valid:
|
|
79
|
+
|
|
77
80
|
```json
|
|
78
81
|
{
|
|
79
|
-
|
|
80
|
-
|
|
82
|
+
"name": true,
|
|
83
|
+
"createdAt": false
|
|
81
84
|
}
|
|
82
85
|
```
|
|
83
86
|
|
|
@@ -86,15 +89,18 @@ Making projections like that valid:
|
|
|
86
89
|
A default environment variable is assumed: DATABASE_URL.
|
|
87
90
|
|
|
88
91
|
Which makes it easier to start a connection:
|
|
92
|
+
|
|
89
93
|
```js
|
|
90
94
|
const database = await createMongoCollection('myCollection')
|
|
91
95
|
```
|
|
96
|
+
|
|
92
97
|
This will read the value from `process.env.DATABASE_URL`.
|
|
93
98
|
|
|
94
99
|
You can still pass a custom connection string:
|
|
100
|
+
|
|
95
101
|
```js
|
|
96
102
|
const database = await createMongoCollection('myCollection', {
|
|
97
|
-
|
|
103
|
+
connectionString: 'my connection string',
|
|
98
104
|
})
|
|
99
105
|
```
|
|
100
106
|
|
|
@@ -103,6 +109,7 @@ const database = await createMongoCollection('myCollection', {
|
|
|
103
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.
|
|
104
110
|
|
|
105
111
|
You could also do this for simplicity, so instead of:
|
|
112
|
+
|
|
106
113
|
```js
|
|
107
114
|
// db.js
|
|
108
115
|
let cachedClient = null
|
|
@@ -127,6 +134,7 @@ router.post('/', (req, res) => {
|
|
|
127
134
|
```
|
|
128
135
|
|
|
129
136
|
You can simply write:
|
|
137
|
+
|
|
130
138
|
```js
|
|
131
139
|
router.post('/', (req, res) => {
|
|
132
140
|
const col = createMongoCollection('myCollection')
|
|
@@ -138,10 +146,3 @@ router.post('/', (req, res) => {
|
|
|
138
146
|
// ...
|
|
139
147
|
})
|
|
140
148
|
```
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
## Remarks
|
|
144
|
-
|
|
145
|
-
This project can contain bugs and should not be used in production applications.
|
|
146
|
-
|
|
147
|
-
If you do find a bug, please report it.
|
|
@@ -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,13 +138,27 @@ 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;
|
|
113
|
-
|
|
141
|
+
/**
|
|
142
|
+
* Configured how the package manages 'createdAt'
|
|
143
|
+
* and 'updatedAt'.
|
|
144
|
+
*/
|
|
145
|
+
timestamps?: ItimestampConfiguration | undefined;
|
|
146
|
+
};
|
|
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
|
+
*/
|
|
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
|
+
*/
|
|
161
|
+
updatedAt: 'generate' | 'none';
|
|
114
162
|
};
|
|
115
|
-
export type IfieldValidator<T> = Partial<Record<keyof T, ((prop: any) => any)>>;
|
|
116
|
-
/**
|
|
117
|
-
* The validator function used in insert operations.
|
|
118
|
-
* This function should throw an exception if the validation fails.
|
|
119
|
-
*/
|
|
120
|
-
export type IgenericValidator<T> = (doc: any) => T | null;
|
|
121
163
|
export type ICollection<T extends import("../types.js").MongoDocument> = Awaited<ReturnType<typeof createMongoCollection<T>>>;
|
|
122
164
|
import { Collection } from 'mongodb';
|
package/dist/src/types.d.ts
CHANGED
|
@@ -15,7 +15,7 @@ type GivenProjectionReturnType<T extends MongoDocument, P extends NonNullProject
|
|
|
15
15
|
[K in keyof T as P[K] extends 1 ? K : never]: T[K];
|
|
16
16
|
};
|
|
17
17
|
export type ProjectionReturnType<T extends MongoDocument, P extends Projection<T>> = P extends NonNullProjection<T> ? GivenProjectionReturnType<T, P> : T;
|
|
18
|
-
export type FindOptions<T extends MongoDocument, K extends Projection<T>> = Omit<FO
|
|
18
|
+
export type FindOptions<T extends MongoDocument, K extends Projection<T>> = Omit<FO, 'projection'> & {
|
|
19
19
|
projection?: K;
|
|
20
20
|
};
|
|
21
21
|
export {};
|
|
@@ -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';
|
|
@@ -5,11 +5,13 @@
|
|
|
5
5
|
* @param {() => Promise<Collection<T>>} parameter.getCollection
|
|
6
6
|
* @param {import('mongodb').Filter<T>} parameter.query
|
|
7
7
|
* @param {import('../../../types.js').FindOptions<T, K>} [parameter.options]
|
|
8
|
+
* @param {import('../../../factories/create_mongo_collection.mjs').IcreateCollectionOptions<T>} [parameter.collectionOptions]
|
|
8
9
|
* @returns {Promise<T[]>}
|
|
9
10
|
*/
|
|
10
|
-
export function find<T extends import("../../../types.js").MongoDocument, K extends import("../../../types.js").Projection<T>>({ getCollection, query, options }: {
|
|
11
|
+
export function find<T extends import("../../../types.js").MongoDocument, K extends import("../../../types.js").Projection<T>>({ getCollection, query, options, collectionOptions }: {
|
|
11
12
|
getCollection: () => Promise<Collection<T>>;
|
|
12
13
|
query: import("mongodb").Filter<T>;
|
|
13
14
|
options?: import("../../../types.js").FindOptions<T, K> | undefined;
|
|
15
|
+
collectionOptions?: import("../../../factories/create_mongo_collection.mjs").IcreateCollectionOptions<T> | undefined;
|
|
14
16
|
}): Promise<T[]>;
|
|
15
17
|
import { Collection } from 'mongodb';
|
|
@@ -5,10 +5,13 @@
|
|
|
5
5
|
* @param {import('mongodb').Filter<T>} param.query
|
|
6
6
|
* @param {import('../../../types.js').FindOptions<T, K>} [param.options]
|
|
7
7
|
* @param {() => Promise<Collection<T>>} param.getCollection
|
|
8
|
+
* @param {import('../../../factories/create_mongo_collection.mjs').IcreateCollectionOptions<T>} param.collectionOptions
|
|
9
|
+
* @returns {Promise<T | null>}
|
|
8
10
|
*/
|
|
9
|
-
export function findOne<T extends import("../../../types.js").MongoDocument, K extends import("../../../types.js").Projection<T>>({ query, options, getCollection }: {
|
|
11
|
+
export function findOne<T extends import("../../../types.js").MongoDocument, K extends import("../../../types.js").Projection<T>>({ query, options, collectionOptions, getCollection }: {
|
|
10
12
|
query: import("mongodb").Filter<T>;
|
|
11
13
|
options?: import("../../../types.js").FindOptions<T, K> | undefined;
|
|
12
14
|
getCollection: () => Promise<Collection<T>>;
|
|
15
|
+
collectionOptions: import("../../../factories/create_mongo_collection.mjs").IcreateCollectionOptions<T>;
|
|
13
16
|
}): Promise<T | null>;
|
|
14
17
|
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";
|
|
@@ -3,10 +3,12 @@
|
|
|
3
3
|
* @param {object} param
|
|
4
4
|
* @param {import('../../../types.js').Optional<T, 'id'>[]} param.docs
|
|
5
5
|
* @param {() => Promise<Collection<T>>} param.getCollection
|
|
6
|
+
* @param {import('../../../factories/create_mongo_collection.mjs').IcreateCollectionOptions<T>} param.collectionOptions
|
|
6
7
|
* @returns {Promise<T[]>}
|
|
7
8
|
*/
|
|
8
|
-
export function insertMany<T extends import("../../../types.js").MongoDocument>({ docs, getCollection }: {
|
|
9
|
+
export function insertMany<T extends import("../../../types.js").MongoDocument>({ docs, collectionOptions, getCollection }: {
|
|
9
10
|
docs: import("../../../types.js").Optional<T, "id">[];
|
|
10
11
|
getCollection: () => Promise<Collection<T>>;
|
|
12
|
+
collectionOptions: import("../../../factories/create_mongo_collection.mjs").IcreateCollectionOptions<T>;
|
|
11
13
|
}): Promise<T[]>;
|
|
12
14
|
import { Collection } from 'mongodb';
|
|
@@ -3,10 +3,12 @@
|
|
|
3
3
|
* @param {object} param
|
|
4
4
|
* @param {import('../../../types.js').Optional<T, 'id'>} param.doc
|
|
5
5
|
* @param {() => Promise<Collection<T>>} param.getCollection
|
|
6
|
+
* @param {import('../../../factories/create_mongo_collection.mjs').IcreateCollectionOptions<T>} [param.collectionOptions]
|
|
6
7
|
* @returns {Promise<T>}
|
|
7
8
|
*/
|
|
8
|
-
export function insertOne<T extends import("../../../types.js").MongoDocument>({ doc, getCollection }: {
|
|
9
|
+
export function insertOne<T extends import("../../../types.js").MongoDocument>({ doc, collectionOptions, getCollection }: {
|
|
9
10
|
doc: import("../../../types.js").Optional<T, "id">;
|
|
10
11
|
getCollection: () => Promise<Collection<T>>;
|
|
12
|
+
collectionOptions?: import("../../../factories/create_mongo_collection.mjs").IcreateCollectionOptions<T> | undefined;
|
|
11
13
|
}): Promise<T>;
|
|
12
14
|
import { Collection } from 'mongodb';
|
|
@@ -5,11 +5,13 @@
|
|
|
5
5
|
* @param {import('mongodb').UpdateFilter<T>} param.update
|
|
6
6
|
* @param {import('mongodb').UpdateOptions} [param.options]
|
|
7
7
|
* @param {() => Promise<Collection<T>>} param.getCollection
|
|
8
|
+
* @param {import('../../../factories/create_mongo_collection.mjs').IcreateCollectionOptions<T>} [param.collectionOptions]
|
|
8
9
|
*/
|
|
9
|
-
export function updateMany<T extends import("../../../types.js").MongoDocument>({ query, update, options, getCollection }: {
|
|
10
|
+
export function updateMany<T extends import("../../../types.js").MongoDocument>({ query, update, options, collectionOptions, getCollection }: {
|
|
10
11
|
query: import("mongodb").Filter<T>;
|
|
11
12
|
update: import("mongodb").UpdateFilter<T>;
|
|
12
13
|
options?: import("mongodb").UpdateOptions | undefined;
|
|
13
14
|
getCollection: () => Promise<Collection<T>>;
|
|
15
|
+
collectionOptions?: import("../../../factories/create_mongo_collection.mjs").IcreateCollectionOptions<T> | undefined;
|
|
14
16
|
}): Promise<import("mongodb").UpdateResult<T>>;
|
|
15
17
|
import { Collection } from 'mongodb';
|
|
@@ -5,11 +5,14 @@
|
|
|
5
5
|
* @param {import('mongodb').UpdateFilter<T>} param.update
|
|
6
6
|
* @param {import('mongodb').UpdateOptions} [param.options]
|
|
7
7
|
* @param {() => Promise<Collection<T>>} param.getCollection
|
|
8
|
+
* @param {import('../../../factories/create_mongo_collection.mjs').IcreateCollectionOptions<T>} [param.collectionOptions]
|
|
9
|
+
* @returns {Promise<boolean>}
|
|
8
10
|
*/
|
|
9
|
-
export function updateOne<T extends import("../../../types.js").MongoDocument>({ query, update, options, getCollection }: {
|
|
11
|
+
export function updateOne<T extends import("../../../types.js").MongoDocument>({ query, update, options, collectionOptions, getCollection }: {
|
|
10
12
|
query: import("mongodb").Filter<T>;
|
|
11
13
|
update: import("mongodb").UpdateFilter<T>;
|
|
12
14
|
options?: import("mongodb").UpdateOptions | undefined;
|
|
13
15
|
getCollection: () => Promise<Collection<T>>;
|
|
16
|
+
collectionOptions?: import("../../../factories/create_mongo_collection.mjs").IcreateCollectionOptions<T> | undefined;
|
|
14
17
|
}): Promise<boolean>;
|
|
15
18
|
import { Collection } from 'mongodb';
|
|
@@ -6,6 +6,7 @@ export function renameFindOptions(options?: import('../../../../types.js').FindO
|
|
|
6
6
|
projection: any;
|
|
7
7
|
sort: any;
|
|
8
8
|
timeout?: boolean | undefined;
|
|
9
|
+
explain?: import("mongodb").ExplainVerbosityLike | import("mongodb").ExplainCommandOptions | undefined;
|
|
9
10
|
checkKeys?: boolean | undefined;
|
|
10
11
|
serializeFunctions?: boolean | undefined;
|
|
11
12
|
ignoreUndefined?: boolean | undefined;
|
|
@@ -20,16 +21,13 @@ export function renameFindOptions(options?: import('../../../../types.js').FindO
|
|
|
20
21
|
collation?: import("mongodb").CollationOptions | undefined;
|
|
21
22
|
maxTimeMS?: number | undefined;
|
|
22
23
|
comment?: unknown;
|
|
23
|
-
retryWrites?: boolean | undefined;
|
|
24
24
|
dbName?: string | undefined;
|
|
25
25
|
authdb?: string | undefined;
|
|
26
|
-
noResponse?: boolean | undefined;
|
|
27
26
|
session?: import("mongodb").ClientSession | undefined;
|
|
28
27
|
willRetryWrite?: boolean | undefined;
|
|
29
28
|
readPreference?: import("mongodb").ReadPreferenceLike | undefined;
|
|
30
|
-
|
|
29
|
+
timeoutMS?: number | undefined;
|
|
31
30
|
enableUtf8Validation?: boolean | undefined;
|
|
32
|
-
explain?: import("mongodb").ExplainVerbosityLike | undefined;
|
|
33
31
|
limit?: number | undefined;
|
|
34
32
|
skip?: number | undefined;
|
|
35
33
|
hint?: import("mongodb").Hint | undefined;
|
|
@@ -47,4 +45,5 @@ export function renameFindOptions(options?: import('../../../../types.js').FindO
|
|
|
47
45
|
showRecordId?: boolean | undefined;
|
|
48
46
|
let?: import("bson").Document | undefined;
|
|
49
47
|
oplogReplay?: boolean | undefined;
|
|
48
|
+
timeoutMode?: import("mongodb").CursorTimeoutMode | undefined;
|
|
50
49
|
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @template {import('../../../types.js').MongoDocument} T
|
|
3
|
+
* @param {object} params
|
|
4
|
+
* @param {import('../../../types.js').Optional<T, 'id'>} params.document
|
|
5
|
+
* @param {import('../../../factories/create_mongo_collection.mjs').IcreateCollectionOptions<T>} [params.collectionOptions]
|
|
6
|
+
*/
|
|
7
|
+
export function inputTransformer<T extends import("../../../types.js").MongoDocument>({ document, collectionOptions, }: {
|
|
8
|
+
document: import("../../../types.js").Optional<T, "id">;
|
|
9
|
+
collectionOptions?: import("../../../factories/create_mongo_collection.mjs").IcreateCollectionOptions<T> | undefined;
|
|
10
|
+
}): import("../../../types.js").Optional<T, "id">;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @template {import('../../../types.js').MongoDocument} T
|
|
3
|
+
* @param {object} p
|
|
4
|
+
* @param {import('mongodb').WithId<T> | T} p.document
|
|
5
|
+
* @param {import('../../../factories/create_mongo_collection.mjs').IcreateCollectionOptions<T>} [p.collectionOptions]
|
|
6
|
+
*/
|
|
7
|
+
export function outputTransformer<T extends import("../../../types.js").MongoDocument>({ document, collectionOptions, }: {
|
|
8
|
+
document: T | import("mongodb").WithId<T>;
|
|
9
|
+
collectionOptions?: import("../../../factories/create_mongo_collection.mjs").IcreateCollectionOptions<T> | undefined;
|
|
10
|
+
}): any;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @template {import('../../../types.js').MongoDocument} T
|
|
3
|
+
* @param {import('mongodb').WithId<T> | import('../../../types.js').MongoDocument} doc
|
|
4
|
+
* @param {import('../../../factories/create_mongo_collection.mjs').IcreateCollectionOptions<T>} options
|
|
5
|
+
*/
|
|
6
|
+
export function timestampTransformer<T extends import("../../../types.js").MongoDocument>(doc: import("../../../types.js").MongoDocument | import("mongodb").WithId<T>, options: import("../../../factories/create_mongo_collection.mjs").IcreateCollectionOptions<T>): void;
|
package/jsconfig.json
CHANGED
|
@@ -1,20 +1,17 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
"outDir": "dist"
|
|
19
|
-
},
|
|
20
|
-
}
|
|
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "assai",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.1.0",
|
|
4
4
|
"repository": {
|
|
5
5
|
"url": "https://github.com/TimeLord2010/assai",
|
|
6
6
|
"type": "git"
|
|
@@ -11,7 +11,8 @@
|
|
|
11
11
|
"scripts": {
|
|
12
12
|
"prepare": "rm -rf dist && tsc -p ./jsconfig.prod.json",
|
|
13
13
|
"start": "node --env-file=.env index.mjs",
|
|
14
|
-
"test": "node --test"
|
|
14
|
+
"test": "node --test",
|
|
15
|
+
"check": "tsc -p jsconfig.json"
|
|
15
16
|
},
|
|
16
17
|
"keywords": [
|
|
17
18
|
"mongodb",
|
|
@@ -28,7 +29,7 @@
|
|
|
28
29
|
"author": "Vinícius Gabriel",
|
|
29
30
|
"license": "ISC",
|
|
30
31
|
"dependencies": {
|
|
31
|
-
"mongodb": "^
|
|
32
|
+
"mongodb": "^7.0.0"
|
|
32
33
|
},
|
|
33
34
|
"devDependencies": {
|
|
34
35
|
"@faker-js/faker": "^8.4.1",
|
|
@@ -36,4 +37,4 @@
|
|
|
36
37
|
"mongodb-memory-server": "^10.0.0",
|
|
37
38
|
"typescript": "^5.4.5"
|
|
38
39
|
}
|
|
39
|
-
}
|
|
40
|
+
}
|