Giter Site home page Giter Site logo

fdt2k / indexeddb-export-import Goto Github PK

View Code? Open in Web Editor NEW

This project forked from polarisation/indexeddb-export-import

0.0 1.0 0.0 340 KB

Export/import an IndexedDB database to/from JSON. Can be used to backup and restore.

License: MIT License

JavaScript 100.00%

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

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

Watchers

 avatar

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.