9 // https://hacks.mozilla.org/2012/02/storing-images-and-files-in-indexeddb/
10 var DB_NAME = "example";
11 var OBJ_STORE_NAME = "objects";
16 var onDBWriteable = function(e) {
17 // Create a new Transaction in which to write
18 var trans = db.transaction([OBJ_STORE_NAME], "readwrite");
19 // Get the Object Store via the Transaction
20 var store = trans.objectStore(OBJ_STORE_NAME);
22 // Write asynchronously
24 var putRequest = store.put({ value: "stuff" }, key);
25 putRequest.onsuccess = function(e) {
26 console.log("writing successful");
29 var getRequest = store.get(key);
30 getRequest.onsuccess = function(e) {
31 console.log("reading successful:", e.target.result);
36 // Open up a DB against a particular version
38 // Note that this call is "async" in the sense that the request
39 // deterministically fires the "upgradeneeded" and "success" events in a
40 // later turn, giving us a chance to regsiter handlers here. There's
41 // nothing in the API contract to indicat that this is so, however.
42 var openRequest = this.indexedDB.open(DB_NAME, VERSION);
44 // If this is the first time we run, upgradeneeded is thrown and we can
45 // create the object store.
46 openRequest.onupgradeneeded = function (e) {
47 console.log("database creation/upgrade needed");
50 console.log("creating object store:", OBJ_STORE_NAME)
51 // Note that we can only call createObjectStore inside of an
52 // upgradeneeded event.
53 db.createObjectStore(OBJ_STORE_NAME);
56 // We can only write once the DB is opened and upgraded/created. onsuccess
57 // is always called after onupgradeneeded (if upgradeneeded is true).
58 openRequest.onsuccess = function(e) {
59 console.log("database opened successfully");
63 // Now write something to the Object Store