Skip to content

Instantly share code, notes, and snippets.

@thomaswilburn
Last active August 29, 2015 14:13
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 thomaswilburn/a56e70c5aab23c5ca9e5 to your computer and use it in GitHub Desktop.
Save thomaswilburn/a56e70c5aab23c5ca9e5 to your computer and use it in GitHub Desktop.
oh god indexeddb why
var log = console.log.bind(console);
var Database = function(name, version, upgrade) {
var self = this;
this.ready = new Promise(function(ok, fail) {
var req = window.indexedDB.open(name, version);
req.onupgradeneeded = function(db) {
self.db_ = req.result;
if (upgrade) upgrade();
}
req.onsuccess = function(db) {
self.db_ = req.result;
ok();
};
req.onerror = fail;
});
};
Database.prototype = {
ready: null,
db_: null,
createStore: function(name, schema) {
var self = this;
schema = schema || {};
return new Promise(function(ok) {
var store = self.db_.createObjectStore(name, { keyPath: schema.key, autoIncrement: schema.autoIncrement });
if (schema.index) {
for (var key in schema.index) {
var options = schema.index[key] || {};
try {
store.createIndex(key, key, options);
} catch(err) { console.log(err) }
}
}
});
},
transaction_: function(stores, write) {
return this.db_.transaction(stores, write ? "readwrite" : undefined);
},
put: function(store, value, key) {
var self = this;
return new Promise(function(ok, fail) {
var request = self.transaction_(store, true)
request.objectStore(store).put(value, key);
request.oncomplete = function() {
ok();
};
request.onerror = fail;
});
},
get: function(table, index, key) {
if (!key) {
key = index;
index = null;
}
var self = this;
return new Promise(function(ok, fail) {
var transaction = self.transaction_(table);
var store = transaction.objectStore(table);
var request;
if (index) {
try {
request = store.index(index).get(key);
} catch(e) { console.log(e) };
} else {
try {
request = store.get(key);
} catch(e) { console.log(e) };
}
transaction.oncomplete = function() {
ok(request.result);
};
request.onerror = fail;
});
},
getAll: function(table, bounds) {
var self = this;
return new Promise(function(ok, fail) {
var transaction = self.transaction_(table);
var store = transaction.objectStore(table);
var items = {};
var req = store.openCursor();
req.onsuccess = function() {
var cursor = req.result;
console.log(cursor);
if (cursor) {
var item = cursor.value;
var key = cursor.key;
items[key] = item;
cursor.continue();
} else {
console.log("returning");
ok(items);
}
}
});
}
}
var db = new Database("whitman", 1, function() {
db.createStore("files", {
key: "track",
autoIncrement: true,
index: {
filename: null
}
});
});
var input = document.querySelector("input");
input.addEventListener("change", function() {
if (input.files.length) {
var file = input.files[0];
var reader = new FileReader();
reader.onload = function() {
var buffer = reader.result;
var blob = new Blob([buffer]);
var record = {
blob: blob,
filename: file.name
};
db.ready.then(function() {
db.put("files", record).then(function() {
log("complete!");
})
});
};
reader.readAsArrayBuffer(file);
}
});
var a = document.querySelector(".get");
a.addEventListener("click", function() {
console.log("get");
db.ready.then(function() {
db.getAll("files").then(function(files) {
log(files);
});
});
});
@thomaswilburn
Copy link
Author

Incidentally, this gets slightly nicer when we have fat-arrow functions.

db.put("objects", { id: 1 })
  .then(() => db.get("objects", 1))
  .then(result => console.log(result));

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