-
Notifications
You must be signed in to change notification settings - Fork 798
/
DBWrapper.ts
323 lines (291 loc) 路 9.96 KB
/
DBWrapper.ts
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
Copyright 2018 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
import '../_version.js';
type IDBObjectStoreMethods = 'get' | 'count' | 'getKey' | 'getAll' |
'getAllKeys' | 'add' | 'put' | 'clear' | 'delete';
type Query = IDBValidKey | IDBKeyRange | null;
interface DBWrapperOptions {
onupgradeneeded?: (event: IDBVersionChangeEvent) => any;
onversionchange?: (event: IDBVersionChangeEvent) => any;
}
interface GetAllMatchingOptions {
index?: string,
query?: Query,
direction?: IDBCursorDirection,
count?: number,
includeKeys?: boolean,
}
/**
* A class that wraps common IndexedDB functionality in a promise-based API.
* It exposes all the underlying power and functionality of IndexedDB, but
* wraps the most commonly used features in a way that's much simpler to use.
*
* @private
*/
export class DBWrapper {
private _name: string;
private _version: number;
private _onupgradeneeded?: DBWrapperOptions['onupgradeneeded'];
private _onversionchange: DBWrapperOptions['onversionchange'];
private _db: IDBDatabase | null = null;
// The following IDBObjectStore methods are shadowed on this class.
add?: Function;
clear?: Function;
count?: Function;
delete?: Function;
get?: Function;
put?: Function;
OPEN_TIMEOUT?: number;
/**
* @param {string} name
* @param {number} version
* @param {Object=} [callback]
* @param {!Function} [callbacks.onupgradeneeded]
* @param {!Function} [callbacks.onversionchange] Defaults to
* DBWrapper.prototype._onversionchange when not specified.
* @private
*/
constructor(name: string, version: number, {
onupgradeneeded,
onversionchange,
} : DBWrapperOptions = {}) {
this._name = name;
this._version = version;
this._onupgradeneeded = onupgradeneeded;
this._onversionchange = onversionchange || (() => this.close());
}
/**
* Returns the IDBDatabase instance (not normally needed).
* @return {IDBDatabase|undefined}
*
* @private
*/
get db() : IDBDatabase | null {
return this._db;
}
/**
* Opens a connected to an IDBDatabase, invokes any onupgradedneeded
* callback, and added an onversionchange callback to the database.
*
* @return {IDBDatabase}
* @private
*/
async open() {
if (this._db) return;
this._db = await new Promise((resolve, reject) => {
// This flag is flipped to true if the timeout callback runs prior
// to the request failing or succeeding. Note: we use a timeout instead
// of an onblocked handler since there are cases where onblocked will
// never never run. A timeout better handles all possible scenarios:
// https://github.com/w3c/IndexedDB/issues/223
let openRequestTimedOut = false;
setTimeout(() => {
openRequestTimedOut = true;
reject(new Error('The open request was blocked and timed out'));
}, this.OPEN_TIMEOUT);
const openRequest = indexedDB.open(this._name, this._version);
openRequest.onerror = () => reject(openRequest.error);
openRequest.onupgradeneeded = (evt: IDBVersionChangeEvent) => {
if (openRequestTimedOut) {
openRequest.transaction!.abort();
openRequest.result.close();
} else if (typeof this._onupgradeneeded === 'function') {
this._onupgradeneeded(evt);
}
};
openRequest.onsuccess = () => {
const db = openRequest.result;
if (openRequestTimedOut) {
db.close();
} else {
db.onversionchange = this._onversionchange!.bind(this);
resolve(db);
}
};
});
return this;
}
/**
* Polyfills the native `getKey()` method. Note, this is overridden at
* runtime if the browser supports the native method.
*
* @param {string} storeName
* @param {*} query
* @return {Array}
* @private
*/
async getKey(storeName: string, query: Query) {
return (await this.getAllKeys(storeName, query, 1))[0];
}
/**
* Polyfills the native `getAll()` method. Note, this is overridden at
* runtime if the browser supports the native method.
*
* @param {string} storeName
* @param {*} query
* @param {number} count
* @return {Array}
* @private
*/
async getAll(storeName: string, query?: Query, count?: number) {
return await this.getAllMatching(storeName, {query, count});
}
/**
* Polyfills the native `getAllKeys()` method. Note, this is overridden at
* runtime if the browser supports the native method.
*
* @param {string} storeName
* @param {*} query
* @param {number} count
* @return {Array}
* @private
*/
async getAllKeys(storeName: string, query: Query, count: number) {
const entries = await this.getAllMatching(
storeName, {query, count, includeKeys: true})
return entries.map((entry: IDBCursor) => entry.key);
}
/**
* Supports flexible lookup in an object store by specifying an index,
* query, direction, and count. This method returns an array of objects
* with the signature .
*
* @param {string} storeName
* @param {Object} [opts]
* @param {string} [opts.index] The index to use (if specified).
* @param {*} [opts.query]
* @param {IDBCursorDirection} [opts.direction]
* @param {number} [opts.count] The max number of results to return.
* @param {boolean} [opts.includeKeys] When true, the structure of the
* returned objects is changed from an array of values to an array of
* objects in the form {key, primaryKey, value}.
* @return {Array}
* @private
*/
async getAllMatching(storeName: string, {
index,
query = null, // IE/Edge errors if query === `undefined`.
direction = 'next',
count,
includeKeys = false,
} : GetAllMatchingOptions = {}) : Promise<Array<IDBCursor | any>> {
return await this.transaction([storeName], 'readonly', (txn, done) => {
const store = txn.objectStore(storeName);
const target = index ? store.index(index) : store;
const results: any[] = [];
const request = target.openCursor(query, direction);
request.onsuccess = () => {
const cursor = request.result;
if (cursor) {
results.push(includeKeys ? cursor : cursor.value);
if (count && results.length >= count) {
done(results);
} else {
cursor.continue();
}
} else {
done(results);
}
};
});
}
/**
* Accepts a list of stores, a transaction type, and a callback and
* performs a transaction. A promise is returned that resolves to whatever
* value the callback chooses. The callback holds all the transaction logic
* and is invoked with two arguments:
* 1. The IDBTransaction object
* 2. A `done` function, that's used to resolve the promise when
* when the transaction is done, if passed a value, the promise is
* resolved to that value.
*
* @param {Array<string>} storeNames An array of object store names
* involved in the transaction.
* @param {string} type Can be `readonly` or `readwrite`.
* @param {!Function} callback
* @return {*} The result of the transaction ran by the callback.
* @private
*/
async transaction(
storeNames: string | string[],
type: IDBTransactionMode,
callback: (txn: IDBTransaction, done: Function) => void,
) : Promise<any> {
await this.open();
return await new Promise((resolve, reject) => {
const txn = this._db!.transaction(storeNames, type);
txn.onabort = () => reject(txn.error);
txn.oncomplete = () => resolve();
callback(txn, (value: any) => resolve(value));
});
}
/**
* Delegates async to a native IDBObjectStore method.
*
* @param {string} method The method name.
* @param {string} storeName The object store name.
* @param {string} type Can be `readonly` or `readwrite`.
* @param {...*} args The list of args to pass to the native method.
* @return {*} The result of the transaction.
* @private
*/
async _call(
method: IDBObjectStoreMethods,
storeName: string,
type: IDBTransactionMode,
...args: any[]
) {
const callback = (txn: IDBTransaction, done: Function) => {
const objStore = txn.objectStore(storeName)
const request = <IDBRequest> objStore[method].apply(objStore, args);
request.onsuccess = () => done(request.result);
};
return await this.transaction([storeName], type, callback);
}
/**
* Closes the connection opened by `DBWrapper.open()`. Generally this method
* doesn't need to be called since:
* 1. It's usually better to keep a connection open since opening
* a new connection is somewhat slow.
* 2. Connections are automatically closed when the reference is
* garbage collected.
* The primary use case for needing to close a connection is when another
* reference (typically in another tab) needs to upgrade it and would be
* blocked by the current, open connection.
*
* @private
*/
close() {
if (this._db) {
this._db.close();
this._db = null;
}
}
}
// Exposed on the prototype to let users modify the default timeout on a
// per-instance or global basis.
DBWrapper.prototype.OPEN_TIMEOUT = 2000;
// Wrap native IDBObjectStore methods according to their mode.
const methodsToWrap = {
readonly: ['get', 'count', 'getKey', 'getAll', 'getAllKeys'],
readwrite: ['add', 'put', 'clear', 'delete'],
};
for (const [mode, methods] of Object.entries(methodsToWrap)) {
for (const method of methods) {
if (method in IDBObjectStore.prototype) {
// Don't use arrow functions here since we're outside of the class.
DBWrapper.prototype[<IDBObjectStoreMethods> method] =
async function(storeName: string, ...args: any[]) {
return await this._call(
<IDBObjectStoreMethods> method,
storeName,
<IDBTransactionMode> mode,
...args);
};
}
}
}