-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
indexeddbV2.js
265 lines (236 loc) · 8.43 KB
/
indexeddbV2.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/* eslint-env browser */
/**
* Helpers to work with IndexedDB.
* This is an experimental implementation using Pledge instead of Promise.
*
* @experimental
*
* @module indexeddbv2
*/
import * as pledge from './pledge.js'
/* c8 ignore start */
/**
* IDB Request to Pledge transformer
*
* @param {pledge.PledgeInstance<any>} p
* @param {IDBRequest} request
*/
export const bindPledge = (p, request) => {
// @ts-ignore
request.onerror = event => p.cancel(event.target.error)
// @ts-ignore
request.onsuccess = event => p.resolve(event.target.result)
}
/**
* @param {string} name
* @param {function(IDBDatabase):any} initDB Called when the database is first created
* @return {pledge.PledgeInstance<IDBDatabase>}
*/
export const openDB = (name, initDB) => {
/**
* @type {pledge.PledgeInstance<IDBDatabase>}
*/
const p = pledge.create()
const request = indexedDB.open(name)
/**
* @param {any} event
*/
request.onupgradeneeded = event => initDB(event.target.result)
/**
* @param {any} event
*/
request.onerror = event => p.cancel(event.target.error)
/**
* @param {any} event
*/
request.onsuccess = event => {
/**
* @type {IDBDatabase}
*/
const db = event.target.result
db.onversionchange = () => { db.close() }
p.resolve(db)
}
return p
}
/**
* @param {pledge.Pledge<string>} name
* @return {pledge.PledgeInstance<void>}
*/
export const deleteDB = name => pledge.createWithDependencies((p, name) => bindPledge(p, indexedDB.deleteDatabase(name)), name)
/**
* @param {IDBDatabase} db
* @param {Array<Array<string>|Array<string|IDBObjectStoreParameters|undefined>>} definitions
*/
export const createStores = (db, definitions) => definitions.forEach(d =>
// @ts-ignore
db.createObjectStore.apply(db, d)
)
/**
* @param {pledge.Pledge<IDBDatabase>} db
* @param {pledge.Pledge<Array<string>>} stores
* @param {"readwrite"|"readonly"} [access]
* @return {pledge.Pledge<Array<IDBObjectStore>>}
*/
export const transact = (db, stores, access = 'readwrite') => pledge.createWithDependencies((p, db, stores) => {
const transaction = db.transaction(stores, access)
p.resolve(stores.map(store => getStore(transaction, store)))
}, db, stores)
/**
* @param {IDBObjectStore} store
* @param {pledge.Pledge<IDBKeyRange|undefined>} [range]
* @return {pledge.PledgeInstance<number>}
*/
export const count = (store, range) => pledge.createWithDependencies((p, store, range) => bindPledge(p, store.count(range)), store, range)
/**
* @param {pledge.Pledge<IDBObjectStore>} store
* @param {pledge.Pledge<String | number | ArrayBuffer | Date | Array<any>>} key
* @return {pledge.PledgeInstance<String | number | ArrayBuffer | Date | Array<any>>}
*/
export const get = (store, key) => pledge.createWithDependencies((p, store, key) => bindPledge(p, store.get(key)), store, key)
/**
* @param {pledge.Pledge<IDBObjectStore>} store
* @param {String | number | ArrayBuffer | Date | IDBKeyRange | Array<any> } key
*/
export const del = (store, key) => pledge.createWithDependencies((p, store, key) => bindPledge(p, store.delete(key)), store, key)
/**
* @param {pledge.Pledge<IDBObjectStore>} store
* @param {String | number | ArrayBuffer | Date | boolean} item
* @param {String | number | ArrayBuffer | Date | Array<any>} [key]
*/
export const put = (store, item, key) => pledge.createWithDependencies((p, store, item, key) => bindPledge(p, store.put(item, key)), store, item, key)
/**
* @param {pledge.Pledge<IDBObjectStore>} store
* @param {String | number | ArrayBuffer | Date | boolean} item
* @param {String | number | ArrayBuffer | Date | Array<any>} key
* @return {pledge.PledgeInstance<any>}
*/
export const add = (store, item, key) => pledge.createWithDependencies((p, store, item, key) => bindPledge(p, store.add(item, key)), store, item, key)
/**
* @param {pledge.Pledge<IDBObjectStore>} store
* @param {String | number | ArrayBuffer | Date} item
* @return {pledge.PledgeInstance<number>} Returns the generated key
*/
export const addAutoKey = (store, item) => pledge.createWithDependencies((p, store, item) => bindPledge(p, store.add(item)), store, item)
/**
* @param {pledge.Pledge<IDBObjectStore>} store
* @param {IDBKeyRange} [range]
* @param {number} [limit]
* @return {pledge.PledgeInstance<Array<any>>}
*/
export const getAll = (store, range, limit) => pledge.createWithDependencies((p, store, range, limit) => bindPledge(p, store.getAll(range, limit)), store, range, limit)
/**
* @param {pledge.Pledge<IDBObjectStore>} store
* @param {IDBKeyRange} [range]
* @param {number} [limit]
* @return {pledge.PledgeInstance<Array<any>>}
*/
export const getAllKeys = (store, range, limit) => pledge.createWithDependencies((p, store, range, limit) => bindPledge(p, store.getAllKeys(range, limit)), store, range, limit)
/**
* @param {IDBObjectStore} store
* @param {IDBKeyRange|null} query
* @param {'next'|'prev'|'nextunique'|'prevunique'} direction
* @return {pledge.PledgeInstance<any>}
*/
export const queryFirst = (store, query, direction) => {
/**
* @type {any}
*/
let first = null
return iterateKeys(store, query, key => {
first = key
return false
}, direction).map(() => first)
}
/**
* @param {IDBObjectStore} store
* @param {IDBKeyRange?} [range]
* @return {pledge.PledgeInstance<any>}
*/
export const getLastKey = (store, range = null) => queryFirst(store, range, 'prev')
/**
* @param {IDBObjectStore} store
* @param {IDBKeyRange?} [range]
* @return {pledge.PledgeInstance<any>}
*/
export const getFirstKey = (store, range = null) => queryFirst(store, range, 'next')
/**
* @typedef KeyValuePair
* @type {Object}
* @property {any} k key
* @property {any} v Value
*/
/**
* @param {pledge.Pledge<IDBObjectStore>} store
* @param {pledge.Pledge<IDBKeyRange|undefined>} [range]
* @param {pledge.Pledge<number|undefined>} [limit]
* @return {pledge.PledgeInstance<Array<KeyValuePair>>}
*/
export const getAllKeysValues = (store, range, limit) => pledge.createWithDependencies((p, store, range, limit) => {
pledge.all([getAllKeys(store, range, limit), getAll(store, range, limit)]).map(([ks, vs]) => ks.map((k, i) => ({ k, v: vs[i] }))).whenResolved(p.resolve.bind(p))
}, store, range, limit)
/**
* @param {pledge.PledgeInstance<void>} p
* @param {any} request
* @param {function(IDBCursorWithValue):void|boolean|Promise<void|boolean>} f
*/
const iterateOnRequest = (p, request, f) => {
request.onerror = p.cancel.bind(p)
/**
* @param {any} event
*/
request.onsuccess = async event => {
const cursor = event.target.result
if (cursor === null || (await f(cursor)) === false) {
p.resolve(undefined)
return
}
cursor.continue()
}
}
/**
* Iterate on keys and values
* @param {pledge.Pledge<IDBObjectStore>} store
* @param {pledge.Pledge<IDBKeyRange|null>} keyrange
* @param {function(any,any):void|boolean|Promise<void|boolean>} f Callback that receives (value, key)
* @param {'next'|'prev'|'nextunique'|'prevunique'} direction
*/
export const iterate = (store, keyrange, f, direction = 'next') => pledge.createWithDependencies((p, store, keyrange) => {
iterateOnRequest(p, store.openCursor(keyrange, direction), cursor => f(cursor.value, cursor.key))
}, store, keyrange)
/**
* Iterate on the keys (no values)
*
* @param {pledge.Pledge<IDBObjectStore>} store
* @param {pledge.Pledge<IDBKeyRange|null>} keyrange
* @param {function(any):void|boolean|Promise<void|boolean>} f callback that receives the key
* @param {'next'|'prev'|'nextunique'|'prevunique'} direction
*/
export const iterateKeys = (store, keyrange, f, direction = 'next') => pledge.createWithDependencies((p, store, keyrange) => {
iterateOnRequest(p, store.openKeyCursor(keyrange, direction), cursor => f(cursor.key))
}, store, keyrange)
/**
* Open store from transaction
* @param {IDBTransaction} t
* @param {String} store
* @returns {IDBObjectStore}
*/
export const getStore = (t, store) => t.objectStore(store)
/**
* @param {any} lower
* @param {any} upper
* @param {boolean} lowerOpen
* @param {boolean} upperOpen
*/
export const createIDBKeyRangeBound = (lower, upper, lowerOpen, upperOpen) => IDBKeyRange.bound(lower, upper, lowerOpen, upperOpen)
/**
* @param {any} upper
* @param {boolean} upperOpen
*/
export const createIDBKeyRangeUpperBound = (upper, upperOpen) => IDBKeyRange.upperBound(upper, upperOpen)
/**
* @param {any} lower
* @param {boolean} lowerOpen
*/
export const createIDBKeyRangeLowerBound = (lower, lowerOpen) => IDBKeyRange.lowerBound(lower, lowerOpen)
/* c8 ignore stop */