ngx-indexed-db 9.1.2 → 9.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +41 -37
- package/bundles/ngx-indexed-db.umd.js +214 -163
- package/bundles/ngx-indexed-db.umd.js.map +1 -1
- package/esm2015/lib/ngx-indexed-db.js +25 -1
- package/esm2015/lib/ngx-indexed-db.meta.js +1 -1
- package/esm2015/lib/ngx-indexed-db.service.js +173 -141
- package/esm2015/utils/index.js +1 -5
- package/fesm2015/ngx-indexed-db.js +194 -143
- package/fesm2015/ngx-indexed-db.js.map +1 -1
- package/lib/ngx-indexed-db.d.ts +2 -0
- package/lib/ngx-indexed-db.meta.d.ts +1 -1
- package/lib/ngx-indexed-db.service.d.ts +10 -6
- package/ngx-indexed-db.metadata.json +1 -1
- package/package.json +1 -1
- package/utils/index.d.ts +2 -2
package/esm2015/utils/index.js
CHANGED
|
@@ -12,7 +12,6 @@ export function validateBeforeTransaction(db, storeName, reject) {
|
|
|
12
12
|
export function createTransaction(db, options) {
|
|
13
13
|
const trans = db.transaction(options.storeName, options.dbMode);
|
|
14
14
|
trans.onerror = options.error;
|
|
15
|
-
trans.oncomplete = options.complete;
|
|
16
15
|
trans.onabort = options.abort;
|
|
17
16
|
return trans;
|
|
18
17
|
}
|
|
@@ -23,12 +22,9 @@ export function optionsGenerator(type, storeName, reject, resolve) {
|
|
|
23
22
|
error: (e) => {
|
|
24
23
|
reject(e);
|
|
25
24
|
},
|
|
26
|
-
complete: (e) => {
|
|
27
|
-
resolve(e);
|
|
28
|
-
},
|
|
29
25
|
abort: (e) => {
|
|
30
26
|
reject(e);
|
|
31
27
|
},
|
|
32
28
|
};
|
|
33
29
|
}
|
|
34
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
30
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6W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3RTdG9yZSBkb2VzIG5vdCBleGlzdHM6ICR7c3RvcmVOYW1lfWApO1xuICB9XG59XG5cbmV4cG9ydCBmdW5jdGlvbiBjcmVhdGVUcmFuc2FjdGlvbihkYjogSURCRGF0YWJhc2UsIG9wdGlvbnM6IE9wdGlvbnMpOiBJREJUcmFuc2FjdGlvbiB7XG4gIGNvbnN0IHRyYW5zOiBJREJUcmFuc2FjdGlvbiA9IGRiLnRyYW5zYWN0aW9uKG9wdGlvbnMuc3RvcmVOYW1lLCBvcHRpb25zLmRiTW9kZSk7XG4gIHRyYW5zLm9uZXJyb3IgPSBvcHRpb25zLmVycm9yO1xuICB0cmFucy5vbmFib3J0ID0gb3B0aW9ucy5hYm9ydDtcbiAgcmV0dXJuIHRyYW5zO1xufVxuXG5leHBvcnQgZnVuY3Rpb24gb3B0aW9uc0dlbmVyYXRvcihcbiAgdHlwZTogYW55LFxuICBzdG9yZU5hbWU6IGFueSxcbiAgcmVqZWN0OiAocmVhc29uPzogYW55KSA9PiB2b2lkLFxuICByZXNvbHZlPzogKGU6IGFueSkgPT4gdm9pZFxuKTogT3B0aW9ucyB7XG4gIHJldHVybiB7XG4gICAgc3RvcmVOYW1lLFxuICAgIGRiTW9kZTogdHlwZSxcbiAgICBlcnJvcjogKGU6IEV2ZW50KSA9PiB7XG4gICAgICByZWplY3QoZSk7XG4gICAgfSxcbiAgICBhYm9ydDogKGU6IEV2ZW50KSA9PiB7XG4gICAgICByZWplY3QoZSk7XG4gICAgfSxcbiAgfTtcbn1cbiJdfQ==
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { InjectionToken, Injectable, Inject, PLATFORM_ID, NgModule } from '@angular/core';
|
|
2
2
|
import { isPlatformBrowser, CommonModule } from '@angular/common';
|
|
3
3
|
import { __awaiter } from 'tslib';
|
|
4
|
-
import { from, Subject } from 'rxjs';
|
|
4
|
+
import { Observable, from, combineLatest, Subject } from 'rxjs';
|
|
5
5
|
import { take } from 'rxjs/operators';
|
|
6
6
|
|
|
7
7
|
function openDatabase(indexedDB, dbName, version, upgradeCallback) {
|
|
@@ -56,6 +56,29 @@ function CreateObjectStore(indexedDB, dbName, version, storeSchemas, migrationFa
|
|
|
56
56
|
e.target.result.close();
|
|
57
57
|
};
|
|
58
58
|
}
|
|
59
|
+
function DeleteObjectStore(dbName, version, storeName) {
|
|
60
|
+
if (!dbName || !version || !storeName) {
|
|
61
|
+
throw Error('Params: "dbName", "version", "storeName" are mandatory.');
|
|
62
|
+
}
|
|
63
|
+
return new Observable((obs) => {
|
|
64
|
+
try {
|
|
65
|
+
const newVersion = version + 1;
|
|
66
|
+
const request = indexedDB.open(dbName, newVersion);
|
|
67
|
+
request.onupgradeneeded = (event) => {
|
|
68
|
+
const database = event.target.result;
|
|
69
|
+
database.deleteObjectStore(storeName);
|
|
70
|
+
database.close();
|
|
71
|
+
console.log('onupgradeneeded');
|
|
72
|
+
obs.next(true);
|
|
73
|
+
obs.complete();
|
|
74
|
+
};
|
|
75
|
+
request.onerror = (e) => obs.error(e);
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
obs.error(error);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
}
|
|
59
82
|
|
|
60
83
|
function validateStoreName(db, storeName) {
|
|
61
84
|
return db.objectStoreNames.contains(storeName);
|
|
@@ -71,7 +94,6 @@ function validateBeforeTransaction(db, storeName, reject) {
|
|
|
71
94
|
function createTransaction(db, options) {
|
|
72
95
|
const trans = db.transaction(options.storeName, options.dbMode);
|
|
73
96
|
trans.onerror = options.error;
|
|
74
|
-
trans.oncomplete = options.complete;
|
|
75
97
|
trans.onabort = options.abort;
|
|
76
98
|
return trans;
|
|
77
99
|
}
|
|
@@ -82,9 +104,6 @@ function optionsGenerator(type, storeName, reject, resolve) {
|
|
|
82
104
|
error: (e) => {
|
|
83
105
|
reject(e);
|
|
84
106
|
},
|
|
85
|
-
complete: (e) => {
|
|
86
|
-
resolve(e);
|
|
87
|
-
},
|
|
88
107
|
abort: (e) => {
|
|
89
108
|
reject(e);
|
|
90
109
|
},
|
|
@@ -147,21 +166,23 @@ class NgxIndexedDBService {
|
|
|
147
166
|
* @param key The optional key for the entry
|
|
148
167
|
*/
|
|
149
168
|
add(storeName, value, key) {
|
|
150
|
-
return
|
|
169
|
+
return new Observable((obs) => {
|
|
151
170
|
openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
|
|
152
171
|
.then((db) => {
|
|
153
|
-
const transaction = createTransaction(db, optionsGenerator(DBMode.readwrite, storeName,
|
|
172
|
+
const transaction = createTransaction(db, optionsGenerator(DBMode.readwrite, storeName, obs.error));
|
|
154
173
|
const objectStore = transaction.objectStore(storeName);
|
|
155
|
-
const request = Boolean(key)
|
|
156
|
-
|
|
157
|
-
: objectStore.add(value);
|
|
158
|
-
request.onsuccess = (evt) => {
|
|
174
|
+
const request = Boolean(key) ? objectStore.add(value, key) : objectStore.add(value);
|
|
175
|
+
request.onsuccess = (evt) => __awaiter(this, void 0, void 0, function* () {
|
|
159
176
|
const result = evt.target.result;
|
|
160
|
-
|
|
161
|
-
|
|
177
|
+
const getRequest = objectStore.get(result);
|
|
178
|
+
getRequest.onsuccess = (event) => {
|
|
179
|
+
obs.next(event.target.result);
|
|
180
|
+
obs.complete();
|
|
181
|
+
};
|
|
182
|
+
});
|
|
162
183
|
})
|
|
163
|
-
.catch((
|
|
164
|
-
})
|
|
184
|
+
.catch((error) => obs.error(error));
|
|
185
|
+
});
|
|
165
186
|
}
|
|
166
187
|
/**
|
|
167
188
|
* Adds new entries in the store and returns its key
|
|
@@ -169,51 +190,54 @@ class NgxIndexedDBService {
|
|
|
169
190
|
* @param values The entries to be added containing optional key attribute
|
|
170
191
|
*/
|
|
171
192
|
bulkAdd(storeName, values) {
|
|
172
|
-
const promises =
|
|
193
|
+
const promises = new Promise((resolve, reject) => {
|
|
194
|
+
openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
|
|
195
|
+
.then((db) => {
|
|
196
|
+
const transaction = createTransaction(db, optionsGenerator(DBMode.readwrite, storeName, resolve, reject));
|
|
197
|
+
const objectStore = transaction.objectStore(storeName);
|
|
198
|
+
const results = values.map((value) => {
|
|
199
|
+
return new Promise((resolve1, reject1) => {
|
|
200
|
+
const key = value.key;
|
|
201
|
+
delete value.key;
|
|
202
|
+
const request = Boolean(key)
|
|
203
|
+
? objectStore.add(value, key)
|
|
204
|
+
: objectStore.add(value);
|
|
205
|
+
request.onsuccess = (evt) => {
|
|
206
|
+
const result = evt.target.result;
|
|
207
|
+
resolve1(result);
|
|
208
|
+
};
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
resolve(Promise.all(results));
|
|
212
|
+
}).catch((reason) => reject(reason));
|
|
213
|
+
});
|
|
214
|
+
return from(promises);
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Delete entries in the store and returns current entries in the store
|
|
218
|
+
* @param storeName The name of the store to add the item
|
|
219
|
+
* @param keys The keys to be deleted
|
|
220
|
+
*/
|
|
221
|
+
bulkDelete(storeName, keys) {
|
|
222
|
+
const promises = keys.map((key) => {
|
|
173
223
|
return new Promise((resolve, reject) => {
|
|
174
224
|
openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
|
|
175
225
|
.then((db) => {
|
|
176
226
|
const transaction = createTransaction(db, optionsGenerator(DBMode.readwrite, storeName, reject, resolve));
|
|
177
227
|
const objectStore = transaction.objectStore(storeName);
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
resolve(result);
|
|
228
|
+
objectStore.delete(key);
|
|
229
|
+
transaction.oncomplete = () => {
|
|
230
|
+
this.getAll(storeName)
|
|
231
|
+
.pipe(take(1))
|
|
232
|
+
.subscribe((newValues) => {
|
|
233
|
+
resolve(newValues);
|
|
234
|
+
});
|
|
186
235
|
};
|
|
187
236
|
})
|
|
188
237
|
.catch((reason) => reject(reason));
|
|
189
238
|
});
|
|
190
239
|
});
|
|
191
|
-
return from(Promise.
|
|
192
|
-
}
|
|
193
|
-
/**
|
|
194
|
-
* Adds new entry in the store and returns the item that was added
|
|
195
|
-
* @param storeName The name of the store to add the item
|
|
196
|
-
* @param value The entry to be added
|
|
197
|
-
* @param key The optional key for the entry
|
|
198
|
-
*/
|
|
199
|
-
addItem(storeName, value, key) {
|
|
200
|
-
return from(new Promise((resolve, reject) => {
|
|
201
|
-
openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
|
|
202
|
-
.then((db) => {
|
|
203
|
-
const transaction = createTransaction(db, optionsGenerator(DBMode.readwrite, storeName, reject, resolve));
|
|
204
|
-
const objectStore = transaction.objectStore(storeName);
|
|
205
|
-
const hasKey = Boolean(key);
|
|
206
|
-
const request = hasKey ? objectStore.add(value, key) : objectStore.add(value);
|
|
207
|
-
request.onsuccess = (evt) => {
|
|
208
|
-
const result = evt.target.result;
|
|
209
|
-
const itemKey = hasKey ? key : result;
|
|
210
|
-
this.getByKey(storeName, itemKey).subscribe((newValue) => {
|
|
211
|
-
resolve(newValue);
|
|
212
|
-
});
|
|
213
|
-
};
|
|
214
|
-
})
|
|
215
|
-
.catch((reason) => reject(reason));
|
|
216
|
-
}));
|
|
240
|
+
return from(Promise.all(promises));
|
|
217
241
|
}
|
|
218
242
|
/**
|
|
219
243
|
* Returns entry by key.
|
|
@@ -221,21 +245,22 @@ class NgxIndexedDBService {
|
|
|
221
245
|
* @param key The entry key
|
|
222
246
|
*/
|
|
223
247
|
getByKey(storeName, key) {
|
|
224
|
-
return
|
|
248
|
+
return new Observable((obs) => {
|
|
225
249
|
openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
|
|
226
250
|
.then((db) => {
|
|
227
|
-
const transaction = createTransaction(db, optionsGenerator(DBMode.readonly, storeName,
|
|
251
|
+
const transaction = createTransaction(db, optionsGenerator(DBMode.readonly, storeName, obs.error));
|
|
228
252
|
const objectStore = transaction.objectStore(storeName);
|
|
229
253
|
const request = objectStore.get(key);
|
|
230
254
|
request.onsuccess = (event) => {
|
|
231
|
-
|
|
255
|
+
obs.next(event.target.result);
|
|
256
|
+
obs.complete();
|
|
232
257
|
};
|
|
233
258
|
request.onerror = (event) => {
|
|
234
|
-
|
|
259
|
+
obs.error(event);
|
|
235
260
|
};
|
|
236
261
|
})
|
|
237
|
-
.catch((
|
|
238
|
-
})
|
|
262
|
+
.catch((error) => obs.error(error));
|
|
263
|
+
});
|
|
239
264
|
}
|
|
240
265
|
/**
|
|
241
266
|
* Retrieve multiple entries in the store
|
|
@@ -243,8 +268,13 @@ class NgxIndexedDBService {
|
|
|
243
268
|
* @param keys The ids entries to be retrieve
|
|
244
269
|
*/
|
|
245
270
|
bulkGet(storeName, keys) {
|
|
246
|
-
const
|
|
247
|
-
return
|
|
271
|
+
const observables = keys.map((key) => this.getByKey(storeName, key));
|
|
272
|
+
return new Observable((obs) => {
|
|
273
|
+
combineLatest(observables).subscribe((values) => {
|
|
274
|
+
obs.next(values);
|
|
275
|
+
obs.complete();
|
|
276
|
+
});
|
|
277
|
+
});
|
|
248
278
|
}
|
|
249
279
|
/**
|
|
250
280
|
* Returns entry by id.
|
|
@@ -252,19 +282,19 @@ class NgxIndexedDBService {
|
|
|
252
282
|
* @param id The entry id
|
|
253
283
|
*/
|
|
254
284
|
getByID(storeName, id) {
|
|
255
|
-
return
|
|
285
|
+
return new Observable((obs) => {
|
|
256
286
|
openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
|
|
257
287
|
.then((db) => {
|
|
258
|
-
validateBeforeTransaction(db, storeName,
|
|
259
|
-
const transaction = createTransaction(db, optionsGenerator(DBMode.readonly, storeName,
|
|
288
|
+
validateBeforeTransaction(db, storeName, obs.error);
|
|
289
|
+
const transaction = createTransaction(db, optionsGenerator(DBMode.readonly, storeName, obs.error, obs.next));
|
|
260
290
|
const objectStore = transaction.objectStore(storeName);
|
|
261
291
|
const request = objectStore.get(id);
|
|
262
292
|
request.onsuccess = (event) => {
|
|
263
|
-
|
|
293
|
+
obs.next(event.target.result);
|
|
264
294
|
};
|
|
265
295
|
})
|
|
266
|
-
.catch((
|
|
267
|
-
})
|
|
296
|
+
.catch((error) => obs.error(error));
|
|
297
|
+
});
|
|
268
298
|
}
|
|
269
299
|
/**
|
|
270
300
|
* Returns entry by index.
|
|
@@ -273,42 +303,44 @@ class NgxIndexedDBService {
|
|
|
273
303
|
* @param key The entry key.
|
|
274
304
|
*/
|
|
275
305
|
getByIndex(storeName, indexName, key) {
|
|
276
|
-
return
|
|
306
|
+
return new Observable((obs) => {
|
|
277
307
|
openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
|
|
278
308
|
.then((db) => {
|
|
279
|
-
validateBeforeTransaction(db, storeName,
|
|
280
|
-
const transaction = createTransaction(db, optionsGenerator(DBMode.readonly, storeName,
|
|
309
|
+
validateBeforeTransaction(db, storeName, obs.error);
|
|
310
|
+
const transaction = createTransaction(db, optionsGenerator(DBMode.readonly, storeName, obs.error));
|
|
281
311
|
const objectStore = transaction.objectStore(storeName);
|
|
282
312
|
const index = objectStore.index(indexName);
|
|
283
313
|
const request = index.get(key);
|
|
284
314
|
request.onsuccess = (event) => {
|
|
285
|
-
|
|
315
|
+
obs.next(event.target.result);
|
|
316
|
+
obs.complete();
|
|
286
317
|
};
|
|
287
318
|
})
|
|
288
|
-
.catch((reason) =>
|
|
289
|
-
})
|
|
319
|
+
.catch((reason) => obs.error(reason));
|
|
320
|
+
});
|
|
290
321
|
}
|
|
291
322
|
/**
|
|
292
323
|
* Return all elements from one store
|
|
293
324
|
* @param storeName The name of the store to select the items
|
|
294
325
|
*/
|
|
295
326
|
getAll(storeName) {
|
|
296
|
-
return
|
|
327
|
+
return new Observable((obs) => {
|
|
297
328
|
openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
|
|
298
329
|
.then((db) => {
|
|
299
|
-
validateBeforeTransaction(db, storeName,
|
|
300
|
-
const transaction = createTransaction(db, optionsGenerator(DBMode.readonly, storeName,
|
|
330
|
+
validateBeforeTransaction(db, storeName, obs.error);
|
|
331
|
+
const transaction = createTransaction(db, optionsGenerator(DBMode.readonly, storeName, obs.error, obs.next));
|
|
301
332
|
const objectStore = transaction.objectStore(storeName);
|
|
302
333
|
const request = objectStore.getAll();
|
|
303
334
|
request.onerror = (evt) => {
|
|
304
|
-
|
|
335
|
+
obs.error(evt);
|
|
305
336
|
};
|
|
306
337
|
request.onsuccess = ({ target: { result: ResultAll } }) => {
|
|
307
|
-
|
|
338
|
+
obs.next(ResultAll);
|
|
339
|
+
obs.complete();
|
|
308
340
|
};
|
|
309
341
|
})
|
|
310
|
-
.catch((
|
|
311
|
-
})
|
|
342
|
+
.catch((error) => obs.error(error));
|
|
343
|
+
});
|
|
312
344
|
}
|
|
313
345
|
/**
|
|
314
346
|
* Returns all items from the store after update.
|
|
@@ -317,23 +349,24 @@ class NgxIndexedDBService {
|
|
|
317
349
|
* @param key The key of the entry to update if exists
|
|
318
350
|
*/
|
|
319
351
|
update(storeName, value, key) {
|
|
320
|
-
return
|
|
352
|
+
return new Observable((obs) => {
|
|
321
353
|
openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
|
|
322
354
|
.then((db) => {
|
|
323
|
-
validateBeforeTransaction(db, storeName,
|
|
324
|
-
const transaction = createTransaction(db, optionsGenerator(DBMode.readwrite, storeName,
|
|
355
|
+
validateBeforeTransaction(db, storeName, obs.error);
|
|
356
|
+
const transaction = createTransaction(db, optionsGenerator(DBMode.readwrite, storeName, obs.error));
|
|
325
357
|
const objectStore = transaction.objectStore(storeName);
|
|
326
358
|
transaction.oncomplete = () => {
|
|
327
359
|
this.getAll(storeName)
|
|
328
360
|
.pipe(take(1))
|
|
329
361
|
.subscribe((newValues) => {
|
|
330
|
-
|
|
362
|
+
obs.next(newValues);
|
|
363
|
+
obs.complete();
|
|
331
364
|
});
|
|
332
365
|
};
|
|
333
366
|
key ? objectStore.put(value, key) : objectStore.put(value);
|
|
334
367
|
})
|
|
335
|
-
.catch((reason) =>
|
|
336
|
-
})
|
|
368
|
+
.catch((reason) => obs.error(reason));
|
|
369
|
+
});
|
|
337
370
|
}
|
|
338
371
|
/**
|
|
339
372
|
* Returns the item you updated from the store after the update.
|
|
@@ -342,21 +375,24 @@ class NgxIndexedDBService {
|
|
|
342
375
|
* @param key The key of the entry to update
|
|
343
376
|
*/
|
|
344
377
|
updateByKey(storeName, value, key) {
|
|
345
|
-
return
|
|
378
|
+
return new Observable((obs) => {
|
|
346
379
|
openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
|
|
347
380
|
.then((db) => {
|
|
348
|
-
validateBeforeTransaction(db, storeName,
|
|
349
|
-
const transaction = createTransaction(db, optionsGenerator(DBMode.readwrite, storeName,
|
|
381
|
+
validateBeforeTransaction(db, storeName, obs.error);
|
|
382
|
+
const transaction = createTransaction(db, optionsGenerator(DBMode.readwrite, storeName, obs.error));
|
|
350
383
|
const objectStore = transaction.objectStore(storeName);
|
|
351
384
|
transaction.oncomplete = () => {
|
|
352
|
-
this.getByKey(storeName, key)
|
|
353
|
-
|
|
385
|
+
this.getByKey(storeName, key)
|
|
386
|
+
.pipe(take(1))
|
|
387
|
+
.subscribe((newValue) => {
|
|
388
|
+
obs.next(newValue);
|
|
389
|
+
obs.complete();
|
|
354
390
|
});
|
|
355
391
|
};
|
|
356
392
|
objectStore.put(value, key);
|
|
357
393
|
})
|
|
358
|
-
.catch((reason) =>
|
|
359
|
-
})
|
|
394
|
+
.catch((reason) => obs.error(reason));
|
|
395
|
+
});
|
|
360
396
|
}
|
|
361
397
|
/**
|
|
362
398
|
* Returns all items from the store after delete.
|
|
@@ -364,23 +400,24 @@ class NgxIndexedDBService {
|
|
|
364
400
|
* @param key The key of the entry to be deleted
|
|
365
401
|
*/
|
|
366
402
|
delete(storeName, key) {
|
|
367
|
-
return
|
|
403
|
+
return new Observable((obs) => {
|
|
368
404
|
openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
|
|
369
405
|
.then((db) => {
|
|
370
|
-
validateBeforeTransaction(db, storeName,
|
|
371
|
-
const transaction = createTransaction(db, optionsGenerator(DBMode.readwrite, storeName,
|
|
406
|
+
validateBeforeTransaction(db, storeName, obs.error);
|
|
407
|
+
const transaction = createTransaction(db, optionsGenerator(DBMode.readwrite, storeName, obs.error));
|
|
372
408
|
const objectStore = transaction.objectStore(storeName);
|
|
373
409
|
objectStore.delete(key);
|
|
374
410
|
transaction.oncomplete = () => {
|
|
375
411
|
this.getAll(storeName)
|
|
376
412
|
.pipe(take(1))
|
|
377
413
|
.subscribe((newValues) => {
|
|
378
|
-
|
|
414
|
+
obs.next(newValues);
|
|
415
|
+
obs.complete();
|
|
379
416
|
});
|
|
380
417
|
};
|
|
381
418
|
})
|
|
382
|
-
.catch((reason) =>
|
|
383
|
-
})
|
|
419
|
+
.catch((reason) => obs.error(reason));
|
|
420
|
+
});
|
|
384
421
|
}
|
|
385
422
|
/**
|
|
386
423
|
* Returns true from the store after a successful delete.
|
|
@@ -388,60 +425,61 @@ class NgxIndexedDBService {
|
|
|
388
425
|
* @param key The key of the entry to be deleted
|
|
389
426
|
*/
|
|
390
427
|
deleteByKey(storeName, key) {
|
|
391
|
-
return
|
|
428
|
+
return new Observable((obs) => {
|
|
392
429
|
openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
|
|
393
430
|
.then((db) => {
|
|
394
|
-
validateBeforeTransaction(db, storeName,
|
|
395
|
-
const transaction = createTransaction(db, optionsGenerator(DBMode.readwrite, storeName,
|
|
431
|
+
validateBeforeTransaction(db, storeName, obs.error);
|
|
432
|
+
const transaction = createTransaction(db, optionsGenerator(DBMode.readwrite, storeName, obs.error));
|
|
396
433
|
const objectStore = transaction.objectStore(storeName);
|
|
397
434
|
transaction.oncomplete = () => {
|
|
398
|
-
|
|
435
|
+
obs.next(true);
|
|
436
|
+
obs.complete();
|
|
399
437
|
};
|
|
400
438
|
objectStore.delete(key);
|
|
401
439
|
})
|
|
402
|
-
.catch((reason) =>
|
|
403
|
-
})
|
|
440
|
+
.catch((reason) => obs.error(reason));
|
|
441
|
+
});
|
|
404
442
|
}
|
|
405
443
|
/**
|
|
406
444
|
* Returns true if successfully delete all entries from the store.
|
|
407
445
|
* @param storeName The name of the store to have the entries deleted
|
|
408
446
|
*/
|
|
409
447
|
clear(storeName) {
|
|
410
|
-
return
|
|
448
|
+
return new Observable((obs) => {
|
|
411
449
|
openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
|
|
412
450
|
.then((db) => {
|
|
413
|
-
validateBeforeTransaction(db, storeName,
|
|
414
|
-
const transaction = createTransaction(db, optionsGenerator(DBMode.readwrite, storeName,
|
|
451
|
+
validateBeforeTransaction(db, storeName, obs.error);
|
|
452
|
+
const transaction = createTransaction(db, optionsGenerator(DBMode.readwrite, storeName, obs.error));
|
|
415
453
|
const objectStore = transaction.objectStore(storeName);
|
|
416
454
|
objectStore.clear();
|
|
417
455
|
transaction.oncomplete = () => {
|
|
418
|
-
|
|
456
|
+
obs.next(true);
|
|
457
|
+
obs.complete();
|
|
419
458
|
};
|
|
420
459
|
})
|
|
421
|
-
.catch((reason) =>
|
|
422
|
-
})
|
|
460
|
+
.catch((reason) => obs.error(reason));
|
|
461
|
+
});
|
|
423
462
|
}
|
|
424
463
|
/**
|
|
425
464
|
* Returns true if successfully delete the DB.
|
|
426
465
|
*/
|
|
427
466
|
deleteDatabase() {
|
|
428
|
-
return
|
|
429
|
-
|
|
430
|
-
|
|
467
|
+
return new Observable((obs) => {
|
|
468
|
+
openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
|
|
469
|
+
.then((db) => __awaiter(this, void 0, void 0, function* () {
|
|
431
470
|
yield db.close();
|
|
432
471
|
const deleteDBRequest = this.indexedDB.deleteDatabase(this.dbConfig.name);
|
|
433
472
|
deleteDBRequest.onsuccess = () => {
|
|
434
|
-
|
|
473
|
+
obs.next(true);
|
|
474
|
+
obs.complete();
|
|
435
475
|
};
|
|
436
|
-
deleteDBRequest.onerror =
|
|
476
|
+
deleteDBRequest.onerror = (error) => obs.error(error);
|
|
437
477
|
deleteDBRequest.onblocked = () => {
|
|
438
478
|
throw new Error(`Unable to delete database because it's blocked`);
|
|
439
479
|
};
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
}
|
|
444
|
-
})));
|
|
480
|
+
}))
|
|
481
|
+
.catch((error) => obs.error(error));
|
|
482
|
+
});
|
|
445
483
|
}
|
|
446
484
|
/**
|
|
447
485
|
* Returns the open cursor event
|
|
@@ -449,19 +487,20 @@ class NgxIndexedDBService {
|
|
|
449
487
|
* @param keyRange The key range which the cursor should be open on
|
|
450
488
|
*/
|
|
451
489
|
openCursor(storeName, keyRange) {
|
|
452
|
-
return
|
|
490
|
+
return new Observable((obs) => {
|
|
453
491
|
openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
|
|
454
492
|
.then((db) => {
|
|
455
|
-
validateBeforeTransaction(db, storeName,
|
|
456
|
-
const transaction = createTransaction(db, optionsGenerator(DBMode.readonly, storeName,
|
|
493
|
+
validateBeforeTransaction(db, storeName, obs.error);
|
|
494
|
+
const transaction = createTransaction(db, optionsGenerator(DBMode.readonly, storeName, obs.error));
|
|
457
495
|
const objectStore = transaction.objectStore(storeName);
|
|
458
496
|
const request = keyRange === undefined ? objectStore.openCursor() : objectStore.openCursor(keyRange);
|
|
459
497
|
request.onsuccess = (event) => {
|
|
460
|
-
|
|
498
|
+
obs.next(event);
|
|
499
|
+
obs.complete();
|
|
461
500
|
};
|
|
462
501
|
})
|
|
463
|
-
.catch((reason) =>
|
|
464
|
-
})
|
|
502
|
+
.catch((reason) => obs.error(reason));
|
|
503
|
+
});
|
|
465
504
|
}
|
|
466
505
|
/**
|
|
467
506
|
* Open a cursor by index filter.
|
|
@@ -499,11 +538,11 @@ class NgxIndexedDBService {
|
|
|
499
538
|
*/
|
|
500
539
|
getAllByIndex(storeName, indexName, keyRange) {
|
|
501
540
|
const data = [];
|
|
502
|
-
return
|
|
541
|
+
return new Observable((obs) => {
|
|
503
542
|
openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
|
|
504
543
|
.then((db) => {
|
|
505
|
-
validateBeforeTransaction(db, storeName,
|
|
506
|
-
const transaction = createTransaction(db, optionsGenerator(DBMode.readonly, storeName,
|
|
544
|
+
validateBeforeTransaction(db, storeName, obs.error);
|
|
545
|
+
const transaction = createTransaction(db, optionsGenerator(DBMode.readonly, storeName, obs.error));
|
|
507
546
|
const objectStore = transaction.objectStore(storeName);
|
|
508
547
|
const index = objectStore.index(indexName);
|
|
509
548
|
const request = index.openCursor(keyRange);
|
|
@@ -514,12 +553,13 @@ class NgxIndexedDBService {
|
|
|
514
553
|
cursor.continue();
|
|
515
554
|
}
|
|
516
555
|
else {
|
|
517
|
-
|
|
556
|
+
obs.next(data);
|
|
557
|
+
obs.complete();
|
|
518
558
|
}
|
|
519
559
|
};
|
|
520
560
|
})
|
|
521
|
-
.catch((reason) =>
|
|
522
|
-
})
|
|
561
|
+
.catch((reason) => obs.error(reason));
|
|
562
|
+
});
|
|
523
563
|
}
|
|
524
564
|
/**
|
|
525
565
|
* Returns all primary keys by an index.
|
|
@@ -529,11 +569,11 @@ class NgxIndexedDBService {
|
|
|
529
569
|
*/
|
|
530
570
|
getAllKeysByIndex(storeName, indexName, keyRange) {
|
|
531
571
|
const data = [];
|
|
532
|
-
return
|
|
572
|
+
return new Observable((obs) => {
|
|
533
573
|
openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
|
|
534
574
|
.then((db) => {
|
|
535
|
-
validateBeforeTransaction(db, storeName,
|
|
536
|
-
const transaction = createTransaction(db, optionsGenerator(DBMode.readonly, storeName,
|
|
575
|
+
validateBeforeTransaction(db, storeName, obs.error);
|
|
576
|
+
const transaction = createTransaction(db, optionsGenerator(DBMode.readonly, storeName, obs.error));
|
|
537
577
|
const objectStore = transaction.objectStore(storeName);
|
|
538
578
|
const index = objectStore.index(indexName);
|
|
539
579
|
const request = index.openKeyCursor(keyRange);
|
|
@@ -544,12 +584,13 @@ class NgxIndexedDBService {
|
|
|
544
584
|
cursor.continue();
|
|
545
585
|
}
|
|
546
586
|
else {
|
|
547
|
-
|
|
587
|
+
obs.next(data);
|
|
588
|
+
obs.complete();
|
|
548
589
|
}
|
|
549
590
|
};
|
|
550
591
|
})
|
|
551
|
-
.catch((reason) =>
|
|
552
|
-
})
|
|
592
|
+
.catch((reason) => obs.error(reason));
|
|
593
|
+
});
|
|
553
594
|
}
|
|
554
595
|
/**
|
|
555
596
|
* Returns the number of rows in a store.
|
|
@@ -557,18 +598,28 @@ class NgxIndexedDBService {
|
|
|
557
598
|
* @param keyRange The range value and criteria to apply.
|
|
558
599
|
*/
|
|
559
600
|
count(storeName, keyRange) {
|
|
560
|
-
return
|
|
601
|
+
return new Observable((obs) => {
|
|
561
602
|
openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
|
|
562
603
|
.then((db) => {
|
|
563
|
-
validateBeforeTransaction(db, storeName,
|
|
564
|
-
const transaction = createTransaction(db, optionsGenerator(DBMode.readonly, storeName,
|
|
604
|
+
validateBeforeTransaction(db, storeName, obs.error);
|
|
605
|
+
const transaction = createTransaction(db, optionsGenerator(DBMode.readonly, storeName, obs.error));
|
|
565
606
|
const objectStore = transaction.objectStore(storeName);
|
|
566
607
|
const request = objectStore.count(keyRange);
|
|
567
|
-
request.onerror = (e) =>
|
|
568
|
-
request.onsuccess = (e) =>
|
|
608
|
+
request.onerror = (e) => obs.error(e);
|
|
609
|
+
request.onsuccess = (e) => {
|
|
610
|
+
obs.next(e.target.result);
|
|
611
|
+
obs.complete();
|
|
612
|
+
};
|
|
569
613
|
})
|
|
570
|
-
.catch((reason) =>
|
|
571
|
-
})
|
|
614
|
+
.catch((reason) => obs.error(reason));
|
|
615
|
+
});
|
|
616
|
+
}
|
|
617
|
+
/**
|
|
618
|
+
* Delete the store by name.
|
|
619
|
+
* @param storeName The name of the store to query
|
|
620
|
+
*/
|
|
621
|
+
deleteObjectStore(storeName) {
|
|
622
|
+
return DeleteObjectStore(this.dbConfig.name, ++this.dbConfig.version, storeName);
|
|
572
623
|
}
|
|
573
624
|
}
|
|
574
625
|
NgxIndexedDBService.decorators = [
|