-
Notifications
You must be signed in to change notification settings - Fork 0
/
testNigiri.js
99 lines (88 loc) · 3.2 KB
/
testNigiri.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
(function(window) {
var dbFind = function(db) {
var tx = db.transaction("books", "readonly");
var store = tx.objectStore("books");
var index = store.index("by_author");
// var request = index.openCursor(IDBKeyRange.only("Fred"));
var request = index.openCursor(null);
request.onsuccess = function() {
var cursor = request.result;
if (cursor) {
// Called for each matching record.
console.log("dbFind: Found " + cursor.value.isbn + ", " + cursor.value.title + ", " + cursor.value.author);
cursor["continue"]();
} else {
// No more matching records.
console.log("dbFind: Nothing found");
}
};
};
var dbGet = function(db) {
var tx = db.transaction("books", "readonly");
var store = tx.objectStore("books");
var index = store.index("by_title");
var readRequest = index.get("Bedrock Nights");
readRequest.onsuccess = function() {
var matching = readRequest.result;
if (matching !== undefined) {
// A match was found.
console.log("dbGet: Found " + matching.isbn + ", " + matching.title + ", " + matching.author);
} else {
// No match was found.
console.log("dbGet: Nothing found");
}
};
tx.oncomplete = function(e) {
console.log("Transaction complete");
};
};
req = window.Nigiri.IndexedDB.deleteDatabase("testdb");
req.onsuccess = function() {
console.log("Database deleted");
var req = window.Nigiri.IndexedDB.open("testdb", 1);
req.onupgradeneeded = function(e) {
var newVersion = e.newVersion;
var db = e.target.result;
console.log("Upgrade needed callback");
try {
db.deleteObjectStore("books");
} catch (ex) {
}
var store = db.createObjectStore("books", {
keyPath : "isbn"
});
store.createIndex("by_title", "title", {
unique : true
});
store.createIndex("by_author", "author");
// Populate with initial data.
store.putAll([ {
title : "Quarry Memories",
author : "Fred",
isbn : 123456
}, {
title : "Water Buffaloes",
author : "Fred",
isbn : 234567
}, {
title : "Bedrock Nights",
author : "Barney",
isbn : 345678
} ]).onsuccess = function(e) {
console.log("Updated or added " + e.currentTarget.result.successes + " objects");
};
};
req.onsuccess = function(e) {
var db = e.target.result;
console.log("Database opened");
dbGet(db);
dbFind(db);
};
req.onerror = function(e) {
console.log("Failed to open the database");
};
req.onblocked = function(e) {
console.log("Database access blocked");
};
};
})(window);