Importing and exporting data in IndexedDB

When working with IndexedDB, you may need to import or export data from the database to perform tasks like backing up data, synchronizing between devices, or migrating data to a different system. In this blog post, we will explore various techniques for importing and exporting data in IndexedDB.

Exporting Data

To export data from IndexedDB, you first need to retrieve the desired data from the database. This can be done using the openCursor() or getAll() methods on the object store.

const exportData = (dbName, objectStoreName) => {
  return new Promise((resolve, reject) => {
    const dbRequest = indexedDB.open(dbName);

    dbRequest.onsuccess = (event) => {
      const db = event.target.result;
      const transaction = db.transaction(objectStoreName, 'readonly');
      const objectStore = transaction.objectStore(objectStoreName);
      const data = [];

      objectStore.openCursor().onsuccess = (event) => {
        const cursor = event.target.result;

        if (cursor) {
          data.push(cursor.value);
          cursor.continue();
        } else {
          resolve(data);
        }
      };
    };

    dbRequest.onerror = (event) => {
      reject(event.target.error);
    };
  });
};

The exportData() function takes the name of the database (dbName) and the name of the object store (objectStoreName) as parameters. It opens a transaction on the object store in ‘readonly’ mode and retrieves the data by iterating over the cursor.

Once the data is retrieved, you can format it in a suitable format like JSON or CSV and provide it as a download link to the user.

Importing Data

Importing data into IndexedDB involves reading the data from an external source, such as a file or an API, and inserting it into the database.

const importData = (dbName, objectStoreName, data) => {
  return new Promise((resolve, reject) => {
    const dbRequest = indexedDB.open(dbName);

    dbRequest.onsuccess = (event) => {
      const db = event.target.result;
      const transaction = db.transaction(objectStoreName, 'readwrite');
      const objectStore = transaction.objectStore(objectStoreName);

      data.forEach((item) => {
        objectStore.add(item);
      });

      transaction.oncomplete = () => {
        resolve();
      };

      transaction.onerror = (event) => {
        reject(event.target.error);
      };
    };

    dbRequest.onerror = (event) => {
      reject(event.target.error);
    };
  });
};

The importData() function takes the name of the database (dbName), the name of the object store (objectStoreName), and the data to be imported (data) as parameters. It opens a transaction on the object store in ‘readwrite’ mode and adds each item from the data array to the object store.

You can invoke the importData() function after reading the data from an external source and pass the retrieved data to it.

Conclusion

Importing and exporting data in IndexedDB provides flexibility in managing and manipulating the data stored in the database. With the ability to export data, you can create backups or migrate data to different systems. Importing data enables you to easily insert data from external sources into your database.

By taking advantage of these techniques, you can optimize your web applications that utilize IndexedDB and ensure efficient data management.

#Tech #IndexedDB