JSFiddle - React, Tailwind, and code Playground
by taylorbuley
JavaScript
(function() {
var database, upgraded = false;
window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB ||
window.msIndexedDB;
window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction ||
window.msIDBTransaction;
window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange || window.msIDBKeyRange;
if (!window.indexedDB) {
alert("No IndexedDB support");
}
//open db
var request = window.indexedDB.open("notepad");
request.onerror = function(event) {
console.log('error code', event.target.errorCode);
};
//if exists or after onupgrade needed is called
request.onsuccess = function(event){
database = request.result;
};
//if doesnt exist or different version
request.onupgradeneeded = function(event) {
var db = event.target.result;
var objectStore = db.createObjectStore("notes" + new Date().getTime(), { keyPath: "id",autoIncrement:true});
objectStore.createIndex("name", "name", { unique: false });
objectStore.createIndex("age", "age", { unique: false });
objectStore.createIndex("tel", "tel", { unique: false });
writeIT();
};
//add test data
function writeIT(){
var note={name:"Test", age:"99", tel:"0123456789"};
var transaction = database.transaction(["notes"], "readwrite");
var objectStore = transaction.objectStore("notes");
var request2=objectStore.put(note);
request2.addEventListener('success', function(e) {
console.log('put key', e.target.result);
});
var note={name:"Test1", age:"199", tel:"0123456789"};
var transaction = database.transaction(["notes"], "readwrite");
var objectStore = transaction.objectStore("notes");
var request2=objectStore.put(note);
request2.addEventListener('success', function(e) {
console.log('put key', e.target.result);
});
var note={name:"Test2", age:"299", tel:"0123456789"};
var transaction = database.transaction(["notes"], "readwrite");
var objectStore =...