-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimple-indexeddb.js
222 lines (222 loc) · 8.85 KB
/
simple-indexeddb.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
// @ts-nocheck
/* Allows to resolve a promise outside the promise */
function makePromise() {
let resolve;
let reject;
const promise = new Promise((res, rej) => {
resolve = res;
reject = rej;
});
return { resolve, reject, promise };
}
/**
* @class IndexeDBObjectStore
* A simple promise based wrapper for an indexedDB object store
* @param {string} dbname - The name of the database you want to open
* @param {string} dbname - The name of the objectstore you want to open
* @param {CreateOptions} options - If the object store does not exist we attempt to create it. Here you can pass autoIncrement and or keyPath. You don't need this if you're sure the objectStore already exists. For more info see [Structuring the database on mdn](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB#structuring_the_database
*
* You can have multiple databases each with multiple object stores. Each object store can hold key value pair objects.
* You can query these key value pairs using strings or using IDBKeyRange (https://developer.mozilla.org/en-US/docs/Web/API/IDBKeyRange)
*/
class IndexedDBObjectStore {
db;
dbname;
objectstorename;
constructor(dbname, objectstorename, createOptions) {
const { promise, reject, resolve } = makePromise();
let request = indexedDB.open(dbname, createOptions?.version);
request.onupgradeneeded = (event) => {
this.db = event.target.result;
if (!this.db.objectStoreNames.contains(objectstorename)) {
this.db.createObjectStore(objectstorename, createOptions);
}
// resolve(this);
};
request.onerror = reject;
request.onsuccess = (event) => {
this.db = event.target.result;
// turn on if you want db to automatically close on version change,
// if you leave it off tabs have to be reloaded before version change can happen or manual close
// this.db.onversionchange = () => this.db.close();
resolve(this);
};
this.dbname = dbname;
this.objectstorename = objectstorename;
return promise;
}
/**
* Get an object store and parse it as json (if it was text)
* @param {string} key
*/
getJson(key) {
const { promise, reject, resolve } = makePromise();
const transaction = this.db
.transaction([this.objectstorename])
.objectStore(this.objectstorename)
.get(key);
transaction.onsuccess = (event) => {
try {
let store = event.target.result;
if (typeof store === "undefined")
reject(`${key} is undefined`);
resolve(JSON.parse(store));
}
catch (e) {
reject(`Could not parse ${key} in object store: ${e}`);
}
};
return promise;
}
/**
* Get the value stored under a key or key range
* @param {IDBValidKey | IDBKeyRange} key
*/
get(key) {
const { promise, reject, resolve } = makePromise();
const transaction = this.db
.transaction([this.objectstorename])
.objectStore(this.objectstorename)
.get(key);
transaction.onsuccess = (event) => {
try {
resolve(event.target.result);
}
catch (e) {
reject(`Could not load ${name} store: ${e}`);
}
};
return promise;
}
/**
* Save data for a key in the store. Replaces data already there.
* Based on your [Key mode](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB#structuring_the_database) you should include a key or not.
* @param {any} data
* @param {IDBValidKey|undefined} key
*/
put(data, key) {
const { promise, reject, resolve } = makePromise();
const transaction = this.db
.transaction([this.objectstorename], "readwrite")
.objectStore(this.objectstorename)
.put(data, key);
transaction.onsuccess = (event) => resolve(event.target.result);
transaction.onerror = reject;
return promise;
}
/**
* Add new key value pair to the store.
* Based on your [Key mode](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB#structuring_the_database) you should include a key or not.
* @param {any} data - data to save
* @param {IDBValidKey|undefined} key - Key under which to save, can be undefined
*/
add(data, key) {
const { promise, reject, resolve } = makePromise();
const transaction = this.db
.transaction([this.objectstorename], "readwrite")
.objectStore(this.objectstorename)
.add(data, key);
transaction.onsuccess = (event) => resolve(event.target.result);
transaction.onerror = reject;
return promise;
}
/**
* Get all data in the object store. It is possible to query using
* [IDBKeyRange](https://developer.mozilla.org/en-US/docs/Web/API/IDBKeyRange)
* @param {undefined|IDBKeyRange} query - Optional query
* @param {undefined|number} count - Number of things to return
*/
getAll(query, count) {
const { promise, reject, resolve } = makePromise();
const transaction = this.db
.transaction([this.objectstorename])
.objectStore(this.objectstorename)
.getAll(query, count);
transaction.onsuccess = (event) => resolve(event.target.result);
transaction.onerror = reject;
return promise;
}
/**
* Clear the entire object store. **This removes all key value pairs in the object store**
*/
clear() {
const { promise, reject, resolve } = makePromise();
const transaction = this.db
.transaction([this.objectstorename], "readwrite")
.objectStore(this.objectstorename)
.clear();
transaction.onsuccess = (event) => resolve(event);
transaction.onerror = reject;
return promise;
}
/**
* Returns the number of key value pairs in the object store.
* You can also query count the results the query would get
* @param {undefined| IDBKeyRange | string} query
*/
count(query) {
const { promise, reject, resolve } = makePromise();
const transaction = this.db
.transaction([this.objectstorename])
.objectStore(this.objectstorename)
.count(query);
transaction.onsuccess = (event) => resolve(event.target.result);
transaction.onerror = reject;
return promise;
}
/**
* Get a indexed db request object for a certain key
* @param {IDBValidKey | IDBKeyRange} key
*/
getKey(key) {
const { promise, reject, resolve } = makePromise();
const transaction = this.db
.transaction([this.objectstorename])
.objectStore(this.objectstorename)
.getKey(key);
transaction.onsuccess = (event) => resolve(event.target.result);
transaction.onerror = reject;
return promise;
}
/**
* Retrieves record keys for all objects in the object store matching the specified parameter or all objects in the store if no parameters are given.
* If you don't give arguments it returns all keys
* @param {IDBKeyRange|IDBValidKey} query
* @param {undefined|number} count
*/
getAllKeys(query, count) {
const { promise, reject, resolve } = makePromise();
const transaction = this.db
.transaction([this.objectstorename])
.objectStore(this.objectstorename)
.getAllKeys(query, count);
transaction.onsuccess = (event) => resolve(event.target.result);
transaction.onerror = reject;
return promise;
}
/**
* Delete one specific key value pair from the object store.
* @param {IDBValidKey} key
*/
delete(key) {
const { promise, reject, resolve } = makePromise();
const transaction = this.db
.transaction([this.objectstorename], "readwrite")
.objectStore(this.objectstorename)
.delete(key);
transaction.onsuccess = (event) => resolve(event);
transaction.onerror = reject;
return promise;
}
/**
* Closes the database connection.
* The close() method of the IDBDatabase interface returns immediately and closes the connection in a separate thread.
* The connection is not actually closed until all transactions created using this connection are complete.
* No new transactions can be created for this connection once this method is called.
* Methods that create transactions throw an exception if a closing operation is pending.
*/
close() {
this.db.close();
}
}
export default IndexedDBObjectStore;