-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
50 lines (43 loc) · 1.16 KB
/
db.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
function Storage(name) {
this.ready = new Promise((resolve, reject) => {
var request = indexedDB.open(location.origin);
request.onupgradeneeded = e => {
this.db = e.target.result;
this.db.createObjectStore('store');
};
request.onsuccess = e => {
this.db = e.target.result;
resolve();
};
request.onerror = e => {
this.db = e.target.result;
reject(e);
};
});
}
Storage.prototype.get = function(key) {
return this.ready.then(() => {
return new Promise((resolve, reject) => {
var request = this.getStore().get(key);
request.onsuccess = e => resolve(e.target.result);
request.onerror = reject;
});
});
};
Storage.prototype.getStore = function() {
return this.db
.transaction(['store'], 'readwrite')
.objectStore('store');
};
Storage.prototype.set = function(key, value) {
return this.ready.then(() => {
return new Promise((resolve, reject) => {
var request = this.getStore().put(value, key);
request.onsuccess = resolve;
request.onerror = reject;
});
});
};
Storage.prototype.reset = function() {
indexedDB.deleteDatabase(location.origin);
};