Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exporting and importing IndexedDB data

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.

like image 393
JJJ Avatar asked Oct 17 '11 18:10

JJJ


People also ask

Can I export IndexedDB?

No browser's devtools allow you to export or import an IndexedDB database (as of 2020–12–17), but you can invoke dexie and dexie-export-import on any page and make it do the export for you.

How is IndexedDB stored?

More specifically, IndexedDB data is stored in the browser profile folder.

Where is IndexedDB stored Chrome?

Browsing your Database. Browsers make it trivially simple to view the contents of your store. First open up the developer console with F12 . On Chrome you will find it under the Application -> Storage -> IndexedDB .


1 Answers

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.

like image 194
buley Avatar answered Sep 29 '22 18:09

buley