assai 2.1.1 → 2.1.3

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.
@@ -2,5 +2,8 @@
2
2
  * ```
3
3
  * "_id" -> "id"
4
4
  * ```
5
+ * Recursively renames all `_id` keys to `id` in objects and arrays.
6
+ * Does not mutate the original input.
7
+ * @param {*} obj
5
8
  */
6
9
  export function renameToDevId(obj: any): any;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "assai",
3
- "version": "2.1.1",
3
+ "version": "2.1.3",
4
4
  "repository": {
5
5
  "url": "https://github.com/TimeLord2010/assai",
6
6
  "type": "git"
@@ -1,5 +1,6 @@
1
1
  import { Collection, ObjectId } from 'mongodb'
2
2
  import { inputTransformer } from '../transformers/input_transformer.mjs'
3
+ import { outputTransformer } from '../transformers/output_transformer.mjs'
3
4
 
4
5
  /**
5
6
  * @template {import('../../../types.js').MongoDocument} T
@@ -30,11 +31,16 @@ export async function insertMany({ docs, collectionOptions, getCollection }) {
30
31
  let { insertedIds } = result
31
32
  const indexes = Object.keys(insertedIds)
32
33
  for (const index of indexes) {
34
+ // @ts-ignore
33
35
  const id = insertedIds[index]
34
36
  if (id instanceof ObjectId) {
37
+ // @ts-ignore
35
38
  docs[index].id = id.toHexString()
36
39
  }
37
40
  }
38
- // @ts-ignore
39
- return docs
41
+ return docs.map(doc => outputTransformer({
42
+ // @ts-ignore
43
+ document: doc,
44
+ collectionOptions
45
+ }))
40
46
  }
@@ -1,5 +1,6 @@
1
1
  import { Collection, ObjectId } from 'mongodb'
2
2
  import { inputTransformer } from '../transformers/input_transformer.mjs'
3
+ import { outputTransformer } from '../transformers/output_transformer.mjs'
3
4
 
4
5
  /**
5
6
  * @template {import('../../../types.js').MongoDocument} T
@@ -26,8 +27,12 @@ export async function insertOne({ doc, collectionOptions, getCollection }) {
26
27
  id = id.toHexString()
27
28
  }
28
29
 
29
- delete doc._id
30
-
31
30
  // @ts-ignore
32
- return { id, ...doc }
31
+ doc.id = id
32
+
33
+ return outputTransformer({
34
+ // @ts-ignore
35
+ document: doc,
36
+ collectionOptions,
37
+ })
33
38
  }
@@ -2,14 +2,35 @@
2
2
  * ```
3
3
  * "_id" -> "id"
4
4
  * ```
5
+ * Recursively renames all `_id` keys to `id` in objects and arrays.
6
+ * Does not mutate the original input.
7
+ * @param {*} obj
5
8
  */
6
9
  export function renameToDevId(obj) {
7
- if (!obj) return obj
8
- let { _id, ...rest } = obj
9
- if (_id === undefined) return obj
10
- if (_id === null) return rest
11
- return {
12
- id: _id,
13
- ...rest
10
+ if (obj == null) return obj
11
+ if (typeof obj !== 'object') return obj
12
+ if (obj instanceof Date) return obj
13
+
14
+ if (Array.isArray(obj)) {
15
+ let hasChanges = false
16
+ const transformed = new Array(obj.length)
17
+ for (let i = 0; i < obj.length; i++) {
18
+ const item = obj[i]
19
+ const transformedItem = renameToDevId(item)
20
+ transformed[i] = transformedItem
21
+ if (transformedItem !== item) hasChanges = true
22
+ }
23
+ return hasChanges ? transformed : obj
14
24
  }
25
+
26
+ let hasChanges = false
27
+ /** @type {Record<string, any>} */
28
+ const transformed = {}
29
+ for (const [key, value] of Object.entries(obj)) {
30
+ const newKey = key === '_id' ? 'id' : key
31
+ const newValue = renameToDevId(value)
32
+ transformed[newKey] = newValue
33
+ if (newKey !== key || newValue !== value) hasChanges = true
34
+ }
35
+ return hasChanges ? transformed : obj
15
36
  }
@@ -24,6 +24,7 @@ export function renameToMongoId(obj) {
24
24
  }
25
25
 
26
26
  let hasChanges = false
27
+ /** @type {Record<string, any>} */
27
28
  const transformed = {}
28
29
  for (const [key, value] of Object.entries(obj)) {
29
30
  const newKey = key === 'id' ? '_id' : key