During development and testing it may be useful to be able to save and load the contents of an IndexedDB database.
I wrote this as a Node.js module for use with a desktop https://electron.atom.io/ app - which has access to both the IndexedDB API and Node.js. But there are minimal dependencies so it should be easy to reuse the functions in a browser environment where Node.js is not available.
You will need an open IDBDatabase connection.
The follwing example exports a database, clears all object stores, then re-imports the database. It uses Dexie.js to initiate the database, but this is not required.
var Dexie = require("Dexie");
var IDBExportImport = require("indexeddb-export-import")
var db = new Dexie("MyDB");
db.version(1).stores({
things : "id++, thing_name, thing_description",
});
db.open().then(function() {
var idb_db = db.backendDB(); // get native IDBDatabase object from Dexie wrapper
// export to JSON, clear database, and import from JSON
IDBExportImport.exportToJsonString(idb_db, function(err, jsonString) {
if(err)
console.error(err);
else {
console.log("Exported as JSON: " + jsonString);
IDBExportImport.clearDatabase(idb_db, function(err) {
if(!err) // cleared data successfully
IDBExportImport.importFromJsonString(idb_db, 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 |
---|---|---|
idb_db | IDBDatabase |
|
cb | function |
callback with signature (error, jsonString) |
Import data from JSON into an IndexedDB database. This does not delete any existing data from the database, so keys could clash
Param | Type | Description |
---|---|---|
idb_db | IDBDatabase |
|
jsonString | string |
data to import, one key per object store |
cb | function |
callback with signature (error), where error is null on success |
Clears a database of all data
Param | Type | Description |
---|---|---|
idb_db | IDBDatabase |
|
cb | function |
callback with signature (error), where error is null on success |
$ npm install indexeddb-export-import
MIT