jakearchibald / idb

IndexedDB, but with promises
https://www.npmjs.com/package/idb
ISC License
6.34k stars 357 forks source link

need method to bulk insert #285

Closed DAmrGharieb closed 1 year ago

DAmrGharieb commented 1 year ago

hi team good day is there is any solution if i have an array and i want to bulk insert them one time without looping

thanks

jakearchibald commented 1 year ago

Why is "no loops" a requirement?

DAmrGharieb commented 1 year ago

For loop take alot of time i think i miss something

jakearchibald commented 1 year ago

Nah, the type of loop is very rarely the performance problem, it's almost always what's inside the loop that causes performance issues.

// Let's say you had a database like this:
const db = await openDB('Articles', 1, {
  upgrade(db) {
    // Create a store of objects
    const store = db.createObjectStore('articles', {
      // The 'id' property of the object will be the key.
      keyPath: 'id',
    });
  },
});

// And you had an array of articles:
const articles = [
  // …articles…
];

// I'd add them all like this:
const tx = db.transaction('articles', 'readwrite');
await Promise.all(articles.map((article) => tx.store.add(article)));
await tx.done;