Skip to content

Instantly share code, notes, and snippets.

@thomasJang
Last active August 29, 2015 14:23
Show Gist options
  • Save thomasJang/ef53d6acd4bb98b44269 to your computer and use it in GitHub Desktop.
Save thomasJang/ef53d6acd4bb98b44269 to your computer and use it in GitHub Desktop.
idb control
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<script>
var db_name = "chequer", db_version = 1;
var idb = (function(){
var indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.msIndexedDB,
IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction,
open_try_time = 3;
/*
indexedDB.deleteDatabase("MyTestDatabase");
indexedDB.deleteDatabase("PeopleDB");
indexedDB.deleteDatabase("TestDatabase");
indexedDB.deleteDatabase("chequer-test");
indexedDB.deleteDatabase("myTestIDB");
*/
//indexedDB.deleteDatabase("chequer");
function open(db_name, db_version, callback){
var _this = this, request = window.indexedDB.open(db_name, db_version);
request.onsuccess = function(evt){
_this.db = request.result;
if(callback) callback.call({}, request.result);
};
request.onerror = function (evt) {
console.log("IndexedDB error: " + evt.target.errorCode);
};
request.onupgradeneeded = function (evt) {
_this.db = event.target.result;
//-- create master
for(var i=_this.db.objectStoreNames.length-1;i>-1;i--){
_this.db.deleteObjectStore(_this.db.objectStoreNames[i]);
}
var
objectStore = _this.db.createObjectStore("master", { keyPath: "key" });
objectStore.createIndex("value", "value", { unique: false });
objectStore = _this.db.createObjectStore("sales", { keyPath: "id", autoIncrement:true });
objectStore.createIndex("date", "date", { unique: false });
objectStore.createIndex("desc", "desc", { unique: false });
objectStore.createIndex("cost", "cost", { unique: false });
};
}
function exec(store_name, act, item, callback, try_time){
if(!this.db){
if(typeof try_time === "undefined" || try_time < open_try_time){
setTimeout((function(){
this.exec(store_name, act, item, callback, (try_time||0)+1);
}).bind(this), 100);
}
return false;
}
var transaction = this.db.transaction(store_name, "readwrite");
// create an object store on the transaction
var objectStore = transaction.objectStore(store_name);
if(act == "add"){
// add our newItem object to the object store
var objectStoreRequest = objectStore.add(item.data);
objectStoreRequest.onsuccess = function(event) {
if(callback) callback.call({}, {});
};
objectStoreRequest.onerror = function(event) {
if(callback) callback.call({error:this.error}, {error:this.error});
};
}
else if(act == "put"){
objectStoreRequest = objectStore.get(item.get);
objectStoreRequest.onsuccess = function() {
// Grab the data object returned as the result
var data = objectStoreRequest.result;
// Update the notified value in the object to "yes"
data[item.set] = item.data;
// Create another request that inserts the item back into the database
var updateTitleRequest = objectStore.put(data);
// When this new request succeeds, run the displayData() function again to update the display
updateTitleRequest.onsuccess = function() {
if(callback) callback.call({}, {});
};
updateTitleRequest.onerror = function() {
if(callback) callback.call({error:this.error}, {error:this.error});
};
};
}
else if(act == "delete"){
objectStoreRequest = objectStore.delete(item.get);
objectStoreRequest.onsuccess = function(event) {
if(callback) callback.call({}, {});
};
objectStoreRequest.onerror = function(event) {
if(callback) callback.call({error:this.error}, {error:this.error});
};
}
else if(act == "get"){
objectStoreRequest = objectStore.get(item.get);
objectStoreRequest.onsuccess = function(event) {
if(callback) callback.call(objectStoreRequest.result, objectStoreRequest.result);
};
objectStoreRequest.onerror = function(event) {
if(callback) callback.call({error:this.error}, {error:this.error});
};
}
}
return {
db: null,
open: open,
exec: exec
}
})();
idb.open(db_name, db_version, function(){
//-- get storeNames
for(var i=0, l=idb.db.objectStoreNames.length;i<l;i++){
// console.log(idb.db.objectStoreNames.item(i));
}
});
function add_master(){
var item = {key:"system", value:{a:1,b:2}};
idb.exec("master", "add", {data:item}, function(result){
if(result && result.error){
console.error(result.error.message);
}
});
idb.exec("master", "add", {data:{key:"store", value:"체커"}}, function(result){
if(result && result.error){
console.error(result.error.message);
}
});
}
function put_master() {
var item = {key: "system", value: {a: 99, b: 88}};
idb.exec("master", "put", {get: item.key, set: "value", data: item.value}, function (result) {
if (result && result.error) {
console.error(result.error.message);
}
});
}
function delete_master() {
idb.exec("master", "delete", {get: "store"}, function (result) {
if (result && result.error) {
console.error(result.error.message);
}
});
}
function get_master() {
idb.exec("master", "get", {get: "store"}, function (result) {
if (result && result.error) {
console.error(result.error.message);
return;
}
console.log(result);
});
idb.exec("master", "get", {get: "1122"}, function (result) {
if (result && result.error) {
console.error(result.error.message);
return;
}
console.log(result);
});
}
</script>
</head>
<body>
<button onclick="add_master();">add master</button>
<button onclick="put_master();">put master</button>
<button onclick="delete_master();">delete master</button>
<button onclick="get_master();">get master</button>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment