-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
182 lines (136 loc) · 4.73 KB
/
index.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
177
178
179
180
181
182
var Nanobus = require('nanobus')
var assert = require('assert')
module.exports = Nanoidb
function Nanoidb (name, version) {
if (!(this instanceof Nanoidb)) return new Nanoidb(name, version)
Nanobus.call(this, 'Nanoidb')
this._name = name
this._version = version
assert.equal(typeof name, 'string', 'Nanoidb: name should be type string')
assert.equal(typeof version, 'number', 'Nanoidb: version should be type number')
this.upgrade(this._version)
}
Nanoidb.prototype = Object.create(Nanobus.prototype)
Nanoidb.prototype.upgrade = function (version) {
assert.equal(typeof version, 'number', 'Nanoidb.upgrade: version should be type number')
this._version = version
this.request = window.indexedDB.open(this._name, this._version)
this.request.onerror = this.onerror.bind(this)
this.request.onsuccess = this.onsuccess.bind(this)
this.request.onupgradeneeded = this.onupgradeneeded.bind(this)
}
Nanoidb.prototype.onsuccess = function (event) {
var storeNames = event.target.result.objectStoreNames
this.db = event.target.result
var self = this
var stores = Object.keys(storeNames).reduce(function (stores, key) {
var name = storeNames[key]
stores[name] = new Store(name, self.db)
return stores
}, {})
this.emit('open', stores)
}
Nanoidb.prototype.onerror = function (event) {
this.db = event.target.result
this.emit('error', this.db.error)
}
Nanoidb.prototype.onupgradeneeded = function (event) {
this.db = event.target.result
this.emit('upgrade', {
db: this.db,
event: event
})
}
function Store (name, db) {
this.name = name
this.db = db
}
Store.prototype.put = function (key, val, cb) {
assert.equal(typeof key, 'string', 'Nanoidb.Store.put: key should be type string')
assert.notEqual(typeof val, 'undefined', 'Nanoidb.Store.put: val should not be type undefined')
assert.equal(typeof cb, 'function', 'Nanoidb.Store.put: cb should be type function')
var transaction = this.db.transaction(this.name, 'readwrite')
var store = transaction.objectStore(this.name)
store.put(val, key)
transaction.oncomplete = function (event) {
cb()
}
transaction.onerror = function () {
cb(transaction.error)
}
}
Store.prototype.get = function (key, cb) {
assert.equal(typeof key, 'string', 'Nanoidb.Store.get: key should be type string')
assert.equal(typeof cb, 'function', 'Nanoidb.Store.get: cb should be type function')
var transaction = this.db.transaction(this.name, 'readonly')
var store = transaction.objectStore(this.name)
var res = store.get(key)
transaction.oncomplete = function (event) {
cb(null, res.result)
}
transaction.onerror = function () {
cb(transaction.error)
}
}
Store.prototype.getAll = function (query, count, cb) {
var transaction = this.db.transaction(this.name, 'readonly')
var store = transaction.objectStore(this.name)
if (typeof query === 'function') {
var res = store.getAll()
cb = query
} else if (typeof query === 'string') {
res = store.getAll(query, count)
}
transaction.oncomplete = function (event) {
cb(null, res.result)
}
transaction.onerror = function () {
cb(transaction.error)
}
}
Store.prototype.del = function (key, cb) {
assert.equal(typeof key, 'string', 'Nanoidb.Store.del: key should be type string')
assert.equal(typeof cb, 'function', 'Nanoidb.Store.del: cb should be type function')
var transaction = this.db.transaction(this.name, 'readwrite')
var store = transaction.objectStore(this.name)
store.delete(key)
transaction.oncomplete = function (event) {
cb()
}
transaction.onerror = function () {
cb(transaction.error)
}
}
Store.prototype.batch = function () {
return new Batch(this.name, this.db)
}
function Batch (name, db) {
this.name = name
this.db = db
this.tx = this.db.transaction(this.name, 'readwrite')
this.store = this.tx.objectStore(this.name)
var self = this
this.tx.oncomplete = function (event) {
assert.ok(self.cb, 'Nanoidb.Batch: no callback found; did you forget to call .flush()?')
self.cb()
}
this.tx.onerror = function () {
assert.ok(self.cb, 'Nanoidb.Batch: no callback found; did you forget to call .flush()?')
self.cb(self.tx.error)
}
}
Batch.prototype.put = function (key, val) {
assert.equal(typeof key, 'string', 'Nanoidb.Batch.put: key should be type string')
assert.notEqual(typeof val, 'undefined', 'Nanoidb.Batch.put: val should not be type undefined')
this.store.put(val, key)
return this
}
Batch.prototype.del = function (key) {
assert.equal(typeof key, 'string', 'Nanoidb.Batch.del: key should be type string')
this.store.delete(key)
return this
}
Batch.prototype.flush = function (cb) {
assert.equal(typeof cb, 'function', 'Nanoidb.Batch.flush: cb should be type function')
this.cb = cb
}