ngx-indexed-db 19.3.2 → 19.4.3

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.
@@ -1,7 +1,7 @@
1
1
  import * as i0 from '@angular/core';
2
2
  import { InjectionToken, isDevMode, Injectable, Inject, assertInInjectionContext, inject, PLATFORM_ID, makeEnvironmentProviders, NgModule } from '@angular/core';
3
3
  import { __decorate } from 'tslib';
4
- import { Observable, from, combineLatest, Subject } from 'rxjs';
4
+ import { Observable, from, combineLatest } from 'rxjs';
5
5
  import { finalize, take } from 'rxjs/operators';
6
6
  import { isPlatformBrowser, DOCUMENT } from '@angular/common';
7
7
 
@@ -125,7 +125,7 @@ function DeleteObjectStore(dbName, version, storeName) {
125
125
  database.deleteObjectStore(storeName);
126
126
  database.close();
127
127
  console.log('onupgradeneeded');
128
- obs.next(true);
128
+ obs.next();
129
129
  obs.complete();
130
130
  };
131
131
  request.onerror = (e) => obs.error(e);
@@ -253,10 +253,10 @@ class NgxIndexedDBService {
253
253
  await CreateObjectStore(this.indexedDB, this.dbConfig.name, ++this.dbConfig.version, storeSchemas, migrationFactory);
254
254
  }
