Exporting and importing IndexedDB data

白昼怎懂夜的黑 提交于 2019-12-22 01:58:05

问题


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.


回答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.




回答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.




回答3:


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). :)



来源:https://stackoverflow.com/questions/7798098/exporting-and-importing-indexeddb-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!