-
Notifications
You must be signed in to change notification settings - Fork 807
/
idb-helper.js
executable file
·159 lines (142 loc) · 3.94 KB
/
idb-helper.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
/*
Copyright 2016 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/* eslint-disable require-jsdoc */
import idb from 'idb';
/**
* A wrapper to store for an IDB connection to a particular ObjectStore.
*
* @private
*/
class IDBHelper {
constructor(name, version, storeName) {
if (name == undefined || version == undefined || storeName == undefined) {
throw Error('name, version, storeName must be passed to the ' +
'constructor.');
}
this._name = name;
this._version = version;
this._storeName = storeName;
}
/**
* Returns a promise that resolves with an open connection to IndexedDB,
* either existing or newly opened.
*
* @private
* @return {Promise<DB>}
*/
_getDb() {
if (this._dbPromise) {
return this._dbPromise;
}
this._dbPromise = idb.open(this._name, this._version, (upgradeDB) => {
upgradeDB.createObjectStore(this._storeName);
})
.then((db) => {
return db;
});
return this._dbPromise;
}
close() {
if (!this._dbPromise) {
return;
}
return this._dbPromise
.then((db) => {
db.close();
this._dbPromise = null;
});
}
/**
* Wrapper on top of the idb wrapper, which simplifies saving the key/value
* pair to the object store.
* Returns a Promise that fulfills when the transaction completes.
*
* @private
* @param {String} key
* @param {Object} value
* @return {Promise<T>}
*/
put(key, value) {
return this._getDb().then((db) => {
const tx = db.transaction(this._storeName, 'readwrite');
const objectStore = tx.objectStore(this._storeName);
objectStore.put(value, key);
return tx.complete;
});
}
/**
* Wrapper on top of the idb wrapper, which simplifies deleting an entry
* from the object store.
* Returns a Promise that fulfills when the transaction completes.
*
* @private
* @param {String} key
* @return {Promise<T>}
*/
delete(key) {
return this._getDb().then((db) => {
const tx = db.transaction(this._storeName, 'readwrite');
const objectStore = tx.objectStore(this._storeName);
objectStore.delete(key);
return tx.complete;
});
}
/**
* Wrapper on top of the idb wrapper, which simplifies getting a key's value
* from the object store.
* Returns a promise that fulfills with the value.
*
* @private
* @param {String} key
* @return {Promise<Object>}
*/
get(key) {
return this._getDb().then((db) => {
return db.transaction(this._storeName)
.objectStore(this._storeName)
.get(key);
});
}
/**
* Wrapper on top of the idb wrapper, which simplifies getting all the values
* in an object store.
* Returns a promise that fulfills with all the values.
*
* @private
* @return {Promise<Array<Object>>}
*/
getAllValues() {
return this._getDb().then((db) => {
return db.transaction(this._storeName)
.objectStore(this._storeName)
.getAll();
});
}
/**
* Wrapper on top of the idb wrapper, which simplifies getting all the keys
* in an object store.
* Returns a promise that fulfills with all the keys.
*
* @private
* @param {String} storeName
* @return {Promise<Array<Object>>}
*/
getAllKeys() {
return this._getDb().then((db) => {
return db.transaction(this._storeName)
.objectStore(this._storeName)
.getAllKeys();
});
}
}
export default IDBHelper;