Skip to content

Instantly share code, notes, and snippets.

@kkasaei
Created February 24, 2016 06:30
Show Gist options
  • Save kkasaei/b6f3551d680a94a0ef9f to your computer and use it in GitHub Desktop.
Save kkasaei/b6f3551d680a94a0ef9f to your computer and use it in GitHub Desktop.
<!doctype html>
<html>
<head>
<title>Index DB DEMO</title>
<!-- https://code.tutsplus.com/tutorials/working-with-indexeddb--net-34673-->
</head>
<body>
<script>
var db;
var dbVersion = 1;
function indexedDBOk() {
return "indexedDB" in window;
}
document.addEventListener("DOMContentLoaded", function() {
//No support? Go in the corner and pout.
if(!indexedDBOk) return;
var openRequest = indexedDB.open("idarticle_people",dbVersion);
openRequest.onupgradeneeded = function(e) {
var thisDB = e.target.result;
if(!thisDB.objectStoreNames.contains("people")) {
thisDB.createObjectStore("people", {autoIncrement:true});
}
};
openRequest.onsuccess = function(e) {
console.log("running onsuccess");
db = e.target.result;
//Listen for add clicks
document.querySelector("#addButton").addEventListener("click", addPerson, false);
};
openRequest.onerror = function(e) {
//Do something for the error
}
},false);
function addPerson(e) {
var name = document.querySelector("#name").value;
var email = document.querySelector("#email").value;
console.log("About to add "+name+"/"+email);
var transaction = db.transaction(["people"],"readwrite");
var store = transaction.objectStore("people");
//Define a person
var person = {
name:name,
email:email,
created:new Date()
};
//Perform the add
var request = store.add(person,dbVersion);
dbVersion++;
request.onerror = function(e) {
console.log("Error",e.target.error.name);
//some type of error handler
};
request.onsuccess = function(e) {
console.log("Woot! Did it");
}
}
function getPerson(e) {
var key = 1;
if(key === "" || isNaN(key)) return;
var transaction = db.transaction(["people"],"readonly");
var store = transaction.objectStore("people");
var request = store.get(Number(key));
request.onsuccess = function(e) {
var result = e.target.result;
console.dir(result);
if(result) {
var s = "&lt;h2>Key "+key+"&lt;/h2>&lt;p>";
for(var field in result) {
s+= field+"="+result[field]+"&lt;br/>";
}
document.querySelector("#status").innerHTML = s;
} else {
document.querySelector("#status").innerHTML = "&lt;h2>No match&lt;/h2>";
}
}
}
function getAll(e) {
var s = "";
db.transaction(["people"], "readonly").objectStore("people").openCursor().onsuccess = function(e) {
var cursor = e.target.result;
if(cursor) {
s += "&lt;h2>Key "+cursor.key+"&lt;/h2>&lt;p>";
for(var field in cursor.value) {
s+= field+"="+cursor.value[field]+"&lt;br/>";
}
s+="&lt;/p>";
cursor.continue();
}
document.querySelector("#status").innerHTML = s;
}
}
</script>
<input type="text" id="name" placeholder="Name"><br/>
<input type="email" id="email" placeholder="Email"><br/>
<button id="addButton">Add Data</button>
<button id="getPerson" onclick="getPerson(event)">get Person</button>
<button id="getAll" onclick="getAll(event)">get All</button>
<div id="status"></div>
</body>
</html>
@kkasaei
Copy link
Author

kkasaei commented Feb 24, 2016

A Sample Index DB exercise base on Tuts+ article : https://code.tutsplus.com/tutorials/working-with-indexeddb--net-34673

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