assai 2.0.0 → 2.1.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.
Files changed (60) hide show
  1. package/README.md +148 -148
  2. package/dist/src/__test/mock_get_collection.d.mts +2 -0
  3. package/dist/src/factories/create_mongo_collection.d.mts +55 -11
  4. package/dist/src/usecases/mongo/operation/aggregate.d.mts +16 -0
  5. package/dist/src/usecases/mongo/operation/bulk_write.d.mts +15 -0
  6. package/dist/src/usecases/mongo/operation/find_one_and_delete.d.mts +16 -0
  7. package/dist/src/usecases/mongo/operation/find_one_and_replace.d.mts +18 -0
  8. package/dist/src/usecases/mongo/operation/find_one_and_update.d.mts +18 -0
  9. package/dist/src/usecases/mongo/operation/index.d.mts +5 -0
  10. package/dist/src/usecases/mongo/transformers/id/rename_to_mongo_id.d.mts +3 -0
  11. package/dist/src/usecases/mongo/transformers/object_id/ids_into_strings.d.mts +5 -0
  12. package/dist/src/usecases/mongo/transformers/object_id/strings_into_id.d.mts +3 -0
  13. package/dist/src/usecases/mongo/transformers/timestamps.d.mts +5 -0
  14. package/index.mjs +2 -2
  15. package/jsconfig.json +17 -20
  16. package/jsconfig.prod.json +12 -12
  17. package/package.json +40 -39
  18. package/src/__test/manage_mock_database.mjs +24 -24
  19. package/src/__test/manage_mock_registry.mjs +54 -54
  20. package/src/__test/mock_get_collection.mjs +22 -20
  21. package/src/data/errors/operation_fail.mjs +9 -9
  22. package/src/data/errors/unsupported_error.mjs +9 -9
  23. package/src/data/interfaces/mongo_operator.mjs +2 -2
  24. package/src/data/interfaces/native_collection.mjs +27 -27
  25. package/src/factories/create_mock_collection.mjs +173 -173
  26. package/src/factories/create_mongo_collection.mjs +224 -181
  27. package/src/factories/index.mjs +1 -1
  28. package/src/types.ts +31 -31
  29. package/src/usecases/index.mjs +1 -1
  30. package/src/usecases/mongo/generate_new_id.mjs +4 -4
  31. package/src/usecases/mongo/index.mjs +3 -3
  32. package/src/usecases/mongo/mongo_client.mjs +36 -36
  33. package/src/usecases/mongo/operation/additional/delete_one_or_throw.mjs +17 -17
  34. package/src/usecases/mongo/operation/additional/index.mjs +1 -1
  35. package/src/usecases/mongo/operation/aggregate.mjs +21 -0
  36. package/src/usecases/mongo/operation/bulk_write.mjs +54 -0
  37. package/src/usecases/mongo/operation/count.mjs +16 -16
  38. package/src/usecases/mongo/operation/delete_many.mjs +15 -15
  39. package/src/usecases/mongo/operation/delete_one.mjs +16 -16
  40. package/src/usecases/mongo/operation/find.mjs +34 -34
  41. package/src/usecases/mongo/operation/find_one.mjs +32 -32
  42. package/src/usecases/mongo/operation/find_one_and_delete.mjs +23 -0
  43. package/src/usecases/mongo/operation/find_one_and_replace.mjs +24 -0
  44. package/src/usecases/mongo/operation/find_one_and_update.mjs +40 -0
  45. package/src/usecases/mongo/operation/index.mjs +15 -10
  46. package/src/usecases/mongo/operation/insert_many.mjs +39 -39
  47. package/src/usecases/mongo/operation/insert_one.mjs +32 -32
  48. package/src/usecases/mongo/operation/update_many.mjs +39 -39
  49. package/src/usecases/mongo/operation/update_one.mjs +40 -40
  50. package/src/usecases/mongo/transformers/id/index.mjs +4 -4
  51. package/src/usecases/mongo/transformers/id/rename_find_options.mjs +12 -12
  52. package/src/usecases/mongo/transformers/id/rename_to_dev_id.mjs +14 -13
  53. package/src/usecases/mongo/transformers/id/rename_to_mongo_id.mjs +34 -13
  54. package/src/usecases/mongo/transformers/index.mjs +2 -2
  55. package/src/usecases/mongo/transformers/input_transformer.mjs +32 -32
  56. package/src/usecases/mongo/transformers/object_id/ids_into_strings.mjs +47 -30
  57. package/src/usecases/mongo/transformers/object_id/index.mjs +3 -3
  58. package/src/usecases/mongo/transformers/object_id/strings_into_id.mjs +90 -44
  59. package/src/usecases/mongo/transformers/output_transformer.mjs +18 -18
  60. package/src/usecases/mongo/transformers/timestamps.mjs +32 -22
@@ -0,0 +1,54 @@
1
+ import { Collection } from 'mongodb'
2
+ import { renameToMongoId, stringsIntoId } from '../transformers/index.mjs'
3
+ import { inputTransformer } from '../transformers/input_transformer.mjs'
4
+
5
+ /**
6
+ * @template {import('../../../types.js').MongoDocument} T
7
+ * @param {object} parameter
8
+ * @param {() => Promise<Collection<T>>} parameter.getCollection
9
+ * @param {import('mongodb').AnyBulkWriteOperation<T>[]} parameter.operations
10
+ * @param {import('mongodb').BulkWriteOptions} [parameter.options]
11
+ * @param {import('../../../factories/create_mongo_collection.mjs').IcreateCollectionOptions<T>} [parameter.collectionOptions]
12
+ */
13
+ export async function bulkWrite({ getCollection, operations, options, collectionOptions }) {
14
+ for (const op of operations) {
15
+ /** @type {any} */
16
+ const o = op
17
+ if (o.insertOne != null) {
18
+ o.insertOne.document = inputTransformer({
19
+ document: o.insertOne.document,
20
+ collectionOptions,
21
+ })
22
+ } else if (o.updateOne != null) {
23
+ o.updateOne.filter = renameToMongoId(o.updateOne.filter)
24
+ o.updateOne.filter = stringsIntoId(o.updateOne.filter)
25
+ o.updateOne.update = stringsIntoId(o.updateOne.update)
26
+ } else if (o.updateMany != null) {
27
+ o.updateMany.filter = renameToMongoId(o.updateMany.filter)
28
+ o.updateMany.filter = stringsIntoId(o.updateMany.filter)
29
+ o.updateMany.update = stringsIntoId(o.updateMany.update)
30
+ } else if (o.deleteOne != null) {
31
+ o.deleteOne.filter = renameToMongoId(o.deleteOne.filter)
32
+ o.deleteOne.filter = stringsIntoId(o.deleteOne.filter)
33
+ } else if (o.deleteMany != null) {
34
+ o.deleteMany.filter = renameToMongoId(o.deleteMany.filter)
35
+ o.deleteMany.filter = stringsIntoId(o.deleteMany.filter)
36
+ } else if (o.replaceOne != null) {
37
+ o.replaceOne.filter = renameToMongoId(o.replaceOne.filter)
38
+ o.replaceOne.filter = stringsIntoId(o.replaceOne.filter)
39
+ o.replaceOne.replacement = stringsIntoId(o.replaceOne.replacement)
40
+ }
41
+ }
42
+
43
+ const col = await getCollection()
44
+ const result = await col.bulkWrite(operations, options)
45
+
46
+ if (result.insertedIds) {
47
+ for (const key of Object.keys(result.insertedIds)) {
48
+ // @ts-ignore
49
+ result.insertedIds[key] = result.insertedIds[key].toHexString()
50
+ }
51
+ }
52
+
53
+ return result
54
+ }
@@ -1,17 +1,17 @@
1
- import { Collection } from 'mongodb'
2
- import { renameToMongoId, stringsIntoId } from '../transformers/index.mjs'
3
-
4
- /**
5
- * @template {import('../../../types.js').MongoDocument} T
6
- * @param {object} params
7
- * @param {() => Promise<Collection<T>>} params.getCollection
8
- * @param {import('mongodb').Filter<T>} [params.query]
9
- */
10
- export async function count({ getCollection, query }) {
11
- query = renameToMongoId(query)
12
- stringsIntoId(query)
13
-
14
- const col = await getCollection()
15
- const count = await col.countDocuments(query)
16
- return count
1
+ import { Collection } from 'mongodb'
2
+ import { renameToMongoId, stringsIntoId } from '../transformers/index.mjs'
3
+
4
+ /**
5
+ * @template {import('../../../types.js').MongoDocument} T
6
+ * @param {object} params
7
+ * @param {() => Promise<Collection<T>>} params.getCollection
8
+ * @param {import('mongodb').Filter<T>} [params.query]
9
+ */
10
+ export async function count({ getCollection, query }) {
11
+ query = renameToMongoId(query)
12
+ query = stringsIntoId(query)
13
+
14
+ const col = await getCollection()
15
+ const count = await col.countDocuments(query)
16
+ return count
17
17
  }
@@ -1,16 +1,16 @@
1
- import { Collection } from 'mongodb'
2
- import { renameToMongoId, stringsIntoId } from '../transformers/index.mjs'
3
-
4
- /**
5
- * @template {import('../../../types.js').MongoDocument} T
6
- * @param {object} parameter
7
- * @param {import('mongodb').Filter<T>} parameter.query
8
- * @param {() => Promise<Collection<T>>} parameter.getCollection
9
- */
10
- export async function deleteMany({ query, getCollection }) {
11
- query = renameToMongoId(query)
12
- stringsIntoId(query)
13
- const col = await getCollection()
14
- const r = await col.deleteMany(query)
15
- return r.deletedCount
1
+ import { Collection } from 'mongodb'
2
+ import { renameToMongoId, stringsIntoId } from '../transformers/index.mjs'
3
+
4
+ /**
5
+ * @template {import('../../../types.js').MongoDocument} T
6
+ * @param {object} parameter
7
+ * @param {import('mongodb').Filter<T>} parameter.query
8
+ * @param {() => Promise<Collection<T>>} parameter.getCollection
9
+ */
10
+ export async function deleteMany({ query, getCollection }) {
11
+ query = renameToMongoId(query)
12
+ query = stringsIntoId(query)
13
+ const col = await getCollection()
14
+ const r = await col.deleteMany(query)
15
+ return r.deletedCount
16
16
  }
@@ -1,17 +1,17 @@
1
- import { Collection } from 'mongodb'
2
- import { renameToMongoId, stringsIntoId } from '../transformers/index.mjs'
3
-
4
- /**
5
- * @template {import('../../../types.js').MongoDocument} T
6
- * @param {object} parameter
7
- * @param {import('mongodb').Filter<T>} parameter.query
8
- * @param {() => Promise<Collection<T>>} parameter.getCollection
9
- * @returns {Promise<boolean>}
10
- */
11
- export async function deleteOne({ query, getCollection }) {
12
- query = renameToMongoId(query)
13
- stringsIntoId(query)
14
- const col = await getCollection()
15
- const r = await col.deleteOne(query)
16
- return r.deletedCount > 0
1
+ import { Collection } from 'mongodb'
2
+ import { renameToMongoId, stringsIntoId } from '../transformers/index.mjs'
3
+
4
+ /**
5
+ * @template {import('../../../types.js').MongoDocument} T
6
+ * @param {object} parameter
7
+ * @param {import('mongodb').Filter<T>} parameter.query
8
+ * @param {() => Promise<Collection<T>>} parameter.getCollection
9
+ * @returns {Promise<boolean>}
10
+ */
11
+ export async function deleteOne({ query, getCollection }) {
12
+ query = renameToMongoId(query)
13
+ query = stringsIntoId(query)
14
+ const col = await getCollection()
15
+ const r = await col.deleteOne(query)
16
+ return r.deletedCount > 0
17
17
  }
@@ -1,35 +1,35 @@
1
- import { Collection } from 'mongodb'
2
- import {
3
- renameFindOptions,
4
- renameToMongoId,
5
- stringsIntoId
6
- } from '../transformers/index.mjs'
7
- import { outputTransformer } from '../transformers/output_transformer.mjs'
8
-
9
- /**
10
- * @template {import('../../../types.js').MongoDocument} T
11
- * @template {import('../../../types.js').Projection<T>} K
12
- * @param {object} parameter
13
- * @param {() => Promise<Collection<T>>} parameter.getCollection
14
- * @param {import('mongodb').Filter<T>} parameter.query
15
- * @param {import('../../../types.js').FindOptions<T, K>} [parameter.options]
16
- * @param {import('../../../factories/create_mongo_collection.mjs').IcreateCollectionOptions<T>} [parameter.collectionOptions]
17
- * @returns {Promise<T[]>}
18
- */
19
- export async function find({ getCollection, query, options, collectionOptions }) {
20
- query = renameToMongoId(query)
21
- options = renameFindOptions(options)
22
-
23
- stringsIntoId(query)
24
-
25
- const col = await getCollection()
26
-
27
- const docs = await col.find(query, options).toArray()
28
- const fixedDocs = docs.map((doc) => {
29
- return outputTransformer({
30
- document: doc,
31
- collectionOptions,
32
- })
33
- })
34
- return fixedDocs
1
+ import { Collection } from 'mongodb'
2
+ import {
3
+ renameFindOptions,
4
+ renameToMongoId,
5
+ stringsIntoId
6
+ } from '../transformers/index.mjs'
7
+ import { outputTransformer } from '../transformers/output_transformer.mjs'
8
+
9
+ /**
10
+ * @template {import('../../../types.js').MongoDocument} T
11
+ * @template {import('../../../types.js').Projection<T>} K
12
+ * @param {object} parameter
13
+ * @param {() => Promise<Collection<T>>} parameter.getCollection
14
+ * @param {import('mongodb').Filter<T>} parameter.query
15
+ * @param {import('../../../types.js').FindOptions<T, K>} [parameter.options]
16
+ * @param {import('../../../factories/create_mongo_collection.mjs').IcreateCollectionOptions<T>} [parameter.collectionOptions]
17
+ * @returns {Promise<T[]>}
18
+ */
19
+ export async function find({ getCollection, query, options, collectionOptions }) {
20
+ query = renameToMongoId(query)
21
+ options = renameFindOptions(options)
22
+
23
+ query = stringsIntoId(query)
24
+
25
+ const col = await getCollection()
26
+
27
+ const docs = await col.find(query, options).toArray()
28
+ const fixedDocs = docs.map((doc) => {
29
+ return outputTransformer({
30
+ document: doc,
31
+ collectionOptions,
32
+ })
33
+ })
34
+ return fixedDocs
35
35
  }
@@ -1,33 +1,33 @@
1
- import { Collection } from 'mongodb'
2
- import { outputTransformer } from '../transformers/output_transformer.mjs'
3
- import { find } from './find.mjs'
4
-
5
- /**
6
- * @template {import('../../../types.js').MongoDocument} T
7
- * @template {import('../../../types.js').Projection<T> | undefined} K
8
- * @param {object} param
9
- * @param {import('mongodb').Filter<T>} param.query
10
- * @param {import('../../../types.js').FindOptions<T, K>} [param.options]
11
- * @param {() => Promise<Collection<T>>} param.getCollection
12
- * @param {import('../../../factories/create_mongo_collection.mjs').IcreateCollectionOptions<T>} param.collectionOptions
13
- * @returns {Promise<T | null>}
14
- */
15
- export async function findOne({ query, options, collectionOptions, getCollection }) {
16
- const docs = await find({
17
- query,
18
- options: {
19
- limit: 1,
20
- ...options,
21
- },
22
- getCollection,
23
- })
24
- const doc = docs[0]
25
- if (doc == null) {
26
- return null
27
- }
28
-
29
- return outputTransformer({
30
- document: doc,
31
- collectionOptions,
32
- })
1
+ import { Collection } from 'mongodb'
2
+ import { outputTransformer } from '../transformers/output_transformer.mjs'
3
+ import { find } from './find.mjs'
4
+
5
+ /**
6
+ * @template {import('../../../types.js').MongoDocument} T
7
+ * @template {import('../../../types.js').Projection<T> | undefined} K
8
+ * @param {object} param
9
+ * @param {import('mongodb').Filter<T>} param.query
10
+ * @param {import('../../../types.js').FindOptions<T, K>} [param.options]
11
+ * @param {() => Promise<Collection<T>>} param.getCollection
12
+ * @param {import('../../../factories/create_mongo_collection.mjs').IcreateCollectionOptions<T>} param.collectionOptions
13
+ * @returns {Promise<T | null>}
14
+ */
15
+ export async function findOne({ query, options, collectionOptions, getCollection }) {
16
+ const docs = await find({
17
+ query,
18
+ options: {
19
+ limit: 1,
20
+ ...options,
21
+ },
22
+ getCollection,
23
+ })
24
+ const doc = docs[0]
25
+ if (doc == null) {
26
+ return null
27
+ }
28
+
29
+ return outputTransformer({
30
+ document: doc,
31
+ collectionOptions,
32
+ })
33
33
  }
@@ -0,0 +1,23 @@
1
+ import { Collection } from 'mongodb'
2
+ import { renameToMongoId, stringsIntoId } from '../transformers/index.mjs'
3
+ import { outputTransformer } from '../transformers/output_transformer.mjs'
4
+
5
+ /**
6
+ * @template {import('../../../types.js').MongoDocument} T
7
+ * @param {object} param
8
+ * @param {import('mongodb').Filter<T>} param.query
9
+ * @param {import('mongodb').FindOneAndDeleteOptions} [param.options]
10
+ * @param {() => Promise<Collection<T>>} param.getCollection
11
+ * @param {import('../../../factories/create_mongo_collection.mjs').IcreateCollectionOptions<T>} [param.collectionOptions]
12
+ * @returns {Promise<T | null>}
13
+ */
14
+ export async function findOneAndDelete({ query, options, collectionOptions, getCollection }) {
15
+ query = renameToMongoId(query)
16
+ query = stringsIntoId(query)
17
+ const col = await getCollection()
18
+
19
+ const doc = await col.findOneAndDelete(query, options ?? {})
20
+ if (doc == null) return null
21
+
22
+ return outputTransformer({ document: doc, collectionOptions })
23
+ }
@@ -0,0 +1,24 @@
1
+ import { Collection } from 'mongodb'
2
+ import { renameToMongoId, stringsIntoId } from '../transformers/index.mjs'
3
+ import { outputTransformer } from '../transformers/output_transformer.mjs'
4
+
5
+ /**
6
+ * @template {import('../../../types.js').MongoDocument} T
7
+ * @param {object} param
8
+ * @param {import('mongodb').Filter<T>} param.query
9
+ * @param {import('mongodb').WithoutId<T>} param.replacement
10
+ * @param {import('mongodb').FindOneAndReplaceOptions} [param.options]
11
+ * @param {() => Promise<Collection<T>>} param.getCollection
12
+ * @param {import('../../../factories/create_mongo_collection.mjs').IcreateCollectionOptions<T>} [param.collectionOptions]
13
+ * @returns {Promise<T | null>}
14
+ */
15
+ export async function findOneAndReplace({ query, replacement, options, collectionOptions, getCollection }) {
16
+ query = renameToMongoId(query)
17
+ query = stringsIntoId(query)
18
+ const col = await getCollection()
19
+
20
+ const doc = await col.findOneAndReplace(query, replacement, options ?? {})
21
+ if (doc == null) return null
22
+
23
+ return outputTransformer({ document: doc, collectionOptions })
24
+ }
@@ -0,0 +1,40 @@
1
+ import { Collection } from 'mongodb'
2
+ import { renameToMongoId, stringsIntoId } from '../transformers/index.mjs'
3
+ import { outputTransformer } from '../transformers/output_transformer.mjs'
4
+
5
+ /**
6
+ * @template {import('../../../types.js').MongoDocument} T
7
+ * @param {object} param
8
+ * @param {import('mongodb').Filter<T>} param.query
9
+ * @param {import('mongodb').UpdateFilter<T>} param.update
10
+ * @param {import('mongodb').FindOneAndUpdateOptions} [param.options]
11
+ * @param {() => Promise<Collection<T>>} param.getCollection
12
+ * @param {import('../../../factories/create_mongo_collection.mjs').IcreateCollectionOptions<T>} [param.collectionOptions]
13
+ * @returns {Promise<T | null>}
14
+ */
15
+ export async function findOneAndUpdate({ query, update, options, collectionOptions, getCollection }) {
16
+ query = renameToMongoId(query)
17
+ query = stringsIntoId(query)
18
+ update = stringsIntoId(update)
19
+ const col = await getCollection()
20
+
21
+ const { timestamps } = collectionOptions ?? {}
22
+ const { updatedAt } = timestamps ?? { updatedAt: 'generate' }
23
+
24
+ const { $set } = update
25
+ const hasUpdatedAt = $set != null && $set.updatedAt != null
26
+ if (!hasUpdatedAt && updatedAt === 'generate') {
27
+ if ($set != null) {
28
+ // @ts-ignore
29
+ $set.updatedAt = new Date()
30
+ } else {
31
+ // @ts-ignore
32
+ update.$set = { updatedAt: new Date() }
33
+ }
34
+ }
35
+
36
+ const doc = await col.findOneAndUpdate(query, update, options ?? {})
37
+ if (doc == null) return null
38
+
39
+ return outputTransformer({ document: doc, collectionOptions })
40
+ }
@@ -1,10 +1,15 @@
1
- export * from './additional/index.mjs'
2
- export * from './count.mjs'
3
- export * from './delete_many.mjs'
4
- export * from './delete_one.mjs'
5
- export * from './find.mjs'
6
- export * from './find_one.mjs'
7
- export * from './insert_many.mjs'
8
- export * from './insert_one.mjs'
9
- export * from './update_many.mjs'
10
- export * from './update_one.mjs'
1
+ export * from './additional/index.mjs'
2
+ export * from './aggregate.mjs'
3
+ export * from './bulk_write.mjs'
4
+ export * from './count.mjs'
5
+ export * from './delete_many.mjs'
6
+ export * from './delete_one.mjs'
7
+ export * from './find.mjs'
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'
12
+ export * from './insert_many.mjs'
13
+ export * from './insert_one.mjs'
14
+ export * from './update_many.mjs'
15
+ export * from './update_one.mjs'
@@ -1,40 +1,40 @@
1
- import { Collection, ObjectId } from 'mongodb'
2
- import { inputTransformer } from '../transformers/input_transformer.mjs'
3
-
4
- /**
5
- * @template {import('../../../types.js').MongoDocument} T
6
- * @param {object} param
7
- * @param {import('../../../types.js').Optional<T, 'id'>[]} param.docs
8
- * @param {() => Promise<Collection<T>>} param.getCollection
9
- * @param {import('../../../factories/create_mongo_collection.mjs').IcreateCollectionOptions<T>} param.collectionOptions
10
- * @returns {Promise<T[]>}
11
- */
12
- export async function insertMany({ docs, collectionOptions, getCollection }) {
13
- if (docs.length == 0) return []
14
-
15
- // We need to clone the array to prevent the function from changing the original input object.
16
- // This is necessary since we change the objects inside the array to rename the "id" to "_id".
17
- docs = [...docs]
18
-
19
- for (let i = 0; i < docs.length; i++) {
20
- docs[i] = inputTransformer({
21
- document: docs[i],
22
- collectionOptions: collectionOptions
23
- })
24
- }
25
- const col = await getCollection()
26
- const result = await col.insertMany(
27
- // @ts-ignore
28
- docs
29
- )
30
- let { insertedIds } = result
31
- const indexes = Object.keys(insertedIds)
32
- for (const index of indexes) {
33
- const id = insertedIds[index]
34
- if (id instanceof ObjectId) {
35
- docs[index].id = id.toHexString()
36
- }
37
- }
38
- // @ts-ignore
39
- return docs
1
+ import { Collection, ObjectId } from 'mongodb'
2
+ import { inputTransformer } from '../transformers/input_transformer.mjs'
3
+
4
+ /**
5
+ * @template {import('../../../types.js').MongoDocument} T
6
+ * @param {object} param
7
+ * @param {import('../../../types.js').Optional<T, 'id'>[]} param.docs
8
+ * @param {() => Promise<Collection<T>>} param.getCollection
9
+ * @param {import('../../../factories/create_mongo_collection.mjs').IcreateCollectionOptions<T>} param.collectionOptions
10
+ * @returns {Promise<T[]>}
11
+ */
12
+ export async function insertMany({ docs, collectionOptions, getCollection }) {
13
+ if (docs.length == 0) return []
14
+
15
+ // We need to clone the array to prevent the function from changing the original input object.
16
+ // This is necessary since we change the objects inside the array to rename the "id" to "_id".
17
+ docs = [...docs]
18
+
19
+ for (let i = 0; i < docs.length; i++) {
20
+ docs[i] = inputTransformer({
21
+ document: docs[i],
22
+ collectionOptions: collectionOptions
23
+ })
24
+ }
25
+ const col = await getCollection()
26
+ const result = await col.insertMany(
27
+ // @ts-ignore
28
+ docs
29
+ )
30
+ let { insertedIds } = result
31
+ const indexes = Object.keys(insertedIds)
32
+ for (const index of indexes) {
33
+ const id = insertedIds[index]
34
+ if (id instanceof ObjectId) {
35
+ docs[index].id = id.toHexString()
36
+ }
37
+ }
38
+ // @ts-ignore
39
+ return docs
40
40
  }
@@ -1,33 +1,33 @@
1
- import { Collection, ObjectId } from 'mongodb'
2
- import { inputTransformer } from '../transformers/input_transformer.mjs'
3
-
4
- /**
5
- * @template {import('../../../types.js').MongoDocument} T
6
- * @param {object} param
7
- * @param {import('../../../types.js').Optional<T, 'id'>} param.doc
8
- * @param {() => Promise<Collection<T>>} param.getCollection
9
- * @param {import('../../../factories/create_mongo_collection.mjs').IcreateCollectionOptions<T>} [param.collectionOptions]
10
- * @returns {Promise<T>}
11
- */
12
- export async function insertOne({ doc, collectionOptions, getCollection }) {
13
- doc = inputTransformer({
14
- document: doc,
15
- collectionOptions
16
- })
17
-
18
- const col = await getCollection()
19
- const result = await col.insertOne(
20
- // @ts-ignore
21
- doc
22
- )
23
- let id = result.insertedId
24
- if (id instanceof ObjectId) {
25
- // @ts-ignore
26
- id = id.toHexString()
27
- }
28
-
29
- delete doc._id
30
-
31
- // @ts-ignore
32
- return { id, ...doc }
1
+ import { Collection, ObjectId } from 'mongodb'
2
+ import { inputTransformer } from '../transformers/input_transformer.mjs'
3
+
4
+ /**
5
+ * @template {import('../../../types.js').MongoDocument} T
6
+ * @param {object} param
7
+ * @param {import('../../../types.js').Optional<T, 'id'>} param.doc
8
+ * @param {() => Promise<Collection<T>>} param.getCollection
9
+ * @param {import('../../../factories/create_mongo_collection.mjs').IcreateCollectionOptions<T>} [param.collectionOptions]
10
+ * @returns {Promise<T>}
11
+ */
12
+ export async function insertOne({ doc, collectionOptions, getCollection }) {
13
+ doc = inputTransformer({
14
+ document: doc,
15
+ collectionOptions
16
+ })
17
+
18
+ const col = await getCollection()
19
+ const result = await col.insertOne(
20
+ // @ts-ignore
21
+ doc
22
+ )
23
+ let id = result.insertedId
24
+ if (id instanceof ObjectId) {
25
+ // @ts-ignore
26
+ id = id.toHexString()
27
+ }
28
+
29
+ delete doc._id
30
+
31
+ // @ts-ignore
32
+ return { id, ...doc }
33
33
  }
@@ -1,40 +1,40 @@
1
- import { Collection } from 'mongodb'
2
- import { renameToMongoId, stringsIntoId } from '../transformers/index.mjs'
3
-
4
- /**
5
- * @template {import('../../../types.js').MongoDocument} T
6
- * @param {object} param
7
- * @param {import('mongodb').Filter<T>} param.query
8
- * @param {import('mongodb').UpdateFilter<T>} param.update
9
- * @param {import('mongodb').UpdateOptions} [param.options]
10
- * @param {() => Promise<Collection<T>>} param.getCollection
11
- * @param {import('../../../factories/create_mongo_collection.mjs').IcreateCollectionOptions<T>} [param.collectionOptions]
12
- */
13
- export async function updateMany({ query, update, options, collectionOptions, getCollection }) {
14
- query = renameToMongoId(query)
15
- stringsIntoId(query)
16
- stringsIntoId(update)
17
-
18
- const { timestamps } = collectionOptions ?? {}
19
- const { updatedAt } = timestamps ?? {
20
- updatedAt: 'generate'
21
- }
22
-
23
- const { $set } = update
24
- const hasUpdatedAt = $set != null && $set.updatedAt != null
25
- if (!hasUpdatedAt && updatedAt === 'generate') {
26
- if ($set != null) {
27
- // @ts-ignore
28
- $set.updatedAt = new Date()
29
- } else {
30
- // @ts-ignore
31
- update.$set = {
32
- updatedAt: new Date()
33
- }
34
- }
35
- }
36
-
37
- const col = await getCollection()
38
- const r = await col.updateMany(query, update, options)
39
- return r
1
+ import { Collection } from 'mongodb'
2
+ import { renameToMongoId, stringsIntoId } from '../transformers/index.mjs'
3
+
4
+ /**
5
+ * @template {import('../../../types.js').MongoDocument} T
6
+ * @param {object} param
7
+ * @param {import('mongodb').Filter<T>} param.query
8
+ * @param {import('mongodb').UpdateFilter<T>} param.update
9
+ * @param {import('mongodb').UpdateOptions} [param.options]
10
+ * @param {() => Promise<Collection<T>>} param.getCollection
11
+ * @param {import('../../../factories/create_mongo_collection.mjs').IcreateCollectionOptions<T>} [param.collectionOptions]
12
+ */
13
+ export async function updateMany({ query, update, options, collectionOptions, getCollection }) {
14
+ query = renameToMongoId(query)
15
+ query = stringsIntoId(query)
16
+ update = stringsIntoId(update)
17
+
18
+ const { timestamps } = collectionOptions ?? {}
19
+ const { updatedAt } = timestamps ?? {
20
+ updatedAt: 'generate'
21
+ }
22
+
23
+ const { $set } = update
24
+ const hasUpdatedAt = $set != null && $set.updatedAt != null
25
+ if (!hasUpdatedAt && updatedAt === 'generate') {
26
+ if ($set != null) {
27
+ // @ts-ignore
28
+ $set.updatedAt = new Date()
29
+ } else {
30
+ // @ts-ignore
31
+ update.$set = {
32
+ updatedAt: new Date()
33
+ }
34
+ }
35
+ }
36
+
37
+ const col = await getCollection()
38
+ const r = await col.updateMany(query, update, options)
39
+ return r
40
40
  }