Skip to content

Instantly share code, notes, and snippets.

@jchris
Created November 7, 2014 17:23
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 jchris/61c1871e13494a9a420f to your computer and use it in GitHub Desktop.
Save jchris/61c1871e13494a9a420f to your computer and use it in GitHub Desktop.
safari indexeddb bug
require('tap-browser-color')();
var test = require("tape");
var dbName = "safari_test2";
test("clear old", function(t) {
indexedDB.deleteDatabase(dbName).onsuccess = function(){
t.end()
}
})
var db;
test("create db", function(t) {
var request = indexedDB.open(dbName, 1);
request.onerror = function(e){
console.error(e)
t.fail(e)
};
request.onsuccess = function(event) {
db = event.target.result;
db.onerror = function(event){
console.error("View IndexedDB error", event);
}
db.onclose = function (e) {
console.error("db closing", e)
}
db.onabort = function (e) {
console.error("db onabort", e)
}
db.onversionchange = function(event) {
console.error("Closing connection to db")
event.target.close();
}
t.end()
}
request.onblocked = function (e) {
console.error("Couldn't open the database due to the operation being blocked");
t.fail(e)
};
request.onupgradeneeded = function(event) {
var db = event.target.result;
var map = db.createObjectStore("map", {autoIncrement: true});
map.createIndex("key", "k");
map.createIndex("id", "i");
db.createObjectStore("meta");
};
})
test("insert records", function(t) {
var txn = db.transaction(["map"], "readwrite"),
map = txn.objectStore("map"),
i = 0;
while (i++ < 100) {
// console.log("put", i)
map.put({i : i.toString(), k : [1000*i]})
map.put({i : i.toString(), k : [2*i]})
}
txn.oncomplete = function(){
t.end()
}
});
test("read index", function(t) {
var txn = db.transaction(["map"], "readonly"),
map = txn.objectStore("map"),
key = map.index("key");
key.openCursor().onsuccess = function(event) {
var cursor = event.target.result;
if (cursor) {
console.log(cursor.key, cursor.value)
var num = parseInt(cursor.value.i)
if (cursor.key[0] > 200) {
t.deepEquals(cursor.key, [num*1000])
} else {
t.deepEquals(cursor.key, [num*2])
}
cursor.continue()
} else {
t.end()
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment