assai 0.4.4 → 0.4.6
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 +8 -1
- package/dist/src/factories/create_mongo_collection.d.mts +20 -2
- 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/dist/src/usecases/mongo/transformers/id/rename_find_options.d.mts +4 -1
- package/package.json +1 -1
- package/src/factories/create_mongo_collection.mjs +19 -2
- package/src/usecases/mongo/operation/count.mjs +1 -1
- package/src/usecases/mongo/operation/delete_many.mjs +2 -2
- package/src/usecases/mongo/operation/delete_one.mjs +1 -1
- package/src/usecases/mongo/operation/find.mjs +2 -2
- package/src/usecases/mongo/operation/insert_many.mjs +8 -3
- package/src/usecases/mongo/operation/insert_one.mjs +1 -1
- package/src/usecases/mongo/operation/update_many.mjs +2 -2
- package/src/usecases/mongo/operation/update_one.mjs +1 -1
- package/src/usecases/mongo/test/manage_mock_registry.mjs +9 -7
- package/src/usecases/mongo/transformers/id/rename_find_options.mjs +4 -2
- package/src/usecases/mongo/transformers/id/rename_to_dev_id.mjs +4 -6
- package/src/usecases/mongo/transformers/id/rename_to_mongo_id.mjs +4 -6
- package/src/usecases/mongo/transformers/object_id/strings_into_id.mjs +3 -0
package/README.md
CHANGED
|
@@ -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,7 +70,7 @@ 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<
|
|
73
|
+
updateMany: (query: import("mongodb").Filter<T>, update: import("mongodb").UpdateFilter<T>) => Promise<import("mongodb").UpdateResult<T>>;
|
|
74
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> = {
|
|
@@ -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';
|
|
@@ -2,4 +2,7 @@
|
|
|
2
2
|
*
|
|
3
3
|
* @param {import('../../../../types.js').FindOptions<any ,any>} options
|
|
4
4
|
*/
|
|
5
|
-
export function renameFindOptions(options?: import('../../../../types.js').FindOptions<any, any>):
|
|
5
|
+
export function renameFindOptions(options?: import('../../../../types.js').FindOptions<any, any>): {
|
|
6
|
+
projection: any;
|
|
7
|
+
sort: any;
|
|
8
|
+
};
|
package/package.json
CHANGED
|
@@ -25,7 +25,10 @@ export function createMongoCollection(name, options = {}) {
|
|
|
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 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 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
|
/**
|
|
@@ -8,7 +8,7 @@ import { renameToMongoId, stringsIntoId } from '../transformers/index.mjs'
|
|
|
8
8
|
* @param {import('mongodb').Filter<T>} [params.query]
|
|
9
9
|
*/
|
|
10
10
|
export async function count({ getCollection, query }) {
|
|
11
|
-
renameToMongoId(query)
|
|
11
|
+
query = renameToMongoId(query)
|
|
12
12
|
stringsIntoId(query)
|
|
13
13
|
|
|
14
14
|
const col = await getCollection()
|
|
@@ -8,9 +8,9 @@ import { renameToMongoId, stringsIntoId } from '../transformers/index.mjs'
|
|
|
8
8
|
* @param {() => Promise<Collection<T>>} parameter.getCollection
|
|
9
9
|
*/
|
|
10
10
|
export async function deleteMany({ query, getCollection }) {
|
|
11
|
-
renameToMongoId(query)
|
|
11
|
+
query = renameToMongoId(query)
|
|
12
12
|
stringsIntoId(query)
|
|
13
13
|
const col = await getCollection()
|
|
14
14
|
const r = await col.deleteMany(query)
|
|
15
|
-
return r.deletedCount
|
|
15
|
+
return r.deletedCount
|
|
16
16
|
}
|
|
@@ -9,7 +9,7 @@ import { renameToMongoId, stringsIntoId } from '../transformers/index.mjs'
|
|
|
9
9
|
* @returns {Promise<boolean>}
|
|
10
10
|
*/
|
|
11
11
|
export async function deleteOne({ query, getCollection }) {
|
|
12
|
-
renameToMongoId(query)
|
|
12
|
+
query = renameToMongoId(query)
|
|
13
13
|
stringsIntoId(query)
|
|
14
14
|
const col = await getCollection()
|
|
15
15
|
const r = await col.deleteOne(query)
|
|
@@ -17,8 +17,8 @@ import {
|
|
|
17
17
|
* @returns {Promise<T[]>}
|
|
18
18
|
*/
|
|
19
19
|
export async function find({ getCollection, query, options }) {
|
|
20
|
-
renameToMongoId(query)
|
|
21
|
-
renameFindOptions(options)
|
|
20
|
+
query = renameToMongoId(query)
|
|
21
|
+
options = renameFindOptions(options)
|
|
22
22
|
|
|
23
23
|
stringsIntoId(query)
|
|
24
24
|
|
|
@@ -10,9 +10,14 @@ import { renameToMongoId, stringsIntoId } from '../transformers/index.mjs'
|
|
|
10
10
|
*/
|
|
11
11
|
export async function insertMany({ docs, getCollection }) {
|
|
12
12
|
if (docs.length == 0) return []
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
|
|
14
|
+
// We need to clone the array to prevent the function from changing the original input object.
|
|
15
|
+
// This is necessary since we change the objects inside the array to rename the "id" to "_id".
|
|
16
|
+
docs = [...docs]
|
|
17
|
+
|
|
18
|
+
for (let i = 0; i < docs.length; i++) {
|
|
19
|
+
docs[i] = renameToMongoId(docs[i])
|
|
20
|
+
stringsIntoId(docs[i])
|
|
16
21
|
}
|
|
17
22
|
const col = await getCollection()
|
|
18
23
|
const result = await col.insertMany(
|
|
@@ -9,7 +9,7 @@ import { renameToMongoId, stringsIntoId } from '../transformers/index.mjs'
|
|
|
9
9
|
* @returns {Promise<T>}
|
|
10
10
|
*/
|
|
11
11
|
export async function insertOne({ doc, getCollection }) {
|
|
12
|
-
renameToMongoId(doc)
|
|
12
|
+
doc = renameToMongoId(doc)
|
|
13
13
|
stringsIntoId(doc)
|
|
14
14
|
const col = await getCollection()
|
|
15
15
|
const result = await col.insertOne(
|
|
@@ -9,10 +9,10 @@ import { renameToMongoId, stringsIntoId } from '../transformers/index.mjs'
|
|
|
9
9
|
* @param {() => Promise<Collection<T>>} param.getCollection
|
|
10
10
|
*/
|
|
11
11
|
export async function updateMany({ query, update, getCollection }) {
|
|
12
|
-
renameToMongoId(query)
|
|
12
|
+
query = renameToMongoId(query)
|
|
13
13
|
stringsIntoId(query)
|
|
14
14
|
stringsIntoId(update)
|
|
15
15
|
const col = await getCollection()
|
|
16
16
|
const r = await col.updateMany(query, update)
|
|
17
|
-
return r
|
|
17
|
+
return r
|
|
18
18
|
}
|
|
@@ -9,7 +9,7 @@ import { renameToMongoId, stringsIntoId } from '../transformers/index.mjs'
|
|
|
9
9
|
* @param {() => Promise<Collection<T>>} param.getCollection
|
|
10
10
|
*/
|
|
11
11
|
export async function updateOne({ query, update, getCollection }) {
|
|
12
|
-
renameToMongoId(query)
|
|
12
|
+
query = renameToMongoId(query)
|
|
13
13
|
stringsIntoId(query)
|
|
14
14
|
stringsIntoId(update)
|
|
15
15
|
const col = await getCollection()
|
|
@@ -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
|
|
|
@@ -5,6 +5,8 @@ import { renameToMongoId } from './index.mjs'
|
|
|
5
5
|
* @param {import('../../../../types.js').FindOptions<any ,any>} options
|
|
6
6
|
*/
|
|
7
7
|
export function renameFindOptions(options = {}) {
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
return {
|
|
9
|
+
projection: renameToMongoId(options.projection),
|
|
10
|
+
sort: renameToMongoId(options.sort)
|
|
11
|
+
}
|
|
10
12
|
}
|
|
@@ -7,10 +7,8 @@ export function renameToDevId(obj) {
|
|
|
7
7
|
if (!obj) return obj
|
|
8
8
|
let { _id, ...rest } = obj
|
|
9
9
|
if (!_id) return obj
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
obj.id = _id
|
|
15
|
-
return obj
|
|
10
|
+
return {
|
|
11
|
+
id: _id,
|
|
12
|
+
...rest
|
|
13
|
+
}
|
|
16
14
|
}
|
|
@@ -7,10 +7,8 @@ export function renameToMongoId(obj) {
|
|
|
7
7
|
if (!obj) return obj
|
|
8
8
|
let { id, ...rest } = obj
|
|
9
9
|
if (!id) return obj
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
obj._id = id
|
|
15
|
-
return obj
|
|
10
|
+
return {
|
|
11
|
+
_id: id,
|
|
12
|
+
...rest
|
|
13
|
+
}
|
|
16
14
|
}
|
|
@@ -15,6 +15,9 @@ export function stringsIntoId(obj) {
|
|
|
15
15
|
if (isObjectIdString(obj)) return new ObjectId(obj)
|
|
16
16
|
if (typeof obj != 'object') return obj
|
|
17
17
|
if (Array.isArray(obj)) {
|
|
18
|
+
// We clone the array to prevent the original array reference to change.
|
|
19
|
+
obj = [...obj]
|
|
20
|
+
|
|
18
21
|
for (let i = 0; i < obj.length; i++) {
|
|
19
22
|
const item = obj[i]
|
|
20
23
|
obj[i] = stringsIntoId(item)
|