255
255
  /**
256
- * Create dynamic store if not already without incrementing version
257
- * For Dynamic store
258
- * @param storeName The name of the store to create
259
- */
256
+ * Create dynamic store if not already without incrementing version
257
+ * For Dynamic store
258
+ * @param storeName The name of the store to create
259
+ */
260
260
  async createDynamicObjectStore(storeSchema, migrationFactory) {
261
261
  const storeSchemas = [storeSchema];
262
262
  await CreateObjectStore(this.indexedDB, this.dbConfig.name, this.dbConfig.version, storeSchemas, migrationFactory);
@@ -273,7 +273,8 @@ class NgxIndexedDBService {
273
273
  .then((db) => {
274
274
  const transaction = createTransaction(db, optionsGenerator(DBMode.readwrite, storeName, (e) => obs.error(e)));
275
275
  const objectStore = transaction.objectStore(storeName);
276
- const request = Boolean(key) ? objectStore.add(value, key) : objectStore.add(value);
276
+ const hasKey = Boolean(key);
277
+ const request = hasKey ? objectStore.add(value, key) : objectStore.add(value);
277
278
  request.onsuccess = async (evt) => {
278
279
  const result = evt.target.result;
279
280
  const getRequest = objectStore.get(result);
@@ -307,7 +308,8 @@ class NgxIndexedDBService {
307
308
  return new Promise((resolve1) => {
308
309
  const key = value.key;
309
310
  delete value.key;
310
- const request = Boolean(key)
311
+ const hasKey = Boolean(key);
312
+ const request = hasKey
311
313
  ? objectStore.add(value, key)
312
314
  : objectStore.add(value);
313
315
  request.onsuccess = (evt) => {
@@ -517,22 +519,26 @@ class NgxIndexedDBService {
517
519
  /**
518
520
  * Returns all items from the store after delete.
519
521
  * @param storeName The name of the store to have the entry deleted
520
- * @param key The key of the entry to be deleted
522
+ * @param query The key or key range criteria to apply
521
523
  */
522
- delete(storeName, key) {
524
+ delete(storeName, query) {
523
525
  return new Observable((obs) => {
524
526
  openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
525
527
  .then((db) => {
526
528
  validateBeforeTransaction(db, storeName, (e) => obs.error(e));
527
529
  const transaction = createTransaction(db, optionsGenerator(DBMode.readwrite, storeName, (e) => obs.error(e)));
528
530
  const objectStore = transaction.objectStore(storeName);
529
- objectStore.delete(key);
531
+ objectStore.delete(query);
532
+ transaction.onerror = (e) => obs.error(e);
530
533
  transaction.oncomplete = () => {
531
534
  this.getAll(storeName)
532
535
  .pipe(take(1))
533
- .subscribe((newValues) => {
534
- obs.next(newValues);
535
- obs.complete();
536
+ .subscribe({
537
+ next: (newValues) => {
538
+ obs.next(newValues);
539
+ },
540
+ error: (e) => obs.error(e),
541
+ complete: () => obs.complete(),
536
542
  });
537
543
  };
538
544
  })
@@ -540,28 +546,61 @@ class NgxIndexedDBService {
540
546
  });
541
547
  }
542
548
  /**
543
- * Returns true from the store after a successful delete.
549
+ * Returns after a successful delete.
544
550
  * @param storeName The name of the store to have the entry deleted
545
- * @param key The key of the entry to be deleted
551
+ * @param query The key or key range criteria to apply
546
552
  */
547
- deleteByKey(storeName, key) {
553
+ deleteByKey(storeName, query) {
548
554
  return new Observable((obs) => {
549
555
  openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
550
556
  .then((db) => {
551
557
  validateBeforeTransaction(db, storeName, (e) => obs.error(e));
552
558
  const transaction = createTransaction(db, optionsGenerator(DBMode.readwrite, storeName, (e) => obs.error(e)));
553
559
  const objectStore = transaction.objectStore(storeName);
560
+ objectStore.delete(query);
561
+ transaction.onerror = (e) => obs.error(e);
554
562
  transaction.oncomplete = () => {
555
- obs.next(true);
563
+ obs.next();
556
564
  obs.complete();
557
565
  };
558
- objectStore.delete(key);
559
566
  })
560
567
  .catch((reason) => obs.error(reason));
561
568
  });
562
569
  }
563
570
  /**
564
- * Returns true if successfully delete all entries from the store.
571
+ * Delete all items by an index.
572
+ * @param storeName The name of the store to query
573
+ * @param indexName The index name to filter
574
+ * @param query The key or key range criteria to apply
575
+ * @param direction A string telling the cursor which direction to travel.
576
+ */
577
+ deleteAllByIndex(storeName, indexName, query, direction) {
578
+ return new Observable((obs) => {
579
+ openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
580
+ .then((db) => {
581
+ validateBeforeTransaction(db, storeName, (e) => obs.error(e));
582
+ const transaction = createTransaction(db, optionsGenerator(DBMode.readwrite, storeName, obs.error));
583
+ const objectStore = transaction.objectStore(storeName);
584
+ const index = objectStore.index(indexName);
585
+ const request = index.openCursor(query, direction);
586
+ request.onerror = (e) => obs.error(e);
587
+ request.onsuccess = (event) => {
588
+ const cursor = event.target.result;
589
+ if (cursor) {
590
+ cursor.delete();
591
+ cursor.continue();
592
+ }
593
+ else {
594
+ obs.next();
595
+ obs.complete();
596
+ }
597
+ };
598
+ })
599
+ .catch((reason) => obs.error(reason));
600
+ });
601
+ }
602
+ /**
603
+ * Clear the data in the objectStore.
565
604
  * @param storeName The name of the store to have the entries deleted
566
605
  */
567
606
  clear(storeName) {
@@ -572,8 +611,9 @@ class NgxIndexedDBService {
572
611
  const transaction = createTransaction(db, optionsGenerator(DBMode.readwrite, storeName, (e) => obs.error(e)));
573
612
  const objectStore = transaction.objectStore(storeName);
574
613
  objectStore.clear();
614
+ transaction.onerror = (e) => obs.error(e);
575
615
  transaction.oncomplete = () => {
576
- obs.next(true);
616
+ obs.next();
577
617
  obs.complete();
578
618
  };
579
619
  })
@@ -581,16 +621,16 @@ class NgxIndexedDBService {
581
621
  });
582
622
  }
583
623
  /**
584
- * Returns true if successfully delete the DB.
624
+ * Delete database.
585
625
  */
586
626
  deleteDatabase() {
587
627
  return new Observable((obs) => {
588
628
  openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
589
629
  .then(async (db) => {
590
- await db.close();
630
+ db.close();
591
631
  const deleteDBRequest = this.indexedDB.deleteDatabase(this.dbConfig.name);
592
632
  deleteDBRequest.onsuccess = () => {
593
- obs.next(true);
633
+ obs.next();
594
634
  obs.complete();
595
635
  };
596
636
  deleteDBRequest.onerror = (error) => obs.error(error);
@@ -603,63 +643,75 @@ class NgxIndexedDBService {
603
643
  });
604
644
  }
605
645
  /**
606
- * Returns the open cursor event
607
- * @param storeName The name of the store to have the entries deleted
608
- * @param keyRange The key range which the cursor should be open on
609
- * @param direction A string telling the cursor which direction to travel. The default is next
646
+ * Returns the open cursor
647
+ * If no matching data are present, the observable is completed immediately.
648
+ * @param options The options to open the cursor
649
+ * @param options.storeName The name of the store to have the entries deleted
650
+ * @param options.query The key or key range criteria to apply
651
+ * @param options.direction A string telling the cursor which direction to travel
652
+ * @param options.mode The transaction mode.
610
653
  */
611
- openCursor(storeName, keyRange, direction = 'next') {
654
+ openCursor(options) {
655
+ const { storeName, query, direction, mode = DBMode.readonly } = options;
612
656
  return new Observable((obs) => {
613
657
  openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
614
658
  .then((db) => {
615
659
  validateBeforeTransaction(db, storeName, (e) => obs.error(e));
616
- const transaction = createTransaction(db, optionsGenerator(DBMode.readwrite, storeName, obs.error));
660
+ const transaction = createTransaction(db, optionsGenerator(mode, storeName, obs.error));
617
661
  const objectStore = transaction.objectStore(storeName);
618
- const request = keyRange === undefined ? objectStore.openCursor() : objectStore.openCursor(keyRange, direction);
662
+ const request = objectStore.openCursor(query, direction);
663
+ transaction.oncomplete = () => obs.complete();
664
+ request.onerror = (e) => obs.error(e);
619
665
  request.onsuccess = (event) => {
620
- obs.next(event);
621
- obs.complete();
666
+ const cursor = event.target.result;
667
+ if (cursor) {
668
+ obs.next(cursor);
669
+ }
622
670
  };
623
671
  })
624
672
  .catch((reason) => obs.error(reason));
625
673
  });
626
674
  }
627
675
  /**
628
- * Open a cursor by index filter.
629
- * @param storeName The name of the store to query.
630
- * @param indexName The index name to filter.
631
- * @param keyRange The range value and criteria to apply on the index.
676
+ * Open a cursor by index filter
677
+ * If no matching data are present, the observable is completed immediately.
678
+ * @param options The options to open the cursor
679
+ * @param options.storeName The name of the store to query
680
+ * @param options.indexName The index name to filter
681
+ * @param options.query The key or key range criteria to apply
682
+ * @param options.direction A string telling the cursor which direction to travel
683
+ * @param options.mode The transaction mode.
632
684
  */
633
- openCursorByIndex(storeName, indexName, keyRange, direction = 'next', mode = DBMode.readonly) {
634
- const obs = new Subject();
635
- openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
636
- .then((db) => {
637
- validateBeforeTransaction(db, storeName, (reason) => {
638
- obs.error(reason);
639
- });
640
- const transaction = createTransaction(db, optionsGenerator(mode, storeName, (reason) => {
641
- obs.error(reason);
642
- }, () => {
643
- obs.next();
644
- }));
645
- const objectStore = transaction.objectStore(storeName);
646
- const index = objectStore.index(indexName);
647
- const request = index.openCursor(keyRange, direction);
648
- request.onsuccess = (event) => {
649
- obs.next(event);
650
- };
651
- })
652
- .catch((reason) => obs.error(reason));
653
- return obs;
685
+ openCursorByIndex(options) {
686
+ const { storeName, indexName, query, direction, mode = DBMode.readonly } = options;
687
+ return new Observable((obs) => {
688
+ openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
689
+ .then((db) => {
690
+ validateBeforeTransaction(db, storeName, (e) => obs.error(e));
691
+ const transaction = createTransaction(db, optionsGenerator(mode, storeName, obs.error));
692
+ const objectStore = transaction.objectStore(storeName);
693
+ const index = objectStore.index(indexName);
694
+ const request = index.openCursor(query, direction);
695
+ transaction.oncomplete = () => obs.complete();
696
+ request.onerror = (e) => obs.error(e);
697
+ request.onsuccess = (event) => {
698
+ const cursor = event.target.result;
699
+ if (cursor) {
700
+ obs.next(cursor);
701
+ }
702
+ };
703
+ })
704
+ .catch((reason) => obs.error(reason));
705
+ });
654
706
  }
655
707
  /**
656
708
  * Returns all items by an index.
657
709
  * @param storeName The name of the store to query
658
710
  * @param indexName The index name to filter
659
- * @param keyRange The range value and criteria to apply on the index.
711
+ * @param query The key or key range criteria to apply
712
+ * @param direction A string telling the cursor which direction to travel.
660
713
  */
661
- getAllByIndex(storeName, indexName, keyRange) {
662
- const data = [];
714
+ getAllByIndex(storeName, indexName, query, direction) {
663
715
  return new Observable((obs) => {
664
716
  openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
665
717
  .then((db) => {
@@ -667,7 +719,9 @@ class NgxIndexedDBService {
667
719
  const transaction = createTransaction(db, optionsGenerator(DBMode.readonly, storeName, obs.error));
668
720
  const objectStore = transaction.objectStore(storeName);
669
721
  const index = objectStore.index(indexName);
670
- const request = index.openCursor(keyRange);
722
+ const request = index.openCursor(query, direction);
723
+ const data = [];
724
+ request.onerror = (e) => obs.error(e);
671
725
  request.onsuccess = (event) => {
672
726
  const cursor = event.target.result;
673
727
  if (cursor) {
@@ -686,11 +740,10 @@ class NgxIndexedDBService {
686
740
  /**
687
741
  * Returns all primary keys by an index.
688
742
  * @param storeName The name of the store to query
689
- * @param indexName The index name to filter
690
- * @param keyRange The range value and criteria to apply on the index.
743
+ * @param query The key or key range criteria to apply
744
+ * @param direction A string telling the cursor which direction to travel.
691
745
  */
692
- getAllKeysByIndex(storeName, indexName, keyRange) {
693
- const data = [];
746
+ getAllKeysByIndex(storeName, indexName, query, direction) {
694
747
  return new Observable((obs) => {
695
748
  openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
696
749
  .then((db) => {
@@ -698,11 +751,17 @@ class NgxIndexedDBService {
698
751
  const transaction = createTransaction(db, optionsGenerator(DBMode.readonly, storeName, obs.error));
699
752
  const objectStore = transaction.objectStore(storeName);
700
753
  const index = objectStore.index(indexName);
701
- const request = index.openKeyCursor(keyRange);
754
+ const data = [];
755
+ const request = index.openKeyCursor(query, direction);
756
+ request.onerror = (e) => obs.error(e);
702
757
  request.onsuccess = (event) => {
703
758
  const cursor = event.target.result;
704
759
  if (cursor) {
705
- data.push({ primaryKey: cursor.primaryKey, key: cursor.key });
760
+ const { primaryKey, key } = cursor;
761
+ data.push({
762
+ primaryKey,
763
+ key,
764
+ });
706
765
  cursor.continue();
707
766
  }
708
767
  else {
@@ -717,16 +776,16 @@ class NgxIndexedDBService {
717
776
  /**
718
777
  * Returns the number of rows in a store.
719
778
  * @param storeName The name of the store to query
720
- * @param keyRange The range value and criteria to apply.
779
+ * @param query The key or key range criteria to apply.
721
780
  */
722
- count(storeName, keyRange) {
781
+ count(storeName, query) {
723
782
  return new Observable((obs) => {
724
783
  openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
725
784
  .then((db) => {
726
785
  validateBeforeTransaction(db, storeName, (e) => obs.error(e));
727
786
  const transaction = createTransaction(db, optionsGenerator(DBMode.readonly, storeName, obs.error));
728
787
  const objectStore = transaction.objectStore(storeName);
729
- const request = objectStore.count(keyRange);
788
+ const request = objectStore.count(query);
730
789
  request.onerror = (e) => obs.error(e);
731
790
  request.onsuccess = (e) => {
732
791
  obs.next(e.target.result);
@@ -737,11 +796,12 @@ class NgxIndexedDBService {
737
796
  });
738
797
  }
739
798
  /**
740
- * Returns the number of rows in a store.
799
+ * Returns the number of records within a key range.
741
800
  * @param storeName The name of the store to query
742
- * @param keyRange The range value and criteria to apply.
801
+ * @param indexName The index name to filter
802
+ * @param query The key or key range criteria to apply.
743
803
  */
744
- countByIndex(storeName, indexName, keyRange) {
804
+ countByIndex(storeName, indexName, query) {
745
805
  return new Observable((obs) => {
746
806
  openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
747
807
  .then((db) => {
@@ -749,7 +809,7 @@ class NgxIndexedDBService {
749
809
  const transaction = createTransaction(db, optionsGenerator(DBMode.readonly, storeName, obs.error));
750
810
  const objectStore = transaction.objectStore(storeName);
751
811
  const index = objectStore.index(indexName);
752
- const request = index.count(keyRange);
812
+ const request = index.count(query);
753
813
  request.onerror = (e) => obs.error(e);
754
814
  request.onsuccess = (e) => {
755
815
  obs.next(e.target.result);
@@ -773,7 +833,7 @@ class NgxIndexedDBService {
773
833
  return new Observable((obs) => {
774
834
  openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
775
835
  .then((db) => {
776
- obs.next([...db.objectStoreNames]);
836
+ obs.next(Array.from(db.objectStoreNames));
777
837
  obs.complete();
778
838
  })
779
839
  .catch((reason) => obs.error(reason));
@@ -821,6 +881,9 @@ __decorate([
821
881
  __decorate([
822
882
  CloseDbConnection()
823
883
  ], NgxIndexedDBService.prototype, "deleteByKey", null);
884
+ __decorate([
885
+ CloseDbConnection()
886
+ ], NgxIndexedDBService.prototype, "deleteAllByIndex", null);
824
887
  __decorate([
825
888
  CloseDbConnection()
826
889
  ], NgxIndexedDBService.prototype, "clear", null);
@@ -856,7 +919,7 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "18.2.13", ngImpo
856
919
  }] }, { type: IDBFactory, decorators: [{
857
920
  type: Inject,
858
921
  args: [INDEXED_DB]
859
- }] }], propDecorators: { getDatabaseVersion: [], add: [], bulkAdd: [], bulkDelete: [], getByKey: [], bulkGet: [], getByID: [], getByIndex: [], getAll: [], update: [], bulkPut: [], delete: [], deleteByKey: [], clear: [], deleteDatabase: [], openCursor: [], openCursorByIndex: [], getAllByIndex: [], getAllKeysByIndex: [], count: [], countByIndex: [], getAllObjectStoreNames: [] } });
922
+ }] }], propDecorators: { getDatabaseVersion: [], add: [], bulkAdd: [], bulkDelete: [], getByKey: [], bulkGet: [], getByID: [], getByIndex: [], getAll: [], update: [], bulkPut: [], delete: [], deleteByKey: [], deleteAllByIndex: [], clear: [], deleteDatabase: [], openCursor: [], openCursorByIndex: [], getAllByIndex: [], getAllKeysByIndex: [], count: [], countByIndex: [], getAllObjectStoreNames: [] } });
860
923
 
861
924
  /**
862
925
  *