IndexedDB Export/Import Utility

2.1.5 · active · verified Wed Apr 22

indexeddb-export-import is a JavaScript utility for serializing and deserializing IndexedDB databases to and from JSON format. It provides functions to export an entire database to a JSON string, import data from a JSON string into an existing database, and clear all data from a database. The package is currently at version 2.1.5 and maintains an active release cadence, primarily focusing on bug fixes and minor improvements. A key differentiator is its minimal footprint, having zero runtime dependencies since version 2.0.0, making it lightweight for both Node.js (especially Electron apps) and browser environments, where it can be included directly via a `<script>` tag. It requires ES6 (Node.js 8+ or modern browsers) since its v2 release, offering a clean API for database backup, restoration, and development/testing workflows.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to export an IndexedDB database to a JSON string, clear all its object stores, and then re-import the data from the previously exported JSON string. It uses Dexie.js to manage the IndexedDB connection for convenience, which is a common pattern for obtaining an `IDBDatabase` instance.

import Dexie from 'dexie';
import { exportToJsonString, clearDatabase, importFromJsonString } from 'indexeddb-export-import';

const db = new Dexie('MyDB');
db.version(1).stores({
  things: 'id++, thing_name, thing_description'
});

db.open().then(function() {
  // Dexie.js provides a convenient way to get the native IDBDatabase object
  const idbDatabase = db.backendDB(); 

  // Export to JSON, clear database, and then import from JSON
  exportToJsonString(idbDatabase, function(err, jsonString) {
    if (err) {
      console.error('Error exporting:', err);
      return;
    }
    console.log('Exported as JSON:', jsonString.substring(0, 100) + '...'); // Log a snippet
    
    clearDatabase(idbDatabase, function(err) {
      if (err) {
        console.error('Error clearing database:', err);
        return;
      }
      console.log('Database cleared successfully.');
      
      importFromJsonString(idbDatabase, jsonString, function(err) {
        if (err) {
          console.error('Error importing:', err);
          return;
        }
        console.log('Data imported successfully.');
        // You can now verify the data is back in the DB
        db.table('things').count().then(count => {
          console.log(`Things in DB after import: ${count}`);
        });
      });
    });
  });
}).catch(function(e) {
  console.error('Could not connect to IndexedDB: ' + e);
});

view raw JSON →