Repository: maxnowack/signaldb
Pull request #923 (2024-09-13 11:11:59)

feat: add insertMany to Collection (leveraging on batch method)
Hello Max,

I added the small method and did a small test about it. I didn't add any new event in order to avoid polluting the bus.

What do you think?

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/maxnowack/signaldb/pull/923?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Max+Nowack) Report
Attention: Patch coverage is `85.71429%` with `1 line` in your changes missing coverage. Please review.
> Project coverage is 99.92%. Comparing base [(`5d13912`)](https://app.codecov.io/gh/maxnowack/signaldb/commit/5d13912183bbaa61518435a73abb1db81aa68471?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Max+Nowack) to head [(`37ef8e9`)](https://app.codecov.io/gh/maxnowack/signaldb/commit/37ef8e92769c9f825e296df6529f4906aebe2b40?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Max+Nowack).
> Report is 4 commits behind head on main.

| [Files with missing lines](https://app.codecov.io/gh/maxnowack/signaldb/pull/923?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Max+Nowack) | Patch % | Lines |
|---|---|---|
| [packages/signaldb/src/Collection/index.ts](https://app.codecov.io/gh/maxnowack/signaldb/pull/923?src=pr&el=tree&filepath=packages%2Fsignaldb%2Fsrc%2FCollection%2Findex.ts&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Max+Nowack#diff-cGFja2FnZXMvc2lnbmFsZGIvc3JjL0NvbGxlY3Rpb24vaW5kZXgudHM=) | 85.71% | [1 Missing :warning: ](https://app.codecov.io/gh/maxnowack/signaldb/pull/923?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Max+Nowack) |

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff             @@
##              main     #923      +/-   ##
===========================================
- Coverage   100.00%   99.92%   -0.08%     
===========================================
  Files           54       54              
  Lines         1380     1387       +7     
  Branches       315      317       +2     
===========================================
+ Hits          1380     1386       +6     
- Misses           0        1       +1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/maxnowack/signaldb/pull/923?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Max+Nowack).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Max+Nowack).

maxnowack: Looks good, thanks! I've made some small improvements, now wait for the tests to pass and merge it afterwards 🙂 

Patch:
diff --git a/docs/collections/index.md b/docs/collections/index.md
index 7d68a7a..4ad0104 100644
--- a/docs/collections/index.md
+++ b/docs/collections/index.md
@@ -48,6 +48,9 @@ Behaves the same like `.find()` but doesn't return a cursor. Instead it will dir
 Inserts an item into the collection and returns the ID of the newly inserted item.
 Also check out the [data manipulation section](/data-manipulation/).
 
+### `insertMany(items: Array<Omit<T, 'id'> & Partial<Pick<T, 'id'>>>)`
+Inserts multiple items into the collection and returns the IDs of the newly inserted items.
+
 Parameters
 * `item`: The item to be inserted into the collection.
 
diff --git a/packages/signaldb/src/Collection/index.ts b/packages/signaldb/src/Collection/index.ts
index 80c7366..fc0b4e9 100644
--- a/packages/signaldb/src/Collection/index.ts
+++ b/packages/signaldb/src/Collection/index.ts
@@ -470,6 +470,21 @@ export default class Collection<T extends BaseItem<I> = BaseItem, I = any, U = T
     return newItem.id
   }
 
+  public insertMany(items: Array<Omit<T, 'id'> & Partial<Pick<T, 'id'>>>) {
+    if (!items) throw new Error('Invalid items')
+    if (items.length === 0) {
+      return []
+    }
+
+    const ids: I[] = []
+    this.batch(() => {
+      items.forEach((item) => {
+        ids.push(this.insert(item))
+      })
+    })
+    return ids
+  }
+
   public updateOne(selector: Selector<T>, modifier: Modifier<T>) {
     if (!selector) throw new Error('Invalid selector')
     if (!modifier) throw new Error('Invalid modifier')


Test patch:
diff --git a/packages/signaldb/__tests__/Collection.spec.ts b/packages/signaldb/__tests__/Collection.spec.ts
index b3a43dc..1002258 100644
--- a/packages/signaldb/__tests__/Collection.spec.ts
+++ b/packages/signaldb/__tests__/Collection.spec.ts
@@ -129,6 +129,25 @@ describe('Collection', () => {
     })
   })
 
+  describe('insertMany', () => {
+    it('should insert multiple items into the collection', () => {
+      const items = [
+        { id: '1', name: 'John' },
+        { id: '2', name: 'Bob' },
+        { id: '3', name: 'Jack' },
+      ]
+
+      const ids = collection.insertMany(items)
+
+      expect(ids).toEqual(['1', '2', '3'])
+      expect(collection.find({}).fetch()).toEqual(items)
+    })
+
+    it('should not fail if empty array was passed', () => {
+      expect(collection.insertMany([])).toEqual([])
+    })
+  })
+
   describe('updateOne', () => {
     it('should update a single item that matches the selector', () => {
       collection.insert({ id: '1', name: 'John' })