Skip to content

Instantly share code, notes, and snippets.

@enjalot
Created September 7, 2013 01:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enjalot/6472041 to your computer and use it in GitHub Desktop.
Save enjalot/6472041 to your computer and use it in GitHub Desktop.
test indexedDB
{"description":"test indexedDB","endpoint":"","display":"svg","public":true,"require":[],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":false,"play":false,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"period","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01,"inline-console":false,"thumbnail":"http://i.imgur.com/ZjsRwTk.png"}
/*
this finally worked for me: http://www.sitepoint.com/creating-a-notepad-app-with-indexeddb/
https://developer.mozilla.org/en-US/docs/IndexedDB/Using_IndexedDB
https://developer.mozilla.org/en-US/docs/Web/API/IDBDatabase
http://hacks.mozilla.org/2012/02/storing-images-and-files-in-indexeddb/
http://greenido.wordpress.com/2012/10/05/how-to-use-indexeddb-simplest-example/
http://www.html5rocks.com/en/tutorials/indexeddb/todo/
*/
var db;
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("Sorry!Your browser doesn't support IndexedDB"); }
var collection = "users";
function init(){
var request = window.indexedDB.open("test",1);
request.onerror = function(event) {
console.log(event.target.errorCode);
};
request.onsuccess = function(event) {
db=request.result;
go();
};
request.onupgradeneeded = function(event) {
console.log("UPGRADE")
var db = event.target.result;
var objectStore = db.createObjectStore(collection, { keyPath: "id"});
objectStore.createIndex("name", "name", { unique: false });
objectStore.createIndex("email", "email", { unique: true });
objectStore.createIndex("accountId", "accountId", { unique: false });
};
}
function go() {
console.log("GO", db)
getDocs({}, function() {});
/*addDoc({
id: 123,
accountId: 345,
name: "ian",
email: "enjalot@gmail.com"
}, function(err, e) {
console.log("done", err, e);
})*/
}
function getDocs(q, cb) {
var objectStore = db.transaction(collection).objectStore(collection);
objectStore.openCursor().onsuccess = function(event) {
var cursor = event.target.result;
console.log("CURSOR", cursor)
};
}
function addDoc(doc, cb) {
console.log("add", doc)
var trans = db.transaction([collection], "readwrite");
console.log("Asdf", collection, trans);
var store = trans.objectStore(collection);
var request = store.put(doc);
request.onsuccess = function(e) { cb(null, e) };
request.onerror = function(e) { cb(e) };
}
init()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment