-
Notifications
You must be signed in to change notification settings - Fork 3
/
promiseDB.js
176 lines (156 loc) · 5.01 KB
/
promiseDB.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
var pdb = {
_transformRequestToPromise: function(thisobj, func, argArray) {
return new Promise(function(resolve, reject) {
var request = func.apply(thisobj, argArray);
request.onsuccess = function() {
resolve(request.result);
};
request.onerror = reject;
})
},
transact: function(db, objectStores, style) {
return Promise.resolve(db.transaction(objectStores, style));
},
openCursor: function(txn, indexOrObjectStore, keyrange, callback) {
return new Promise(function(resolve, reject) {
var request = indexOrObjectStore.openCursor(keyrange);
request.onerror = reject;
request.onsuccess = function() {
var cursor = request.result;
var cont = false;
var control = {
continue: function() {cont = true;}
};
if (cursor) {
callback(control, cursor.value);
if (cont) {
cursor.continue();
} else {
resolve(txn);
}
} else {
resolve(txn);
}
};
});
},
get: function(indexOrObjectStore, key) {
return this._transformRequestToPromise(indexOrObjectStore, indexOrObjectStore.get, [key]);
},
count: function(indexOrObjectStore, key) {
return this._transformRequestToPromise(indexOrObjectStore, indexOrObjectStore.count, [key]);
},
put: function(objectStore, key, value) {
return this._transformRequestToPromise(objectStore, objectStore.put, [key, value]);
},
add: function(objectStore, key, value) {
return this._transformRequestToPromise(objectStore, objectStore.add, [key, value]);
},
delete: function(objectStore, key) {
return this._transformRequestToPromise(objectStore, objectStore.delete, [key]);
},
clear: function(objectStore) {
return this._transformRequestToPromise(objectStore, objectStore.clear, []);
},
getKey: function(index, key) {
return this._transformRequestToPromise(index, index.getKey, [key]);
},
waitForTransaction: function(txn) {
return new Promise(function(resolve, reject) {
txn.oncomplete = resolve;
txn.onerror = reject;
txn.onabort = reject;
});
},
waitForTransactionFcn: function(txn) {
return function() {
return this.waitForTransaction(txn);
};
},
}
var logToConsole = function(value) {
console.log(value);
};
var db;
var version = 3.0;
function initDb(oncomplete) {
var request = indexedDB.open("TestDatabase", version);
request.onerror = function(evt) {
console.log("Database error code: " + evt.target.errorCode);
};
request.onsuccess = function(evt) {
db = request.result;
oncomplete();
};
request.onupgradeneeded = function (evt) {
var db = request.result;
if (db.objectStoreNames.contains("books")) {
db.deleteObjectStore("books");
}
var store = db.createObjectStore("books", {keyPath: "isbn"});
var titleIndex = store.createIndex("by_title", "title", {unique: true});
var authorIndex = store.createIndex("by_author", "author");
// Populate with initial data.
store.put({title: "Quarry Memories", author: "Fred", isbn: 123456});
store.put({title: "Water Buffaloes", author: "Fred", isbn: 234567});
store.put({title: "Bedrock Nights", author: "Barney", isbn: 345678});
};
}
var main = function() {
console.log("starting main");
getBooks("Fred").then(function(books) {
console.log(books.join(", "));
}).then(function() {
addMoreBooks();
}).then(function() {
return getBooks("Fred").then(function(books) {
console.log(books.join(", "));
});
}).then(function() {
return lookupBook("Bedrock Nights");
}).then(logToConsole)
.then(function() {
return lookupBook("Test3");
})
.then(logToConsole)
.then(function(x) { console.log("done!"); } )
.catch(logToConsole);
}
initDb(main);
var lookupBook = function(bookName) {
var book;
return pdb.transact(db, "books", "readonly")
.then(function(txn) {
var store = txn.objectStore("books");
var index = store.index("by_title");
return pdb.get(index, bookName)
.then(function (result) {
book = result;
}).then(pdb.waitForTransaction(txn));
}).then(function() {
return book;
});
}
var addMoreBooks = function() {
return pdb.transact(db, "books", "readwrite")
.then(function(txn) {
var objectStore = txn.objectStore("books");
var p1 = pdb.put(objectStore, {title: "Test2", author: "Fred", isbn: 1234526});
var p2 = pdb.put(objectStore, {title: "Test3", author: "Barney", isbn: 1234523});
return Promise.all([p1, p2, pdb.waitForTransaction(txn)]);
});
}
var getBooks = function(author) {
var books = [];
return pdb.transact(db, "books", "readonly")
.then(function(txn) {
var index = txn.objectStore("books").index("by_author");
return pdb.openCursor(txn, index, IDBKeyRange.only(author), function(control, value) {
books.push(value.title);
control.continue();
});
}).then(pdb.waitForTransaction)
.then(function() {
return books;
});
}