Giter VIP home page Giter VIP logo

indexeddb-export-import's Introduction

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

indexeddb-export-import's People

Contributors

alexandremsouza1 avatar dmarcelino avatar fdt2k avatar greenkeeper[bot] avatar polarisation avatar robbiethewagner avatar rwv avatar shwarcu avatar trancephorm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

indexeddb-export-import's Issues

Failed to execute 'transaction' on 'IDBDatabase': The storeNames parameter was empty.

When importing a backup into a browser that didn't have an existing database I get the following error:

Uncaught DOMException: Failed to execute 'transaction' on 'IDBDatabase': The storeNames parameter was empty.

This is due to idbDatabase.objectStoreNames being an empty list in

  const transaction = idbDatabase.transaction(
      idbDatabase.objectStoreNames,
      'readwrite'
  );

Any help would be appreciated!

re-run npm install FAILS

if this module is already installed in the project, running the npm insatll fails with the message

`npm install
npm ERR! code EISGIT
npm ERR! path **\node_modules\indexeddb-export-import
npm ERR! git **\node_modules\indexeddb-export-import: Appears to be a git
repo or submodule.
npm ERR! git **\node_modules\indexeddb-export-import
npm ERR! git Refusing to remove it. Update manually,
npm ERR! git or move it out of the way first.

npm ERR! A complete log of this run can be found in:
npm ERR! **\AppData\Roaming\npm-cache_logs\2020-06-01T13_10_57_546Z-debug.log`

This error is caused because tar.gz in version 2.0.2 has a directory **. git ** inside, the solution is to remove and resend. The package **. zip ** is OK

UglifyJs: ERROR - Unexpected token: name (count)

Hi,

When trying to bundling my project with webpack and UglifyJs I get the following error:

ERROR in static/js/vendor.a27e424093e8687946a1.js from UglifyJs
Unexpected token: name (count) [./node_modules/indexeddb-export-import/index.js:50,0][static/js/vendor.a27e424093e8687946a1.js:51531,6]
...
ERROR in static/js/vendor.c242818493dd22375921.js from UglifyJs
Unexpected token: name (count) [./node_modules/indexeddb-export-import/index.js:76,0][static/js/vendor.c242818493dd22375921.js:51557,5]

Question: is this supposed to export/import indexes?

This may be a very naive question but index creation is very slow for me and I have a large (400k+) table with a few indexes. Following the examples I am still getting the browser creating indexes internally (which use a lot of CPU and take several minutes, so it is very visible), so it appears this project doesn't do that. I would have been very surprised if it could do this but if you don't ask you will never know!

Hi, is this possible to use this project to backup data inside indexedDB in Electron APP?

We are trying to converting our Web App into an Electron APP.
We use DexieJS in our Web App, so we prefer to use it as well in our Electron APP.

However, we are worried about the data safety, since there are maybe some scenarios, the data inside indexedDB MIGHT be deleted. Such as reinstall the App, auto update the App etc. We are not sure about it. But we can not risk it.

I found this project while searching around the internet. I am wondering maybe this could be used to backup the indexedDB.

Is this possible?
Thanks.

Sets exported as empty objects

I'm saving sets to my database, eg new Set() and while they show as a Set in the indexeddb inspector, they appear as empty objects {} in the exported JSON.

Something like this in the Chrome inspector:

{
  items: Set(12)
}

becomes:

{
  "items": {}
}

Example needs to be updated

Example provided uses exportToJson but the method inside the package is exportToJsonString
applicable for importFromJson as well

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.