ngx-indexed-db 9.4.0

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.
@@ -0,0 +1,4 @@
1
+ export * from './lib/ngxindexeddb.module';
2
+ export * from './lib/ngx-indexed-db.service';
3
+ export * from './lib/ngx-indexed-db.meta';
4
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicHVibGljLWFwaS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uL3Byb2plY3RzL25neC1pbmRleGVkLWRiL3NyYy9wdWJsaWMtYXBpLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLGNBQWMsMkJBQTJCLENBQUM7QUFDMUMsY0FBYyw4QkFBOEIsQ0FBQztBQUM3QyxjQUFjLDJCQUEyQixDQUFDIiwic291cmNlc0NvbnRlbnQiOlsiZXhwb3J0ICogZnJvbSAnLi9saWIvbmd4aW5kZXhlZGRiLm1vZHVsZSc7XG5leHBvcnQgKiBmcm9tICcuL2xpYi9uZ3gtaW5kZXhlZC1kYi5zZXJ2aWNlJztcbmV4cG9ydCAqIGZyb20gJy4vbGliL25neC1pbmRleGVkLWRiLm1ldGEnO1xuIl19
@@ -0,0 +1,30 @@
1
+ export function validateStoreName(db, storeName) {
2
+ return db.objectStoreNames.contains(storeName);
3
+ }
4
+ export function validateBeforeTransaction(db, storeName, reject) {
5
+ if (!db) {
6
+ reject('You need to use the openDatabase function to create a database before you query it!');
7
+ }
8
+ if (!validateStoreName(db, storeName)) {
9
+ reject(`objectStore does not exists: ${storeName}`);
10
+ }
11
+ }
12
+ export function createTransaction(db, options) {
13
+ const trans = db.transaction(options.storeName, options.dbMode);
14
+ trans.onerror = options.error;
15
+ trans.onabort = options.abort;
16
+ return trans;
17
+ }
18
+ export function optionsGenerator(type, storeName, reject, resolve) {
19
+ return {
20
+ storeName,
21
+ dbMode: type,
22
+ error: (e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VuY3Rpb24gdG8gY3JlYXRlIGEgZGF0YWJhc2UgYmVmb3JlIHlvdSBxdWVyeSBpdCEnKTtcbiAgfVxuICBpZiAoIXZhbGlkYXRlU3RvcmVOYW1lKGRiLCBzdG9yZU5hbWUpKSB7XG4gICAgcmVqZWN0KGBvYmplY3RTdG9yZSBkb2VzIG5vdCBleGlzdHM6ICR7c3RvcmVOYW1lfWApO1xuICB9XG59XG5cbmV4cG9ydCBmdW5jdGlvbiBjcmVhdGVUcmFuc2FjdGlvbihkYjogSURCRGF0YWJhc2UsIG9wdGlvbnM6IE9wdGlvbnMpOiBJREJUcmFuc2FjdGlvbiB7XG4gIGNvbnN0IHRyYW5zOiBJREJUcmFuc2FjdGlvbiA9IGRiLnRyYW5zYWN0aW9uKG9wdGlvbnMuc3RvcmVOYW1lLCBvcHRpb25zLmRiTW9kZSk7XG4gIHRyYW5zLm9uZXJyb3IgPSBvcHRpb25zLmVycm9yO1xuICB0cmFucy5vbmFib3J0ID0gb3B0aW9ucy5hYm9ydDtcbiAgcmV0dXJuIHRyYW5zO1xufVxuXG5leHBvcnQgZnVuY3Rpb24gb3B0aW9uc0dlbmVyYXRvcihcbiAgdHlwZTogYW55LFxuICBzdG9yZU5hbWU6IGFueSxcbiAgcmVqZWN0OiAocmVhc29uPzogYW55KSA9PiB2b2lkLFxuICByZXNvbHZlPzogKGU6IGFueSkgPT4gdm9pZFxuKTogT3B0aW9ucyB7XG4gIHJldHVybiB7XG4gICAgc3RvcmVOYW1lLFxuICAgIGRiTW9kZTogdHlwZSxcbiAgICBlcnJvcjogKGU6IEV2ZW50KSA9PiB7XG4gICAgICByZWplY3QoZSk7XG4gICAgfSxcbiAgICBhYm9ydDogKGU6IEV2ZW50KSA9PiB7XG4gICAgICByZWplY3QoZSk7XG4gICAgfSxcbiAgfTtcbn1cbiJdfQ==
@@ -0,0 +1,622 @@
1
+ import { InjectionToken, Injectable, Inject, PLATFORM_ID, NgModule } from '@angular/core';
2
+ import { isPlatformBrowser, CommonModule } from '@angular/common';
3
+ import { __awaiter } from 'tslib';
4
+ import { Observable, forkJoin, from, combineLatest, Subject } from 'rxjs';
5
+ import { take } from 'rxjs/operators';
6
+
7
+ function openDatabase(indexedDB, dbName, version, upgradeCallback) {
8
+ return new Promise((resolve, reject) => {
9
+ if (!indexedDB) {
10
+ reject('IndexedDB not available');
11
+ }
12
+ const request = indexedDB.open(dbName, version);
13
+ let db;
14
+ request.onsuccess = (event) => {
15
+ db = request.result;
16
+ resolve(db);
17
+ };
18
+ request.onerror = (event) => {
19
+ reject(`IndexedDB error: ${request.error}`);
20
+ };
21
+ if (typeof upgradeCallback === 'function') {
22
+ request.onupgradeneeded = (event) => {
23
+ upgradeCallback(event, db);
24
+ };
25
+ }
26
+ });
27
+ }
28
+ function CreateObjectStore(indexedDB, dbName, version, storeSchemas, migrationFactory) {
29
+ if (!indexedDB) {
30
+ return;
31
+ }
32
+ const request = indexedDB.open(dbName, version);
33
+ request.onupgradeneeded = (event) => {
34
+ const database = event.target.result;
35
+ storeSchemas.forEach((storeSchema) => {
36
+ if (!database.objectStoreNames.contains(storeSchema.store)) {
37
+ const objectStore = database.createObjectStore(storeSchema.store, storeSchema.storeConfig);
38
+ storeSchema.storeSchema.forEach((schema) => {
39
+ objectStore.createIndex(schema.name, schema.keypath, schema.options);
40
+ });
41
+ }
42
+ });
43
+ const storeMigrations = migrationFactory && migrationFactory();
44
+ if (storeMigrations) {
45
+ Object.keys(storeMigrations)
46
+ .map((k) => parseInt(k, 10))
47
+ .filter((v) => v > event.oldVersion)
48
+ .sort((a, b) => a - b)
49
+ .forEach((v) => {
50
+ storeMigrations[v](database, request.transaction);
51
+ });
52
+ }
53
+ database.close();
54
+ };
55
+ request.onsuccess = (e) => {
56
+ e.target.result.close();
57
+ };
58
+ }
59
+
60
+ function validateStoreName(db, storeName) {
61
+ return db.objectStoreNames.contains(storeName);
62
+ }
63
+ function validateBeforeTransaction(db, storeName, reject) {
64
+ if (!db) {
65
+ reject('You need to use the openDatabase function to create a database before you query it!');
66
+ }
67
+ if (!validateStoreName(db, storeName)) {
68
+ reject(`objectStore does not exists: ${storeName}`);
69
+ }
70
+ }
71
+ function createTransaction(db, options) {
72
+ const trans = db.transaction(options.storeName, options.dbMode);
73
+ trans.onerror = options.error;
74
+ trans.onabort = options.abort;
75
+ return trans;
76
+ }
77
+ function optionsGenerator(type, storeName, reject, resolve) {
78
+ return {
79
+ storeName,
80
+ dbMode: type,
81
+ error: (e) => {
82
+ reject(e);
83
+ },
84
+ abort: (e) => {
85
+ reject(e);
86
+ },
87
+ };
88
+ }
89
+
90
+ var DBMode;
91
+ (function (DBMode) {
92
+ DBMode["readonly"] = "readonly";
93
+ DBMode["readwrite"] = "readwrite";
94
+ })(DBMode || (DBMode = {}));
95
+ const CONFIG_TOKEN = new InjectionToken(null);
96
+
97
+ class NgxIndexedDBService {
98
+ constructor(dbConfig, platformId) {
99
+ this.dbConfig = dbConfig;
100
+ this.platformId = platformId;
101
+ if (!dbConfig.name) {
102
+ throw new Error('NgxIndexedDB: Please, provide the dbName in the configuration');
103
+ }
104
+ if (!dbConfig.version) {
105
+ throw new Error('NgxIndexedDB: Please, provide the db version in the configuration');
106
+ }
107
+ this.isBrowser = isPlatformBrowser(this.platformId);
108
+ if (this.isBrowser) {
109
+ this.indexedDB =
110
+ window.indexedDB ||
111
+ window.mozIndexedDB ||
112
+ window.webkitIndexedDB ||
113
+ window.msIndexedDB;
114
+ CreateObjectStore(this.indexedDB, dbConfig.name, dbConfig.version, dbConfig.objectStoresMeta, dbConfig.migrationFactory);
115
+ openDatabase(this.indexedDB, dbConfig.name).then((db) => {
116
+ if (db.version !== dbConfig.version) {
117
+ if (process.env.NODE_ENV !== 'production') {
118
+ console.warn(`
119
+ Your DB Config doesn't match the most recent version of the DB with name ${this.dbConfig.name}, please update it
120
+ DB current version: ${db.version};
121
+ Your configuration: ${dbConfig.version};
122
+ `);
123
+ console.warn(`Using latest version ${db.version}`);
124
+ }
125
+ this.dbConfig.version = db.version;
126
+ }
127
+ });
128
+ }
129
+ }
130
+ /**
131
+ * Allows to crate a new object store ad-hoc
132
+ * @param storeName The name of the store to be created
133
+ * @param migrationFactory The migration factory if exists
134
+ */
135
+ createObjectStore(storeSchema, migrationFactory) {
136
+ const storeSchemas = [storeSchema];
137
+ CreateObjectStore(this.indexedDB, this.dbConfig.name, ++this.dbConfig.version, storeSchemas, migrationFactory);
138
+ }
139
+ /**
140
+ * Adds new entry in the store and returns its key
141
+ * @param storeName The name of the store to add the item
142
+ * @param value The entry to be added
143
+ * @param key The optional key for the entry
144
+ */
145
+ add(storeName, value, key) {
146
+ return new Observable((obs) => {
147
+ openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
148
+ .then((db) => {
149
+ const transaction = createTransaction(db, optionsGenerator(DBMode.readwrite, storeName, obs.error));
150
+ const objectStore = transaction.objectStore(storeName);
151
+ const request = Boolean(key) ? objectStore.add(value, key) : objectStore.add(value);
152
+ request.onsuccess = (evt) => __awaiter(this, void 0, void 0, function* () {
153
+ const result = evt.target.result;
154
+ const getRequest = objectStore.get(result);
155
+ getRequest.onsuccess = (event) => {
156
+ obs.next(event.target.result);
157
+ obs.complete();
158
+ };
159
+ });
160
+ })
161
+ .catch((error) => obs.error(error));
162
+ });
163
+ }
164
+ /**
165
+ * Adds new entries in the store and returns its key
166
+ * @param storeName The name of the store to add the item
167
+ * @param values The entries to be added containing optional key attribute
168
+ */
169
+ bulkAdd(storeName, values) {
170
+ const promises = values.map((value) => {
171
+ return new Promise((resolve, reject) => {
172
+ openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
173
+ .then((db) => {
174
+ const transaction = createTransaction(db, optionsGenerator(DBMode.readwrite, storeName, reject, resolve));
175
+ const objectStore = transaction.objectStore(storeName);
176
+ const key = value.key;
177
+ delete value.key;
178
+ const request = Boolean(key)
179
+ ? objectStore.add(value, key)
180
+ : objectStore.add(value);
181
+ request.onsuccess = (evt) => {
182
+ const result = evt.target.result;
183
+ resolve(result);
184
+ };
185
+ })
186
+ .catch((reason) => reject(reason));
187
+ });
188
+ });
189
+ return forkJoin(promises);
190
+ }
191
+ /**
192
+ * Delete entries in the store and returns current entries in the store
193
+ * @param storeName The name of the store to add the item
194
+ * @param keys The keys to be deleted
195
+ */
196
+ bulkDelete(storeName, keys) {
197
+ const promises = keys.map((key) => {
198
+ return new Promise((resolve, reject) => {
199
+ openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
200
+ .then((db) => {
201
+ const transaction = createTransaction(db, optionsGenerator(DBMode.readwrite, storeName, reject, resolve));
202
+ const objectStore = transaction.objectStore(storeName);
203
+ objectStore.delete(key);
204
+ transaction.oncomplete = () => {
205
+ this.getAll(storeName)
206
+ .pipe(take(1))
207
+ .subscribe((newValues) => {
208
+ resolve(newValues);
209
+ });
210
+ };
211
+ })
212
+ .catch((reason) => reject(reason));
213
+ });
214
+ });
215
+ return from(Promise.all(promises));
216
+ }
217
+ /**
218
+ * Returns entry by key.
219
+ * @param storeName The name of the store to query
220
+ * @param key The entry key
221
+ */
222
+ getByKey(storeName, key) {
223
+ return new Observable((obs) => {
224
+ openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
225
+ .then((db) => {
226
+ const transaction = createTransaction(db, optionsGenerator(DBMode.readonly, storeName, obs.error));
227
+ const objectStore = transaction.objectStore(storeName);
228
+ const request = objectStore.get(key);
229
+ request.onsuccess = (event) => {
230
+ obs.next(event.target.result);
231
+ obs.complete();
232
+ };
233
+ request.onerror = (event) => {
234
+ obs.error(event);
235
+ };
236
+ })
237
+ .catch((error) => obs.error(error));
238
+ });
239
+ }
240
+ /**
241
+ * Retrieve multiple entries in the store
242
+ * @param storeName The name of the store to retrieve the items
243
+ * @param keys The ids entries to be retrieve
244
+ */
245
+ bulkGet(storeName, keys) {
246
+ const observables = keys.map((key) => this.getByKey(storeName, key));
247
+ return new Observable((obs) => {
248
+ combineLatest(observables).subscribe((values) => {
249
+ obs.next(values);
250
+ obs.complete();
251
+ });
252
+ });
253
+ }
254
+ /**
255
+ * Returns entry by id.
256
+ * @param storeName The name of the store to query
257
+ * @param id The entry id
258
+ */
259
+ getByID(storeName, id) {
260
+ return new Observable((obs) => {
261
+ openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
262
+ .then((db) => {
263
+ validateBeforeTransaction(db, storeName, obs.error);
264
+ const transaction = createTransaction(db, optionsGenerator(DBMode.readonly, storeName, obs.error, obs.next));
265
+ const objectStore = transaction.objectStore(storeName);
266
+ const request = objectStore.get(id);
267
+ request.onsuccess = (event) => {
268
+ obs.next(event.target.result);
269
+ };
270
+ })
271
+ .catch((error) => obs.error(error));
272
+ });
273
+ }
274
+ /**
275
+ * Returns entry by index.
276
+ * @param storeName The name of the store to query
277
+ * @param indexName The index name to filter
278
+ * @param key The entry key.
279
+ */
280
+ getByIndex(storeName, indexName, key) {
281
+ return new Observable((obs) => {
282
+ openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
283
+ .then((db) => {
284
+ validateBeforeTransaction(db, storeName, obs.error);
285
+ const transaction = createTransaction(db, optionsGenerator(DBMode.readonly, storeName, obs.error));
286
+ const objectStore = transaction.objectStore(storeName);
287
+ const index = objectStore.index(indexName);
288
+ const request = index.get(key);
289
+ request.onsuccess = (event) => {
290
+ obs.next(event.target.result);
291
+ obs.complete();
292
+ };
293
+ })
294
+ .catch((reason) => obs.error(reason));
295
+ });
296
+ }
297
+ /**
298
+ * Return all elements from one store
299
+ * @param storeName The name of the store to select the items
300
+ */
301
+ getAll(storeName) {
302
+ return new Observable((obs) => {
303
+ openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
304
+ .then((db) => {
305
+ validateBeforeTransaction(db, storeName, obs.error);
306
+ const transaction = createTransaction(db, optionsGenerator(DBMode.readonly, storeName, obs.error, obs.next));
307
+ const objectStore = transaction.objectStore(storeName);
308
+ const request = objectStore.getAll();
309
+ request.onerror = (evt) => {
310
+ obs.error(evt);
311
+ };
312
+ request.onsuccess = ({ target: { result: ResultAll } }) => {
313
+ obs.next(ResultAll);
314
+ obs.complete();
315
+ };
316
+ })
317
+ .catch((error) => obs.error(error));
318
+ });
319
+ }
320
+ /**
321
+ * Returns all items from the store after update.
322
+ * @param storeName The name of the store to update
323
+ * @param value The new value for the entry
324
+ * @param key The key of the entry to update if exists
325
+ */
326
+ update(storeName, value, key) {
327
+ return new Observable((obs) => {
328
+ openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
329
+ .then((db) => {
330
+ validateBeforeTransaction(db, storeName, obs.error);
331
+ const transaction = createTransaction(db, optionsGenerator(DBMode.readwrite, storeName, obs.error));
332
+ const objectStore = transaction.objectStore(storeName);
333
+ transaction.oncomplete = () => {
334
+ this.getAll(storeName)
335
+ .pipe(take(1))
336
+ .subscribe((newValues) => {
337
+ obs.next(newValues);
338
+ obs.complete();
339
+ });
340
+ };
341
+ key ? objectStore.put(value, key) : objectStore.put(value);
342
+ })
343
+ .catch((reason) => obs.error(reason));
344
+ });
345
+ }
346
+ /**
347
+ * Returns the item you updated from the store after the update.
348
+ * @param storeName The name of the store to update
349
+ * @param value The new value for the entry
350
+ * @param key The key of the entry to update
351
+ */
352
+ updateByKey(storeName, value, key) {
353
+ return new Observable((obs) => {
354
+ openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
355
+ .then((db) => {
356
+ validateBeforeTransaction(db, storeName, obs.error);
357
+ const transaction = createTransaction(db, optionsGenerator(DBMode.readwrite, storeName, obs.error));
358
+ const objectStore = transaction.objectStore(storeName);
359
+ transaction.oncomplete = () => {
360
+ const request = objectStore.get(key);
361
+ request.onsuccess = (event) => {
362
+ obs.next(event.target.result);
363
+ };
364
+ request.onerror = (event) => {
365
+ obs.error(event);
366
+ };
367
+ };
368
+ objectStore.put(value, key);
369
+ })
370
+ .catch((reason) => obs.error(reason));
371
+ });
372
+ }
373
+ /**
374
+ * Returns all items from the store after delete.
375
+ * @param storeName The name of the store to have the entry deleted
376
+ * @param key The key of the entry to be deleted
377
+ */
378
+ delete(storeName, key) {
379
+ return new Observable((obs) => {
380
+ openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
381
+ .then((db) => {
382
+ validateBeforeTransaction(db, storeName, obs.error);
383
+ const transaction = createTransaction(db, optionsGenerator(DBMode.readwrite, storeName, obs.error));
384
+ const objectStore = transaction.objectStore(storeName);
385
+ objectStore.delete(key);
386
+ transaction.oncomplete = () => {
387
+ this.getAll(storeName)
388
+ .pipe(take(1))
389
+ .subscribe((newValues) => {
390
+ obs.next(newValues);
391
+ obs.complete();
392
+ });
393
+ };
394
+ })
395
+ .catch((reason) => obs.error(reason));
396
+ });
397
+ }
398
+ /**
399
+ * Returns true from the store after a successful delete.
400
+ * @param storeName The name of the store to have the entry deleted
401
+ * @param key The key of the entry to be deleted
402
+ */
403
+ deleteByKey(storeName, key) {
404
+ return new Observable((obs) => {
405
+ openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
406
+ .then((db) => {
407
+ validateBeforeTransaction(db, storeName, obs.error);
408
+ const transaction = createTransaction(db, optionsGenerator(DBMode.readwrite, storeName, obs.error));
409
+ const objectStore = transaction.objectStore(storeName);
410
+ transaction.oncomplete = () => {
411
+ obs.next(true);
412
+ obs.complete();
413
+ };
414
+ objectStore.delete(key);
415
+ })
416
+ .catch((reason) => obs.error(reason));
417
+ });
418
+ }
419
+ /**
420
+ * Returns true if successfully delete all entries from the store.
421
+ * @param storeName The name of the store to have the entries deleted
422
+ */
423
+ clear(storeName) {
424
+ return new Observable((obs) => {
425
+ openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
426
+ .then((db) => {
427
+ validateBeforeTransaction(db, storeName, obs.error);
428
+ const transaction = createTransaction(db, optionsGenerator(DBMode.readwrite, storeName, obs.error));
429
+ const objectStore = transaction.objectStore(storeName);
430
+ objectStore.clear();
431
+ transaction.oncomplete = () => {
432
+ obs.next(true);
433
+ obs.complete();
434
+ };
435
+ })
436
+ .catch((reason) => obs.error(reason));
437
+ });
438
+ }
439
+ /**
440
+ * Returns true if successfully delete the DB.
441
+ */
442
+ deleteDatabase() {
443
+ return new Observable((obs) => {
444
+ openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
445
+ .then((db) => __awaiter(this, void 0, void 0, function* () {
446
+ yield db.close();
447
+ const deleteDBRequest = this.indexedDB.deleteDatabase(this.dbConfig.name);
448
+ deleteDBRequest.onsuccess = () => {
449
+ obs.next(true);
450
+ obs.complete();
451
+ };
452
+ deleteDBRequest.onerror = (error) => obs.error(error);
453
+ deleteDBRequest.onblocked = () => {
454
+ throw new Error(`Unable to delete database because it's blocked`);
455
+ };
456
+ }))
457
+ .catch((error) => obs.error(error));
458
+ });
459
+ }
460
+ /**
461
+ * Returns the open cursor event
462
+ * @param storeName The name of the store to have the entries deleted
463
+ * @param keyRange The key range which the cursor should be open on
464
+ */
465
+ openCursor(storeName, keyRange) {
466
+ return new Observable((obs) => {
467
+ openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
468
+ .then((db) => {
469
+ validateBeforeTransaction(db, storeName, obs.error);
470
+ const transaction = createTransaction(db, optionsGenerator(DBMode.readonly, storeName, obs.error));
471
+ const objectStore = transaction.objectStore(storeName);
472
+ const request = keyRange === undefined ? objectStore.openCursor() : objectStore.openCursor(keyRange);
473
+ request.onsuccess = (event) => {
474
+ obs.next(event);
475
+ obs.complete();
476
+ };
477
+ })
478
+ .catch((reason) => obs.error(reason));
479
+ });
480
+ }
481
+ /**
482
+ * Open a cursor by index filter.
483
+ * @param storeName The name of the store to query.
484
+ * @param indexName The index name to filter.
485
+ * @param keyRange The range value and criteria to apply on the index.
486
+ */
487
+ openCursorByIndex(storeName, indexName, keyRange, mode = DBMode.readonly) {
488
+ const obs = new Subject();
489
+ openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
490
+ .then((db) => {
491
+ validateBeforeTransaction(db, storeName, (reason) => {
492
+ obs.error(reason);
493
+ });
494
+ const transaction = createTransaction(db, optionsGenerator(mode, storeName, (reason) => {
495
+ obs.error(reason);
496
+ }, () => {
497
+ obs.next();
498
+ }));
499
+ const objectStore = transaction.objectStore(storeName);
500
+ const index = objectStore.index(indexName);
501
+ const request = index.openCursor(keyRange);
502
+ request.onsuccess = (event) => {
503
+ obs.next(event);
504
+ };
505
+ })
506
+ .catch((reason) => obs.error(reason));
507
+ return obs;
508
+ }
509
+ /**
510
+ * Returns all items by an index.
511
+ * @param storeName The name of the store to query
512
+ * @param indexName The index name to filter
513
+ * @param keyRange The range value and criteria to apply on the index.
514
+ */
515
+ getAllByIndex(storeName, indexName, keyRange) {
516
+ const data = [];
517
+ return new Observable((obs) => {
518
+ openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
519
+ .then((db) => {
520
+ validateBeforeTransaction(db, storeName, obs.error);
521
+ const transaction = createTransaction(db, optionsGenerator(DBMode.readonly, storeName, obs.error));
522
+ const objectStore = transaction.objectStore(storeName);
523
+ const index = objectStore.index(indexName);
524
+ const request = index.openCursor(keyRange);
525
+ request.onsuccess = (event) => {
526
+ const cursor = event.target.result;
527
+ if (cursor) {
528
+ data.push(cursor.value);
529
+ cursor.continue();
530
+ }
531
+ else {
532
+ obs.next(data);
533
+ obs.complete();
534
+ }
535
+ };
536
+ })
537
+ .catch((reason) => obs.error(reason));
538
+ });
539
+ }
540
+ /**
541
+ * Returns all primary keys by an index.
542
+ * @param storeName The name of the store to query
543
+ * @param indexName The index name to filter
544
+ * @param keyRange The range value and criteria to apply on the index.
545
+ */
546
+ getAllKeysByIndex(storeName, indexName, keyRange) {
547
+ const data = [];
548
+ return new Observable((obs) => {
549
+ openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
550
+ .then((db) => {
551
+ validateBeforeTransaction(db, storeName, obs.error);
552
+ const transaction = createTransaction(db, optionsGenerator(DBMode.readonly, storeName, obs.error));
553
+ const objectStore = transaction.objectStore(storeName);
554
+ const index = objectStore.index(indexName);
555
+ const request = index.openKeyCursor(keyRange);
556
+ request.onsuccess = (event) => {
557
+ const cursor = event.target.result;
558
+ if (cursor) {
559
+ data.push({ primaryKey: cursor.primaryKey, key: cursor.key });
560
+ cursor.continue();
561
+ }
562
+ else {
563
+ obs.next(data);
564
+ obs.complete();
565
+ }
566
+ };
567
+ })
568
+ .catch((reason) => obs.error(reason));
569
+ });
570
+ }
571
+ /**
572
+ * Returns the number of rows in a store.
573
+ * @param storeName The name of the store to query
574
+ * @param keyRange The range value and criteria to apply.
575
+ */
576
+ count(storeName, keyRange) {
577
+ return new Observable((obs) => {
578
+ openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
579
+ .then((db) => {
580
+ validateBeforeTransaction(db, storeName, obs.error);
581
+ const transaction = createTransaction(db, optionsGenerator(DBMode.readonly, storeName, obs.error));
582
+ const objectStore = transaction.objectStore(storeName);
583
+ const request = objectStore.count(keyRange);
584
+ request.onerror = (e) => obs.error(e);
585
+ request.onsuccess = (e) => {
586
+ obs.next(e.target.result);
587
+ obs.complete();
588
+ };
589
+ })
590
+ .catch((reason) => obs.error(reason));
591
+ });
592
+ }
593
+ }
594
+ NgxIndexedDBService.decorators = [
595
+ { type: Injectable }
596
+ ];
597
+ NgxIndexedDBService.ctorParameters = () => [
598
+ { type: undefined, decorators: [{ type: Inject, args: [CONFIG_TOKEN,] }] },
599
+ { type: undefined, decorators: [{ type: Inject, args: [PLATFORM_ID,] }] }
600
+ ];
601
+
602
+ class NgxIndexedDBModule {
603
+ static forRoot(dbConfig) {
604
+ return {
605
+ ngModule: NgxIndexedDBModule,
606
+ providers: [NgxIndexedDBService, { provide: CONFIG_TOKEN, useValue: dbConfig }]
607
+ };
608
+ }
609
+ }
610
+ NgxIndexedDBModule.decorators = [
611
+ { type: NgModule, args: [{
612
+ declarations: [],
613
+ imports: [CommonModule]
614
+ },] }
615
+ ];
616
+
617
+ /**
618
+ * Generated bundle index. Do not edit.
619
+ */
620
+
621
+ export { CONFIG_TOKEN, DBMode, NgxIndexedDBModule, NgxIndexedDBService };
622
+ //# sourceMappingURL=ngx-indexed-db.js.map