assai 0.4.3 → 0.4.5
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/dist/src/factories/create_mongo_collection.d.mts +22 -4
- package/dist/src/usecases/mongo/operation/delete_many.d.mts +1 -1
- package/dist/src/usecases/mongo/operation/update_many.d.mts +1 -1
- package/package.json +2 -2
- package/src/factories/create_mongo_collection.mjs +20 -3
- package/src/usecases/mongo/operation/delete_many.mjs +1 -1
- package/src/usecases/mongo/operation/update_many.mjs +1 -1
- package/src/usecases/mongo/test/manage_mock_registry.mjs +9 -7
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
* @param {string} name
|
|
11
11
|
* @param {IcreateCollectionOptions<T>} [options]
|
|
12
12
|
*/
|
|
13
|
-
export function createMongoCollection<T extends import("../types.js").MongoDocument>(name: string, options?: IcreateCollectionOptions<T> | undefined):
|
|
13
|
+
export function createMongoCollection<T extends import("../types.js").MongoDocument>(name: string, options?: IcreateCollectionOptions<T> | undefined): {
|
|
14
14
|
/**
|
|
15
15
|
* Returns the native driver.
|
|
16
16
|
*/
|
|
@@ -60,7 +60,7 @@ export function createMongoCollection<T extends import("../types.js").MongoDocum
|
|
|
60
60
|
*
|
|
61
61
|
* @param {import('mongodb').Filter<T>} query
|
|
62
62
|
*/
|
|
63
|
-
deleteMany: (query: import("mongodb").Filter<T>) => Promise<
|
|
63
|
+
deleteMany: (query: import("mongodb").Filter<T>) => Promise<number>;
|
|
64
64
|
/**
|
|
65
65
|
* @param {import('mongodb').Filter<T>} query
|
|
66
66
|
* @param {import('mongodb').UpdateFilter<T>} update
|
|
@@ -70,8 +70,8 @@ export function createMongoCollection<T extends import("../types.js").MongoDocum
|
|
|
70
70
|
* @param {import('mongodb').Filter<T>} query
|
|
71
71
|
* @param {import('mongodb').UpdateFilter<T>} update
|
|
72
72
|
*/
|
|
73
|
-
updateMany: (query: import("mongodb").Filter<T>, update: import("mongodb").UpdateFilter<T>) => Promise<
|
|
74
|
-
}
|
|
73
|
+
updateMany: (query: import("mongodb").Filter<T>, update: import("mongodb").UpdateFilter<T>) => Promise<import("mongodb").UpdateResult<T>>;
|
|
74
|
+
};
|
|
75
75
|
export type IcollectionGetter<T extends import("../types.js").MongoDocument> = () => Promise<Collection<T>>;
|
|
76
76
|
export type IcreateCollectionOptions<T extends import("../types.js").MongoDocument> = {
|
|
77
77
|
/**
|
|
@@ -80,16 +80,34 @@ export type IcreateCollectionOptions<T extends import("../types.js").MongoDocume
|
|
|
80
80
|
* This will be not be cached and will be used on every operation such as insertOne or findOne.
|
|
81
81
|
*
|
|
82
82
|
* If you wish to cache the collection object, see `cachableCollectionGetter`.
|
|
83
|
+
*
|
|
84
|
+
* Example:
|
|
85
|
+
* ```js
|
|
86
|
+
* collectionGetter: async () => await getMyCollection()
|
|
87
|
+
* ```
|
|
83
88
|
*/
|
|
84
89
|
collectionGetter?: IcollectionGetter<T> | undefined;
|
|
85
90
|
/**
|
|
86
91
|
* Same as `collectionGetter`. But the object is cached.
|
|
92
|
+
*
|
|
93
|
+
* Example:
|
|
94
|
+
* ```js
|
|
95
|
+
* cachableCollectionGetter: async () => await getMyCollection()
|
|
96
|
+
* ```
|
|
87
97
|
*/
|
|
88
98
|
cachableCollectionGetter?: IcollectionGetter<T> | undefined;
|
|
89
99
|
/**
|
|
90
100
|
* A custom connection string. If none is given, `process.env.DATABASE_URL` will be used.
|
|
91
101
|
*/
|
|
92
102
|
connectionString?: string | undefined;
|
|
103
|
+
/**
|
|
104
|
+
* The name of the database.
|
|
105
|
+
*/
|
|
106
|
+
dbName?: string | undefined;
|
|
107
|
+
/**
|
|
108
|
+
* The options used when initializing client.
|
|
109
|
+
*/
|
|
110
|
+
options?: import("mongodb").DbOptions | undefined;
|
|
93
111
|
};
|
|
94
112
|
export type ICollection<T extends import("../types.js").MongoDocument> = Awaited<ReturnType<typeof createMongoCollection<T>>>;
|
|
95
113
|
import { Collection } from 'mongodb';
|
|
@@ -7,5 +7,5 @@
|
|
|
7
7
|
export function deleteMany<T extends import("../../../types.js").MongoDocument>({ query, getCollection }: {
|
|
8
8
|
query: import("mongodb").Filter<T>;
|
|
9
9
|
getCollection: () => Promise<Collection<T>>;
|
|
10
|
-
}): Promise<
|
|
10
|
+
}): Promise<number>;
|
|
11
11
|
import { Collection } from 'mongodb';
|
|
@@ -9,5 +9,5 @@ export function updateMany<T extends import("../../../types.js").MongoDocument>(
|
|
|
9
9
|
query: import("mongodb").Filter<T>;
|
|
10
10
|
update: import("mongodb").UpdateFilter<T>;
|
|
11
11
|
getCollection: () => Promise<Collection<T>>;
|
|
12
|
-
}): Promise<
|
|
12
|
+
}): Promise<import("mongodb").UpdateResult<T>>;
|
|
13
13
|
import { Collection } from 'mongodb';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "assai",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.5",
|
|
4
4
|
"repository": {
|
|
5
5
|
"url": "https://github.com/TimeLord2010/assai",
|
|
6
6
|
"type": "git"
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"main": "index.mjs",
|
|
10
10
|
"types": "dist/index.d.mts",
|
|
11
11
|
"scripts": {
|
|
12
|
-
"prepare": "tsc -p ./jsconfig.prod.json",
|
|
12
|
+
"prepare": "rm -rf dist && tsc -p ./jsconfig.prod.json",
|
|
13
13
|
"start": "node --env-file=.env index.mjs",
|
|
14
14
|
"test": "node --env-file=.env --test"
|
|
15
15
|
},
|
|
@@ -19,13 +19,16 @@ import {
|
|
|
19
19
|
* @param {string} name
|
|
20
20
|
* @param {IcreateCollectionOptions<T>} [options]
|
|
21
21
|
*/
|
|
22
|
-
export
|
|
22
|
+
export function createMongoCollection(name, options = {}) {
|
|
23
23
|
|
|
24
24
|
/** @type {Collection<T> | null} */
|
|
25
25
|
let _collection = null
|
|
26
26
|
|
|
27
27
|
async function getCollection() {
|
|
28
|
-
const {
|
|
28
|
+
const {
|
|
29
|
+
connectionString, dbName, options: createOptions,
|
|
30
|
+
cachableCollectionGetter, collectionGetter
|
|
31
|
+
} = options
|
|
29
32
|
|
|
30
33
|
// Connection getter from options
|
|
31
34
|
if (collectionGetter != null) return await collectionGetter()
|
|
@@ -41,7 +44,7 @@ export async function createMongoCollection(name, options = {}) {
|
|
|
41
44
|
|
|
42
45
|
// Default connection getter
|
|
43
46
|
const client = await getClient({ connectionString })
|
|
44
|
-
let db = client.db()
|
|
47
|
+
let db = client.db(dbName, createOptions)
|
|
45
48
|
/** @type {Collection<T>} */
|
|
46
49
|
_collection = db.collection(name)
|
|
47
50
|
return _collection
|
|
@@ -129,11 +132,25 @@ export async function createMongoCollection(name, options = {}) {
|
|
|
129
132
|
*
|
|
130
133
|
* If you wish to cache the collection object, see `cachableCollectionGetter`.
|
|
131
134
|
*
|
|
135
|
+
* Example:
|
|
136
|
+
* ```js
|
|
137
|
+
* collectionGetter: async () => await getMyCollection()
|
|
138
|
+
* ```
|
|
139
|
+
*
|
|
132
140
|
* @property {IcollectionGetter<T>} [options.cachableCollectionGetter]
|
|
133
141
|
* Same as `collectionGetter`. But the object is cached.
|
|
134
142
|
*
|
|
143
|
+
* Example:
|
|
144
|
+
* ```js
|
|
145
|
+
* cachableCollectionGetter: async () => await getMyCollection()
|
|
146
|
+
* ```
|
|
147
|
+
*
|
|
135
148
|
* @property {string} [options.connectionString]
|
|
136
149
|
* A custom connection string. If none is given, `process.env.DATABASE_URL` will be used.
|
|
150
|
+
*
|
|
151
|
+
* @property {string} [dbName] The name of the database.
|
|
152
|
+
*
|
|
153
|
+
* @property {import('mongodb').DbOptions} [options] The options used when initializing client.
|
|
137
154
|
*/
|
|
138
155
|
|
|
139
156
|
/**
|
|
@@ -20,16 +20,18 @@ export function manageMockRegistry({ tag, ...rest } = {}, {
|
|
|
20
20
|
const name = rest.name ?? fakerPT_BR.person.fullName()
|
|
21
21
|
|
|
22
22
|
before(async () => {
|
|
23
|
+
/** @type {import('./mock_get_collection.mjs').ItestCollection} */
|
|
24
|
+
const doc = {
|
|
25
|
+
id,
|
|
26
|
+
createdAt: new Date(),
|
|
27
|
+
tag,
|
|
28
|
+
name: name,
|
|
29
|
+
...rest,
|
|
30
|
+
}
|
|
23
31
|
await insertOne({
|
|
24
32
|
// @ts-ignore
|
|
25
33
|
getCollection: mockGetCollection,
|
|
26
|
-
doc:
|
|
27
|
-
id,
|
|
28
|
-
createdAt: new Date(),
|
|
29
|
-
tag,
|
|
30
|
-
name: name,
|
|
31
|
-
...rest,
|
|
32
|
-
},
|
|
34
|
+
doc: doc,
|
|
33
35
|
})
|
|
34
36
|
})
|
|
35
37
|
|