Skip to content

Instantly share code, notes, and snippets.

@dtschust
Created February 26, 2021 22:37
Show Gist options
  • Save dtschust/823390aa0ba86849b552da2056d14acc to your computer and use it in GitHub Desktop.
Save dtschust/823390aa0ba86849b552da2056d14acc to your computer and use it in GitHub Desktop.
fill up indexedDB with at least 2 gigs of junk
var one = new Array(267386880/2 - 1000).join('a')
// 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);
// 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
for(i=0;i<40;i++) {
store.put({id: 100 + i, name: {first: one, last: "Doe"}, age: 42});
}
// 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() {
console.log("DONE")
db.close();
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment