-
Notifications
You must be signed in to change notification settings - Fork 5
/
example.html
89 lines (70 loc) · 2.4 KB
/
example.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>IndexedDB-getAll-shim example</title>
</head>
<body>
<div id="log"></div>
<script src="IndexedDB-getAll-shim.js"></script>
<script>
var db, employeesData, request;
window.indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.msIndexedDB;
window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange;
function log(msg) {
var e = document.getElementById("log");
if (e) {
document.getElementById("log").innerHTML += msg + "<br>"
} else {
console.log(msg);
}
}
employeesData = [
{salary: 50000, name: "Bob"},
{salary: 40000, name: "Sarah"},
{salary: 48000, name: "Kate"}
];
request = indexedDB.open("testDatabase");
request.onupgradeneeded = function (event) {
var db, i, objectStore;
db = event.target.result;
if (!db.objectStoreNames.contains("employees")) {
objectStore = db.createObjectStore("employees", {keyPath: "id", autoIncrement: true});
objectStore.createIndex("name", "name", {unique: false});
for (i = 0; i < employeesData.length; i += 1) {
objectStore.add(employeesData[i]);
}
}
};
request.onsuccess = function (event) {
db = request.result;
db.transaction("employees").objectStore("employees").getAll().onsuccess = function (event) {
var employees, i;
employees = event.target.result;
log("All employees:");
for (i = 0; i < employees.length; i += 1) {
log(employees[i].name + " (ID: " + employees[i].id + ")")
}
log("");
db.transaction("employees").objectStore("employees").getAll(IDBKeyRange.lowerBound(2)).onsuccess = function (event) {
var employees, i;
employees = event.target.result;
log("Employees with ID 2 or greater (IDBKeyRange support):");
for (i = 0; i < employees.length; i += 1) {
log(employees[i].name + " (ID: " + employees[i].id + ")")
}
log("");
db.transaction("employees").objectStore("employees").index("name").getAll(IDBKeyRange.lowerBound("C")).onsuccess = function (event) {
var employees, i;
employees = event.target.result;
log("Employees with names starting with a letter after 'C' (index support):");
for (i = 0; i < employees.length; i += 1) {
log(employees[i].name + " (ID: " + employees[i].id + ")")
}
};
};
};
};
</script>
</body>
</html>