-
-
Notifications
You must be signed in to change notification settings - Fork 642
/
DexieAlgorithmsSamples.html
121 lines (112 loc) · 4.44 KB
/
DexieAlgorithmsSamples.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<!DOCTYPE html>
<html>
<head>
<title>Extended indexedDB queries using Dexie.js</title>
<script>
if (!(window.indexedDB || window.window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB))
// Pull down the polyfill if needed (Safari + older opera browser)
document.write('<script src="https://rawgit.com/axemclion/IndexedDBShim/master/dist/indexeddbshim.js">\x3C/script>');
</script>
<script type="text/javascript" src="../../dist/dexie.js"></script>
<script>
//
// App global database instance and schema
//
var db = new Dexie("MyDB");
db.version(1).stores({
friends: "++id,name,shoeSize"
});
db.open();
//
// Populate some data
//
function populateSomeData() {
log("Populating some data", "heading");
return db.transaction("rw", db.friends, function () {
db.friends.clear();
db.friends.add({ name: "David", shoeSize: 43 });
db.friends.add({ name: "Ylva", shoeSize: 37 });
db.friends.add({ name: "Jon", shoeSize: 44 });
db.friends.add({ name: "Måns", shoeSize: 42 });
db.friends.add({ name: "Daniel", shoeSize: 39 });
db.friends.add({ name: "Nils", shoeSize: 45 });
db.friends.add({ name: "Zlatan", shoeSize: 47 });
// Log data from DB:
db.friends.orderBy('name').each(function (friend) {
log(JSON.stringify(friend));
});
}).catch(function (e) {
log(e, "error");
});
}
//
// Examples
//
function equalsAnyOf() {
log("db.friends.where('name').anyOf('David', 'Zlatan', 'Daniel')", "heading");
return db.friends.where('name').anyOf('David', 'Zlatan', 'Daniel')
.each(function (friend) {
log(JSON.stringify(friend));
});
}
function equalsIgnoreCase() {
log("db.friends.where('name').equalsIgnoreCase('david')", "heading");
return db.friends.where('name').equalsIgnoreCase('david')
.each(function (friend) {
log(JSON.stringify(friend));
});
}
function startsWithIgnoreCase() {
log("db.friends.where('name').startsWithIgnoreCase('da')", "heading");
return db.friends.where('name').startsWithIgnoreCase('da')
.each(function (friend) {
log(JSON.stringify(friend));
});
}
function logicalOR() {
log("db.friends.where('name').startsWithIgnoreCase('da').or('shoeSize').below(40)", "heading");
return db.friends.where('name').startsWithIgnoreCase('da')
.or('shoeSize').below(40)
.each(function (friend) {
log(JSON.stringify(friend));
});
}
function logicalAND() {
log("db.friends.where('name').startsWithIgnoreCase('da').and(function (friend) { return friend.shoeSize > 40; })", "heading");
return db.friends.where('name').startsWithIgnoreCase('da')
.and(function (friend) { return friend.shoeSize > 40; })
.each(function (friend) {
log(JSON.stringify(friend));
});
}
//
// Helpers
//
function log(txt, clazz) {
var li = document.createElement('li');
li.textContent = txt.toString();
if (clazz) li.className = clazz;
document.getElementById('log').appendChild(li);
}
function runSamples() {
populateSomeData()
.then(equalsAnyOf)
.then(equalsIgnoreCase)
.then(startsWithIgnoreCase)
.then(logicalOR)
.then(logicalAND)
.catch(function (e) {
log(e, "error");
});
}
</script>
<style>
li {list-style-type:none;}
.error { color: red; }
.heading { color: #808080; margin-top: 12px;}
</style>
</head>
<body onload="runSamples();">
<ul id="log"></ul>
</body>
</html>