-
Notifications
You must be signed in to change notification settings - Fork 0
/
idb.js
81 lines (72 loc) · 2.1 KB
/
idb.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
module.exports = function (file) {
var db
var env = typeof window == 'object' ? window : self;
function load (cb) {
var req = env.indexedDB.open(file, 1)
req.onsuccess = function (ev) {
db = ev.target.result
db.transaction(['meta'],'readonly').objectStore('meta')
.get('meta')
.onsuccess = function (ev) {
if(ev.target.result) cb(null, ev.target.result)
else cb(new Error('meta value is missing'))
}
}
req.onupgradeneeded = function (ev) {
console.log('upgrade needed')
db = ev.target.result
db.createObjectStore('meta')
db.createObjectStore('data')
}
req.onerror = function (ev) {
throw new Error('could not load indexdb:'+dir)
}
}
function destroy (cb) {
// var tx = db.transaction(['meta', 'data'])
db.close(function () {})
var tx = env.indexedDB.deleteDatabase(file)
// tx.deleteDatabase('data')
// tx.onversionchange = function () {
// cb()
// // load(cb)
// }
tx.onsuccess = function () { cb() }
tx.onerror = cb
}
return {
load: load,
batch: function (data, meta, cb) {
var tx = db.transaction(['meta', 'data'], 'readwrite')
tx.onabort = tx.onerror = function (err) { cb(err || error) }
tx.oncomplete = function () {
cb()
}
function onError (_err) {
error = _err
tx.abort()
}
tx.objectStore('meta').put(meta, 'meta').onerror = onError
var dataStore = tx.objectStore('data')
for(var k in data)
dataStore.put(data[k], k).onerror = onError
},
get: function (key, cb) {
var tx = db.transaction(['data'], 'readonly')
var req = tx.objectStore('data').get(key)
req.onsuccess = function (ev) {
if(!ev.target.result) cb(new Error('not found'))
else cb(null, ev.target.result)
}
req.onerror = function () {
cb(new Error('key not found:'+key))
}
},
destroy: destroy,
close: function (cb) {
db.close()
//IDB doesn't actually tell you when your own database is closed!!! thanks IDB!
cb()
}
}
}