IndexedDB Export/Import Utility
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
-
TypeError: Cannot read properties of undefined (reading 'length') OR Database doesn't have object store: 'storeName'
cause Attempting to import JSON data where some keys in the JSON do not correspond to existing object store names in the IndexedDB database, or vice versa, especially in older versions.fixEnsure the JSON structure exactly matches the object store names in your `IDBDatabase`. Upgrade to `indexeddb-export-import@2.1.5` or later to mitigate issues with missing/extra keys. -
ReferenceError: require is not defined (in browser)
cause Attempting to use CommonJS `require` syntax in a browser environment without a module bundler like Webpack or Rollup.fixFor browser usage without a bundler, include the library via a `<script>` tag. If using a bundler, configure it to handle CommonJS modules or use ES module `import` syntax if your project supports it.
Warnings
- breaking Version 2.0.0 introduced a requirement for ES6, meaning Node.js 8+ or a modern browser. If you need to support older environments (like ES5), you must use a transpiler or stick with the v1.x series of this package.
- gotcha Prior to v2.1.3 and v2.1.5, importing JSON data could lead to bugs if the import JSON structure had keys that did not exactly match the existing object store names in the IndexedDB database. This could result in data not being imported correctly or errors during the process.
- gotcha The `importFromJsonString` function does not delete any existing data in the database before importing. If you import data into a non-empty database, keys could clash, potentially leading to errors or unexpected data merges.
- breaking Version 2.0.0 removed all external dependencies, reducing package size and simplifying the dependency tree. While beneficial, this change means any previously implicitly relied-upon transitive dependencies are no longer present.
Install
-
npm install indexeddb-export-import -
yarn add indexeddb-export-import -
pnpm add indexeddb-export-import
Imports
- IDBExportImport
const IDBExportImport = require('indexeddb-export-import');import * as IDBExportImport from 'indexeddb-export-import';
- exportToJsonString
const { exportToJsonString } = require('indexeddb-export-import');import { exportToJsonString } from 'indexeddb-export-import'; - importFromJsonString
import { importFromJsonString } from 'indexeddb-export-import';
Quickstart
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);
});