Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

indexeddb-export-import

Polarisation19.3kMIT2.1.5

Export/import an IndexedDB database to/from JSON

IndexedDB, JSON, import, export, serialize, deserialize, backup, restore

readme

indexeddb-export-import - JSON export/import for IndexedDB

IndexedDB is a client-side database API available in modern browsers and https://electron.atom.io/. During development and testing of an web / 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 (eg. for use with an Electron app). You may also use it in a browser environment by simply including via a <script> tag.

Build Status NPM

Usage

You will need an open IDBDatabase connection.

The following 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.

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

  // export to JSON, clear database, and import from JSON
  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) { // cleared data successfully
          IDBExportImport.importFromJsonString(idbDatabase, jsonString, function(err) {
            if (!err) {
              console.log('Imported data successfully');
            }
          });
        }
      });
    }
  });
}).catch(function(e) {
  console.error('Could not connect. ' + e);
});

API

exportToJsonString(idbDatabase, cb)

Export all data from an IndexedDB database

Param Type Description
idbDatabase IDBDatabase
cb function callback with signature (error, jsonString)

importFromJsonString(idbDatabase, jsonString, cb)

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
idbDatabase IDBDatabase
jsonString string data to import, one key per object store
cb function callback with signature (error), where error is null on success

clearDatabase(idbDatabase, cb)

Clears a database of all data

Param Type Description
idbDatabase IDBDatabase
cb function callback with signature (error), where error is null on success

Installation

$ npm install indexeddb-export-import

License

MIT

changelog

Changelog

v2.1.5

  • Fix bug when import JSON is missing keys that are present as object store names (#25).

v2.1.4

Bad release - do not use.

v2.1.3

  • Fix bug when import JSON contains keys that are not present as object store names (#21).
  • Updated tests to run on Node.js v12 (current maintenance LTS).

v2.1.2

  • Fix possible issue if objectStoreNames is undefined.

v2.1.1

  • Handle case where no object stores exist

v2.1.0

  • Handle cases where there are empty stores or duplicated store names (#16)

v2.0.3

  • Fix issue (#14) with npm package
  • Update dev dependencies

v2.0.2

  • Update dev dependencies (fixes potential security vulnerabilities)

v2.0.1

  • Including extra files in NPM distribution as excluding them impacts quality score
  • README improvements

v2.0.0

  • Now has no dependencies
  • Reduced package distribution size by 30%
  • Cleaner code
  • Now works in the browser when index.js is imported via <script>
  • Now requires ES6 (Node.js 8+ or a modern browser). If you need to support ES5, use a transpiler or stick with v1.x.