ngx-indexed-db 19.3.2 → 19.3.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);
@@ -517,22 +517,26 @@ class NgxIndexedDBService {
517
517
  /**
518
518
  * Returns all items from the store after delete.
519
519
  * @param storeName The name of the store to have the entry deleted
520
- * @param key The key of the entry to be deleted
520
+ * @param query The key or key range criteria to apply
521
521
  */
522
- delete(storeName, key) {
522
+ delete(storeName, query) {
523
523
  return new Observable((obs) => {
524
524
  openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
525
525
  .then((db) => {
526
526
  validateBeforeTransaction(db, storeName, (e) => obs.error(e));
527
527
  const transaction = createTransaction(db, optionsGenerator(DBMode.readwrite, storeName, (e) => obs.error(e)));
528
528
  const objectStore = transaction.objectStore(storeName);
529
- objectStore.delete(key);
529
+ objectStore.delete(query);
530
+ transaction.onerror = (e) => obs.error(e);
530
531
  transaction.oncomplete = () => {
531
532
  this.getAll(storeName)
532
533
  .pipe(take(1))
533
- .subscribe((newValues) => {
534
- obs.next(newValues);
535
- obs.complete();
534
+ .subscribe({
535
+ next: (newValues) => {
536
+ obs.next(newValues);
537
+ },
538
+ error: (e) => obs.error(e),
539
+ complete: () => obs.complete(),
536
540
  });
537
541
  };
538
542
  })
@@ -540,28 +544,29 @@ class NgxIndexedDBService {
540
544
  });
541
545
  }
542
546
  /**
543
- * Returns true from the store after a successful delete.
547
+ * Returns after a successful delete.
544
548
  * @param storeName The name of the store to have the entry deleted
545
- * @param key The key of the entry to be deleted
549
+ * @param query The key or key range criteria to apply
546
550
  */
547
- deleteByKey(storeName, key) {
551
+ deleteByKey(storeName, query) {
548
552
  return new Observable((obs) => {
549
553
  openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
550
554
  .then((db) => {
551
555
  validateBeforeTransaction(db, storeName, (e) => obs.error(e));
552
556
  const transaction = createTransaction(db, optionsGenerator(DBMode.readwrite, storeName, (e) => obs.error(e)));
553
557
  const objectStore = transaction.objectStore(storeName);
558
+ objectStore.delete(query);
559
+ transaction.onerror = (e) => obs.error(e);
554
560
  transaction.oncomplete = () => {
555
- obs.next(true);
561
+ obs.next();
556
562
  obs.complete();
557
563
  };
558
- objectStore.delete(key);
559
564
  })
560
565
  .catch((reason) => obs.error(reason));
561
566
  });
562
567
  }
563
568
  /**
564
- * Returns true if successfully delete all entries from the store.
569
+ * Clear the data in the objectStore.
565
570
  * @param storeName The name of the store to have the entries deleted
566
571
  */
567
572
  clear(storeName) {
@@ -572,8 +577,9 @@ class NgxIndexedDBService {
572
577
  const transaction = createTransaction(db, optionsGenerator(DBMode.readwrite, storeName, (e) => obs.error(e)));
573
578
  const objectStore = transaction.objectStore(storeName);
574
579
  objectStore.clear();
580
+ transaction.onerror = (e) => obs.error(e);
575
581
  transaction.oncomplete = () => {
576
- obs.next(true);
582
+ obs.next();
577
583
  obs.complete();
578
584
  };
579
585
  })
@@ -581,16 +587,16 @@ class NgxIndexedDBService {
581
587
  });
582
588
  }
583
589
  /**
584
- * Returns true if successfully delete the DB.
590
+ * Delete database.
585
591
  */
586
592
  deleteDatabase() {
587
593
  return new Observable((obs) => {
588
594
  openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
589
595
  .then(async (db) => {
590
- await db.close();
596
+ db.close();
591
597
  const deleteDBRequest = this.indexedDB.deleteDatabase(this.dbConfig.name);
592
598
  deleteDBRequest.onsuccess = () => {
593
- obs.next(true);
599
+ obs.next();
594
600
  obs.complete();
595
601
  };
596
602
  deleteDBRequest.onerror = (error) => obs.error(error);
@@ -603,63 +609,75 @@ class NgxIndexedDBService {
603
609
  });
604
610
  }
605
611
  /**
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
612
+ * Returns the open cursor
613
+ * If no matching data are present, the observable is completed immediately.
614
+ * @param options The options to open the cursor
615
+ * @param options.storeName The name of the store to have the entries deleted
616
+ * @param options.query The key or key range criteria to apply
617
+ * @param options.direction A string telling the cursor which direction to travel
618
+ * @param options.mode The transaction mode.
610
619
  */
611
- openCursor(storeName, keyRange, direction = 'next') {
620
+ openCursor(options) {
621
+ const { storeName, query, direction, mode = DBMode.readonly } = options;
612
622
  return new Observable((obs) => {
613
623
  openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
614
624
  .then((db) => {
615
625
  validateBeforeTransaction(db, storeName, (e) => obs.error(e));
616
- const transaction = createTransaction(db, optionsGenerator(DBMode.readwrite, storeName, obs.error));
626
+ const transaction = createTransaction(db, optionsGenerator(mode, storeName, obs.error));
617
627
  const objectStore = transaction.objectStore(storeName);
618
- const request = keyRange === undefined ? objectStore.openCursor() : objectStore.openCursor(keyRange, direction);
628
+ const request = objectStore.openCursor(query, direction);
629
+ transaction.oncomplete = () => obs.complete();
630
+ request.onerror = (e) => obs.error(e);
619
631
  request.onsuccess = (event) => {
620
- obs.next(event);
621
- obs.complete();
632
+ const cursor = event.target.result;
633
+ if (cursor) {
634
+ obs.next(cursor);
635
+ }
622
636
  };
623
637
  })
624
638
  .catch((reason) => obs.error(reason));
625
639
  });
626
640
  }
627
641
  /**
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.
642
+ * Open a cursor by index filter
643
+ * If no matching data are present, the observable is completed immediately.
644
+ * @param options The options to open the cursor
645
+ * @param options.storeName The name of the store to query
646
+ * @param options.indexName The index name to filter
647
+ * @param options.query The key or key range criteria to apply
648
+ * @param options.direction A string telling the cursor which direction to travel
649
+ * @param options.mode The transaction mode.
632
650
  */
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;
651
+ openCursorByIndex(options) {
652
+ const { storeName, indexName, query, direction, mode = DBMode.readonly } = options;
653
+ return new Observable((obs) => {
654
+ openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
655
+ .then((db) => {
656
+ validateBeforeTransaction(db, storeName, (e) => obs.error(e));
657
+ const transaction = createTransaction(db, optionsGenerator(mode, storeName, obs.error));
658
+ const objectStore = transaction.objectStore(storeName);
659
+ const index = objectStore.index(indexName);
660
+ const request = index.openCursor(query, direction);
661
+ transaction.oncomplete = () => obs.complete();
662
+ request.onerror = (e) => obs.error(e);
663
+ request.onsuccess = (event) => {
664
+ const cursor = event.target.result;
665
+ if (cursor) {
666
+ obs.next(cursor);
667
+ }
668
+ };
669
+ })
670
+ .catch((reason) => obs.error(reason));
671
+ });
654
672
  }
655
673
  /**
656
674
  * Returns all items by an index.
657
675
  * @param storeName The name of the store to query
658
676
  * @param indexName The index name to filter
659
- * @param keyRange The range value and criteria to apply on the index.
677
+ * @param query The key or key range criteria to apply
678
+ * @param direction A string telling the cursor which direction to travel.
660
679
  */
661
- getAllByIndex(storeName, indexName, keyRange) {
662
- const data = [];
680
+ getAllByIndex(storeName, indexName, query, direction) {
663
681
  return new Observable((obs) => {
664
682
  openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
665
683
  .then((db) => {
@@ -667,7 +685,9 @@ class NgxIndexedDBService {
667
685
  const transaction = createTransaction(db, optionsGenerator(DBMode.readonly, storeName, obs.error));
668
686
  const objectStore = transaction.objectStore(storeName);
669
687
  const index = objectStore.index(indexName);
670
- const request = index.openCursor(keyRange);
688
+ const request = index.openCursor(query, direction);
689
+ const data = [];
690
+ request.onerror = (e) => obs.error(e);
671
691
  request.onsuccess = (event) => {
672
692
  const cursor = event.target.result;
673
693
  if (cursor) {
@@ -686,11 +706,10 @@ class NgxIndexedDBService {
686
706
  /**
687
707
  * Returns all primary keys by an index.
688
708
  * @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.
709
+ * @param query The key or key range criteria to apply
710
+ * @param direction A string telling the cursor which direction to travel.
691
711
  */
692
- getAllKeysByIndex(storeName, indexName, keyRange) {
693
- const data = [];
712
+ getAllKeysByIndex(storeName, indexName, query, direction) {
694
713
  return new Observable((obs) => {
695
714
  openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
696
715
  .then((db) => {
@@ -698,11 +717,17 @@ class NgxIndexedDBService {
698
717
  const transaction = createTransaction(db, optionsGenerator(DBMode.readonly, storeName, obs.error));
699
718
  const objectStore = transaction.objectStore(storeName);
700
719
  const index = objectStore.index(indexName);
701
- const request = index.openKeyCursor(keyRange);
720
+ const data = [];
721
+ const request = index.openKeyCursor(query, direction);
722
+ request.onerror = (e) => obs.error(e);
702
723
  request.onsuccess = (event) => {
703
724
  const cursor = event.target.result;
704
725
  if (cursor) {
705
- data.push({ primaryKey: cursor.primaryKey, key: cursor.key });
726
+ const { primaryKey, key } = cursor;
727
+ data.push({
728
+ primaryKey,
729
+ key,
730
+ });
706
731
  cursor.continue();
707
732
  }
708
733
  else {
@@ -717,16 +742,16 @@ class NgxIndexedDBService {
717
742
  /**
718
743
  * Returns the number of rows in a store.
719
744
  * @param storeName The name of the store to query
720
- * @param keyRange The range value and criteria to apply.
745
+ * @param query The key or key range criteria to apply.
721
746
  */
722
- count(storeName, keyRange) {
747
+ count(storeName, query) {
723
748
  return new Observable((obs) => {
724
749
  openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
725
750
  .then((db) => {
726
751
  validateBeforeTransaction(db, storeName, (e) => obs.error(e));
727
752
  const transaction = createTransaction(db, optionsGenerator(DBMode.readonly, storeName, obs.error));
728
753
  const objectStore = transaction.objectStore(storeName);
729
- const request = objectStore.count(keyRange);
754
+ const request = objectStore.count(query);
730
755
  request.onerror = (e) => obs.error(e);
731
756
  request.onsuccess = (e) => {
732
757
  obs.next(e.target.result);
@@ -737,11 +762,12 @@ class NgxIndexedDBService {
737
762
  });
738
763
  }
739
764
  /**
740
- * Returns the number of rows in a store.
765
+ * Returns the number of records within a key range.
741
766
  * @param storeName The name of the store to query
742
- * @param keyRange The range value and criteria to apply.
767
+ * @param indexName The index name to filter
768
+ * @param query The key or key range criteria to apply.
743
769
  */
744
- countByIndex(storeName, indexName, keyRange) {
770
+ countByIndex(storeName, indexName, query) {
745
771
  return new Observable((obs) => {
746
772
  openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
747
773
  .then((db) => {
@@ -749,7 +775,7 @@ class NgxIndexedDBService {
749
775
  const transaction = createTransaction(db, optionsGenerator(DBMode.readonly, storeName, obs.error));
750
776
  const objectStore = transaction.objectStore(storeName);
751
777
  const index = objectStore.index(indexName);
752
- const request = index.count(keyRange);
778
+ const request = index.count(query);
753
779
  request.onerror = (e) => obs.error(e);
754
780
  request.onsuccess = (e) => {
755
781
  obs.next(e.target.result);
@@ -773,7 +799,7 @@ class NgxIndexedDBService {
773
799
  return new Observable((obs) => {
774
800
  openDatabase(this.indexedDB, this.dbConfig.name, this.dbConfig.version)
775
801
  .then((db) => {
776
- obs.next([...db.objectStoreNames]);
802
+ obs.next(Array.from(db.objectStoreNames));
777
803
  obs.complete();
778
804
  })
779
805
  .catch((reason) => obs.error(reason));