IndexedDB is a client-side database API available in modern browsers and Electron. During development and testing of a web or desktop app which uses IndexedDB, it can be helpful to save, load, or clear the contents of an IndexedDB database — this package provides that capability.
You can use indexeddb-export-import in a Node.js environment imported as a module (e.g. for use with an Electron app). You may also use it in a browser environment by simply including via a <script> tag.
You will need an open IDBDatabase connection.
All three functions support both a Promise/async-await style (omit the callback) and a traditional callback style.
The following examples export a database, clear all object stores, then re-import the database. They use Dexie.js to initiate the database, but this is not required.
const Dexie = require('dexie');
const IDBExportImport = require('indexeddb-export-import');
const db = new Dexie('MyDB');
db.version(1).stores({
things: 'id++, thing_name, thing_description',
});
async function run() {
await db.open();
const idbDatabase = db.backendDB(); // get native IDBDatabase object from Dexie wrapper
try {
const jsonString = await IDBExportImport.exportToJsonString(idbDatabase);
console.log('Exported as JSON: ' + jsonString);
await IDBExportImport.clearDatabase(idbDatabase);
console.log('Cleared the database');
await IDBExportImport.importFromJsonString(idbDatabase, jsonString);
console.log('Imported data successfully');
} catch (err) {
console.error(err);
}
}
run();const Dexie = require('dexie');
const IDBExportImport = require('indexeddb-export-import');
const db = new Dexie('MyDB');
db.version(1).stores({
things: 'id++, thing_name, thing_description',
});
db.open().then(function() {
const idbDatabase = db.backendDB(); // get native IDBDatabase object from Dexie wrapper
IDBExportImport.exportToJsonString(idbDatabase, function(err, jsonString) {
if (err) {
console.error(err);
} else {
console.log('Exported as JSON: ' + jsonString);
IDBExportImport.clearDatabase(idbDatabase, function(err) {
if (!err) {
IDBExportImport.importFromJsonString(idbDatabase, jsonString, function(err) {
if (!err) {
console.log('Imported data successfully');
}
});
}
});
}
});
}).catch(function(e) {
console.error('Could not connect. ' + e);
});Export all data from an IndexedDB database.
| Param | Type | Description |
|---|---|---|
| idbDatabase | IDBDatabase |
open database connection |
| cb | function |
optional callback (error, jsonString); omit to receive a Promise<string> |
Import data from JSON into an IndexedDB database. This does not delete any existing data from the database, so keys could clash.
Only object stores that already exist will be imported.
| Param | Type | Description |
|---|---|---|
| idbDatabase | IDBDatabase |
open database connection |
| jsonString | string |
data to import, one key per object store |
| cb | function |
optional callback (error); omit to receive a Promise<void> |
Clears a database of all data. The object stores themselves are preserved.
| Param | Type | Description |
|---|---|---|
| idbDatabase | IDBDatabase |
open database connection |
| cb | function |
optional callback (error); omit to receive a Promise<void> |
$ npm install indexeddb-export-import
Note for contributors: running the test suite requires Node ≥ 20. The library itself has no runtime dependencies and works on Node ≥ 12.
MIT
