Exporting and importing IndexedDB data

20,293

Solution 1

There is nothing like this available in the pure IndexedDB spec, however, it's possible to write your own methods that will accomplish this.

The basic steps to import data are to

  1. open an IndexedDB database
  2. create an object store
  3. add indexes
  4. loop through your objects and add them one by one (via an add or put operation)

For exporting an object store you can:

  1. open up a cursor with zero as the left bound and at each tick
  2. add an onsuccess callback to the request object to capture the row value
  3. on each success callback push the row to a privileged array var.

The final row will emit null, which is a state you can watch for to figure out when the cursor has exhausted all its records and is done. When that happens, you can call an export callback passing the privileged array of objects representing a backup of your object store.

Solution 2

You can do it in WebSQL writing a bit of Javascript on top of a solution posted here, however you'll miss out the chance to learn IndexDB.

If you really want to learn IndexDB inside out maybe you can write the import/export tool yourself, I reckon there will be enough need for one in the future.

Share:
20,293
JJJ
Author by

JJJ

Updated on November 21, 2020

Comments

  • JJJ
    JJJ over 3 years

    I'm making a tool for my own use that needs a simple database. This seems like a good chance to learn the HTML5 IndexedDB API, but it's important that I don't lose data at any point.

    I suppose backing up the browser's profile directory would do for a backup, but I'd also like to potentially work with different computers so exporting and importing the database would be nice. Is there an easy way to export and import IndexedDB databases? Browser-specific solutions are ok.

  • JJJ
    JJJ over 12 years
    This doesn't answer the question per se but offers a solution to the underlying problem, so +1.
  • Ash Blue
    Ash Blue over 11 years
    From experience I can say editor's response is the only way to do it. Also be warned, you have to be very clever with all the asynchronous programming it requires. I recommend have a cache object where all the data gets dumped.
  • RaphaelDDL
    RaphaelDDL over 10 years
    Would be fun for the person that added -1 today (2 years after answer was posted) to say why downvoted.
  • David Mulder
    David Mulder over 10 years
    1 year later let me answer your question, it was probably for the same reason I considered downvoting: it doesn't answer the question just Juhana already mentioned
  • RaphaelDDL
    RaphaelDDL over 10 years
    @DavidMulder The question have no code to work on.From the main point of "Is there an easy way to export and import IndexedDB databases?",I gave an example that,like Juhana mentioned in the first comment, offer a solution to the problem.That without giving the ready-to-copy-code. I don't normally give code out of the blue like people who are desperate for rep does. Looking today, I would say the question needs a rewrite with whatever code OP have tried so we can work with or even flagg as too broad. As you saw, even the other answers did not gave OP ready-to-copy-code so no one was awarded..
  • David Mulder
    David Mulder over 10 years
    I wasn't talking about ready-to-copy code, I was just talking about the fact that giving another way of accessing the database (jStorage) does nothing in regards to helping out with the problem of exporting and importing itself (looping over items, getting items, flushing items, etc. are all possible in indexedDB itself...). Either way, build my own general tool for the job in the end (native indexedDB) which I will probably publish on github sometime in the next few days.
  • Justin Emery
    Justin Emery over 7 years
    I've written a module indexeddb-export-import which does this, basically as described in this answer.