concept IndexedDB in category electron

appears as: IndexedDB
Electron in Action

This is an excerpt from Manning's book Electron in Action.

  • IndexedDB is a popular browser-based option for storing user data on the client. It is a NoSQL database provided by Chromium.
  • 12.2.3. Writing data to IndexedDB

    Getting all of the items from IndexedDB isn’t particularly helpful if there aren’t any items in the store to begin with. Let’s implement the ability to add an item to the database. This procedure is similar to reading from the database with a few important distinctions. When reading from the database, the transaction is complete when we finish reading. Modifying the database is a little more complicated. As a result, we need to be more explicit about when a transaction has completed. We also need to let IndexedDB know that we intend to write to the database. After that, it’s a matter of adding the item to the database and then calling the transaction complete.

    Listing 12.17. Adding an item to IndexedDB: ./app/database.js
    export default {
      getAll() { ... },
      add(item) {
        return database.then(db => {
          const tx = db.transaction('items', 'readwrite');     #1
          tx.objectStore('items').add(item);                   #2
          return tx.complete;                                  #3
        });
      },
      update(item) { },
      markAllAsUnpacked() { },
      delete(item) { },
      deleteUnpackedItems() { }
    };