-
Notifications
You must be signed in to change notification settings - Fork 146
/
indexedDB.ts
87 lines (78 loc) · 2.39 KB
/
indexedDB.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { del, get, keys, set, Store } from 'idb-keyval';
/**
* API for IndexedDB Util
* This API uses IndexedDB to store our data and throws an exception in case indexedDB is not available
*
*/
export interface IndexedDBAPI {
/**
* @param {string} key - sets the value for this key
* @param {any} value - sets value in indexedDB for specified key
* @returns Promise - returns a promise with true/false for success/failure
*/
setItem(key: string, value: any): Promise<boolean>;
/**
* @param {string} key
* @param {string} storeName
* @returns Promise - returns a promise that resolves to the value for key
*/
getItem(key: string): Promise<any>;
/**
* @param {string} key
* @param {string} storeName
* @returns Promise - resolves to true/false for success/failure in removing item
*/
removeItem(key: string): Promise<boolean>;
/**
* @param {string} storeName
* @returns Promise
* returns a promise that resolves to provide the size of indexed db
*/
getSize(): Promise<number>;
}
/**
* Implementation for IndexedDBAPI defined above
*
* provides following functions
* getItem, setItem, removeItem, getSize
*
* window.indexedDB is the param that tells us about availabiltiy
*
* Read more on IndexedDB: https://developers.google.com/web/ilt/pwa/working-with-indexeddb
*/
export class IndexedDBUtil implements IndexedDBAPI {
constructor(private readonly store: Store) {}
public async getItem(key: string): Promise<any> {
try {
return await get<any>(key, this.store);
} catch (error) {
return undefined;
}
}
public async setItem(key: string, value: any): Promise<boolean> {
try {
await set(key, value, this.store);
return true;
} catch (error) {
return false;
}
}
public async removeItem(key: string): Promise<boolean> {
try {
await del(key, this.store);
return true;
} catch (error) {
return false;
}
}
public async getSize(): Promise<number> {
try {
const keyArray = await keys(this.store);
return keyArray.length;
} catch (error) {
return 0;
}
}
}