Guest User

Untitled

a guest
Oct 21st, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. // This works on all devices/browsers, and uses IndexedDBShim as a final fallback
  2. var indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB || window.shimIndexedDB;
  3. // Open (or create) the database
  4. var open = indexedDB.open("MachC", 1);
  5. // Create the schema
  6. open.onupgradeneeded = function() {
  7. var db = open.result;
  8. var store = db.createObjectStore("MyObjectStore", {keyPath: "id"});
  9. var index = store.createIndex("NameIndex", ["name.last", "name.first"]);
  10. };
  11. var getJohn;
  12. open.onsuccess = function() {
  13. // Start a new transaction
  14. var db = open.result;
  15. var tx = db.transaction("MyObjectStore", "readwrite");
  16. var store = tx.objectStore("MyObjectStore");
  17. var index = store.index("NameIndex");
  18.  
  19. // Add some data
  20. store.put({id: 12345, name: {first: "John", last: "Doe"}, age: 42});
  21. store.put({id: 67890, name: {first: "Bob", last: "Smith"}, age: 35});
  22.  
  23. // Query the data
  24. getJohn = store.get(12345);
  25. var getBob = index.get(["Smith", "Bob"]);
  26.  
  27. getJohn.onsuccess = function() {
  28. console.log(getJohn.result.name.first); // => "John"
  29. };
  30.  
  31. getBob.onsuccess = function() {
  32. console.log(getBob.result.name.first); // => "Bob"
  33. };
  34.  
  35. // Close the db when the transaction is done
  36. tx.oncomplete = function() {
  37. db.close();
  38. };
  39. }
Add Comment
Please, Sign In to add comment