-
-
Save mrvicadai/3a6c1a98dba067c0443edc0bab5d955a to your computer and use it in GitHub Desktop.
Revisions
-
JamesMessinger revised this gist
Apr 30, 2015 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,6 @@ // This works on all devices/browsers, and uses IndexedDBShim as a final fallback var indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB || window.shimIndexedDB; // Open (or create) the database var open = indexedDB.open("MyDatabase", 1); -
JamesMessinger created this gist
Apr 30, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,38 @@ // Open (or create) the database var open = indexedDB.open("MyDatabase", 1); // Create the schema open.onupgradeneeded = function() { var db = open.result; var store = db.createObjectStore("MyObjectStore", {keyPath: "id"}); var index = store.createIndex("NameIndex", ["name.last", "name.first"]); }; open.onsuccess = function() { // Start a new transaction var db = open.result; var tx = db.transaction("MyObjectStore", "readwrite"); var store = tx.objectStore("MyObjectStore"); var index = store.index("NameIndex"); // Add some data store.put({id: 12345, name: {first: "John", last: "Doe"}, age: 42}); store.put({id: 67890, name: {first: "Bob", last: "Smith"}, age: 35}); // Query the data var getJohn = store.get(12345); var getBob = index.get(["Smith", "Bob"]); getJohn.onsuccess = function() { console.log(getJohn.result.name.first); // => "John" }; getBob.onsuccess = function() { console.log(getBob.result.name.first); // => "Bob" }; // Close the db when the transaction is done tx.oncomplete = function() { db.close(); }; }