Skip to content

Instantly share code, notes, and snippets.

@bstavroulakis
Last active February 15, 2024 13:02
Show Gist options
  • Save bstavroulakis/a962b30bb2de84cd3eb0e2856bff86ca to your computer and use it in GitHub Desktop.
Save bstavroulakis/a962b30bb2de84cd3eb0e2856bff86ca to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<body>
<div id="data"></div>
<script>
const request = window.indexedDB.open("testDB", 1);
request.onupdatedneeded = function(){
const objectStore = request.result.createObjectStore("users", {keyPath:"id"});
objectStore.add({
id: 1,
name: "Eva Smith",
hobbies: ["travelling"]
});
objectStore.add({
id: 2,
name: "Bill Smith",
hobbies: ["tennis"]
});
objectStore.createIndex("by-name", "name")
}
request.onsuccess = function(){
const transaction = request.result.transaction(['users'], 'readonly');
const objectStore = transaction.objectStore("users");
objectStore.get(1).onsuccess = function(){
document.getElementById("data").innerHTML = this.result.name;
}
}
request.onerror = function(){}
request.onblocked = function(){}
</script>
</body>
</html>
@laurenwestonn
Copy link

"onupdatedneeded" doesn't exist, I think you mean "onupgradeneeded"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment