Skip to content

Instantly share code, notes, and snippets.

@Breta01
Created September 3, 2016 17:33
Show Gist options
  • Save Breta01/eb86eb28bdeb66b947f576aeb6cd2d66 to your computer and use it in GitHub Desktop.
Save Breta01/eb86eb28bdeb66b947f576aeb6cd2d66 to your computer and use it in GitHub Desktop.
Implementing IndexedDB
/** Fetch all of the stats from the datastore **/
mkDB.fetchStats = function(callback, user = "All") {
var db = datastore;
var tx = db.transaction("stats", "readonly");
var store = tx.objectStore("stats");
//Get only certain user data if specified
if (user === "All") {
var request = store.openCursor(IDBKeyRange.lowerBound(0));
} else {
var index = store.index("by_user");
var request = index.openCursor(IDBKeyRange.only(user));
}
// Storing loaded stats
var stats = [];
request.onsuccess = function() {
var rec = request.result;
if (rec) {
// Called for each matching record.
stats.push(rec.value);
rec.continue();
}
};
tx.oncomplete = function() {
callback(stats);
};
// Error handling
request.onerror = mkDB.onerror;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment