20

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
  • 32,902
  • 20
  • 89
  • 102
  • 4
    I'm a little surprised there are no options for anything like that in the web inspectors. Chrome's inspector lets you see the database, but it doesn't look like you can interact with it much. – Bart Oct 28 '11 at 21:07
  • @StevendeSalas Not yet -- I suppose it's likely that it's new enough that tools like this don't exist yet. – JJJ Nov 01 '11 at 12:12

3 Answers3

10

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.

buley
  • 28,032
  • 17
  • 85
  • 106
  • 3
    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. – Ash Blue Sep 06 '12 at 21:28
  • 2
    I've written a module [indexeddb-export-import](https://github.com/Polarisation/indexeddb-export-import) which does this, basically as described in this answer. – Justin Emery Dec 01 '16 at 11:12
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.

Community
  • 1
  • 1
Steven de Salas
  • 20,944
  • 9
  • 74
  • 82
0

Try use jStorage, it supports most browsers, except the ones without localStorage (like deprecated Safari3)

It got lots of functions, but we can try achieve what you want with those:

set(key, value)

$.jStorage.set(key, value)

Saves a value to local storage. key needs to be string otherwise an exception is thrown. value can be any JSONeable value, including objects and arrays or a XML node. Currently XML nodes can't be nested inside other objects: $.jStorage.set("xml", xml_node) is OK but $.jStorage.set("xml", {xml: xml_node}) is not.


get(key[, default])

value = $.jStorage.get(key)
value = $.jStorage.get(key, "default value")

get retrieves the value if key exists, or default if it doesn't. key needs to be string otherwise an exception is thrown. default can be any value.


flush()

$.jStorage.flush()

Clears the cache.


index()

$.jStorage.index()

Returns all the keys currently in use as an array.

var index = $.jStorage.index();
console.log(index); // ["key1","key2","key3"]

With that in mind, considering you already have a DB set up, you can use var index = $.jStorage.index(); and with the array, create a jQuery .each() loop that gets each key of the array and call the get() $.jStorage.get(key) and add to a big string, that in the end can be parsed as .csv, or even XML or json (you choose).

With these data in hands, you can $.jStorage.flush() to clear.

Then, if you want to import the data for a new DB, all you need to do is a .each() that reads the string/file you've saved and start setting the kay/value par with $.jStorage.set(key, value).

If you don't have a DB already, just populate a new one with $.jStorage.set(key, value). :)

RaphaelDDL
  • 4,452
  • 2
  • 32
  • 56
  • 3
    This doesn't answer the question per se but offers a solution to the underlying problem, so +1. – JJJ Oct 29 '11 at 08:53
  • Would be fun for the person that added -1 today (2 years after answer was posted) to say why downvoted. – RaphaelDDL Sep 02 '13 at 21:03
  • 3
    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 – David Mulder Feb 11 '14 at 16:40
  • @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.. – RaphaelDDL Feb 12 '14 at 18:57
  • 3
    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. – David Mulder Feb 12 '14 at 22:02