@cocreate/indexeddb 1.14.3 → 1.14.4

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.
Files changed (3) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/package.json +2 -2
  3. package/src/index.js +141 -123
package/CHANGELOG.md CHANGED
@@ -1,3 +1,14 @@
1
+ ## [1.14.4](https://github.com/CoCreate-app/CoCreate-indexeddb/compare/v1.14.3...v1.14.4) (2023-10-14)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * bump dependencies ([9cb8c0b](https://github.com/CoCreate-app/CoCreate-indexeddb/commit/9cb8c0b4518616d2da792ecc13056a60fca10b43))
7
+ * error handeling if array does exist ([29e92fb](https://github.com/CoCreate-app/CoCreate-indexeddb/commit/29e92fbf8f162cb9ae372d778240e7fb165f7cd7))
8
+ * improved cursor handling ([0a64f78](https://github.com/CoCreate-app/CoCreate-indexeddb/commit/0a64f78b3c51da5548a91650df4c0adb19c023e5))
9
+ * improved handling of cursor conditions ([a141167](https://github.com/CoCreate-app/CoCreate-indexeddb/commit/a141167fe771488183e71dd1bfa3ab38ed00188e))
10
+ * update.object upsert without_id ([740bf7e](https://github.com/CoCreate-app/CoCreate-indexeddb/commit/740bf7e455ee906a114cafa833878822b6fd76cb))
11
+
1
12
  ## [1.14.3](https://github.com/CoCreate-app/CoCreate-indexeddb/compare/v1.14.2...v1.14.3) (2023-10-09)
2
13
 
3
14
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/indexeddb",
3
- "version": "1.14.3",
3
+ "version": "1.14.4",
4
4
  "description": "A simple indexeddb component in vanilla javascript. Easily configured using HTML5 data-attributes and/or JavaScript API.",
5
5
  "keywords": [
6
6
  "indexeddb",
@@ -58,6 +58,6 @@
58
58
  "webpack-log": "^3.0.1"
59
59
  },
60
60
  "dependencies": {
61
- "@cocreate/utils": "^1.25.2"
61
+ "@cocreate/utils": "^1.25.3"
62
62
  }
63
63
  }
package/src/index.js CHANGED
@@ -433,9 +433,11 @@ async function processObject(data, newData, database, array, type) {
433
433
 
434
434
  let arrayExist = db.objectStoreNames.contains(array)
435
435
  if (!arrayExist) {
436
- if (data.$filter || data.method == 'read.object' && (!data.object || data.object && !data.object.length)) {
436
+ if (data.method == 'create.object' || data.method == 'update.object') {
437
437
  db.close()
438
438
  db = await processDatabase({ method: 'get.database', database, array })
439
+ } else {
440
+ throw new Error({ error: "array does not exist", array });
439
441
  }
440
442
  }
441
443
 
@@ -551,6 +553,8 @@ async function processObject(data, newData, database, array, type) {
551
553
  if (limit)
552
554
  limit = (index || 0) + limit;
553
555
  }
556
+ } else if (data.method == 'delete.object' || data.method == 'update.object' && !range && !upsert && !isFilter) {
557
+ continue
554
558
  }
555
559
 
556
560
  await openCursor(objectStore, range, direction, data, newData, isFilter, limit, database, array, upsert, type, i, globalOperators);
@@ -569,6 +573,142 @@ async function processObject(data, newData, database, array, type) {
569
573
  db.close()
570
574
  }
571
575
 
576
+ function openCursor(objectStore, range, direction, data, newData, isFilter, limit, database, array, upsert, type, i, globalOperators) {
577
+ return new Promise(async (resolve, reject) => {
578
+ const request = objectStore.openCursor(range || null, direction);// next, prev
579
+
580
+ let hasUpdated
581
+ request.onsuccess = async () => {
582
+ let cursor = request.result
583
+
584
+ if (data.method === 'create.object'
585
+ || data.method === 'update.object'
586
+ && (!range && !isFilter && upsert || !cursor && upsert && !hasUpdated)) {
587
+ let isMatch = true
588
+ if (isFilter) {
589
+ if (cursor)
590
+ isMatch = filter(objectStore, data[type][i], data[type][i], cursor.value)
591
+ else
592
+ isMatch = false
593
+ }
594
+
595
+ if (isMatch !== false) {
596
+ try {
597
+ data[type][i]._id = ObjectId(data[type][i]._id);
598
+ } catch (error) {
599
+ data[type][i]._id = ObjectId()
600
+ }
601
+
602
+ let result
603
+ if (data.method == 'create.object') {
604
+ result = await add(objectStore, data[type][i])
605
+ } else if (data.method == 'update.object') {
606
+ result = await put(objectStore, data[type][i])
607
+ }
608
+
609
+ if (result)
610
+ newData.push({ $storage: 'indexeddb', $database: database, $array: array, ...result })
611
+
612
+ resolve();
613
+ }
614
+ } else if (cursor) {
615
+ let isMatch = true
616
+
617
+ if (isFilter)
618
+ isMatch = filter(objectStore, data, data[type][i], cursor.value)
619
+
620
+ if (isMatch !== false) {
621
+ let result = cursor.value
622
+
623
+ if (data.method == 'update.object') {
624
+ let update = createUpdate(cursor.value, data[type][i], globalOperators)
625
+ if (update)
626
+ result = await put(objectStore, update)
627
+ hasUpdated = true
628
+ // set dotnotation for keys with $operators for items that end in [] to be used by socket.id
629
+ // TODO: if update.$<operator>.someKey[] requires the index of inerted item added to the field name. update.$<operator>.someKey[<index>]
630
+ // TODO: if $addToSet get field name and item if it does not exist.
631
+ // TODO: if $pull get field name and find if item exist and delete.
632
+ } else if (data.method == 'delete.object') {
633
+ result = cursor.value
634
+ cursor.delete()
635
+ }
636
+
637
+ newData.push({ $storage: 'indexeddb', $database: database, $array: array, ...result })
638
+ }
639
+
640
+ if (!limit || limit && limit < newData.length) {
641
+ cursor.continue();
642
+ } else {
643
+ cursor.close()
644
+ }
645
+ } else {
646
+ resolve();
647
+ }
648
+
649
+ }
650
+
651
+ request.onerror = () => {
652
+ reject(request.error)
653
+ }
654
+ });
655
+ }
656
+
657
+ function add(objectStore, object) {
658
+ return new Promise((resolve, reject) => {
659
+ const request = objectStore.add(object);
660
+ request.onsuccess = () => resolve(object);
661
+ request.onerror = () => reject(request.error);
662
+ });
663
+ }
664
+
665
+ function get(objectStore, object) {
666
+ return new Promise((resolve, reject) => {
667
+ const request = objectStore.get(object._id);
668
+ request.onsuccess = () => resolve(request.result);
669
+ request.onerror = () => reject(request.error);
670
+ });
671
+ }
672
+
673
+ function put(objectStore, object) {
674
+ return new Promise((resolve, reject) => {
675
+ const request = objectStore.put(object);
676
+ request.onsuccess = () => resolve(object);
677
+ request.onerror = () => reject(request.error);
678
+ });
679
+ }
680
+
681
+ function deleteObject(objectStore, object) {
682
+ return new Promise((resolve, reject) => {
683
+ const request = objectStore.delete(object._id);
684
+ request.onsuccess = () => resolve(object);
685
+ request.onerror = () => reject(request.error);
686
+ });
687
+ }
688
+
689
+
690
+ function filter(objectStore, data, object, result) {
691
+ let isMatch = true;
692
+ let filter = data.$filter || object.$filter
693
+ if (filter) {
694
+ try {
695
+ let count = objectStore.count();
696
+ count.onsuccess = function () {
697
+ filter.count = count.result
698
+ }
699
+ } catch (error) {
700
+ errorHandler(data, error, database, array)
701
+ }
702
+
703
+ if (filter.query)
704
+ isMatch = queryData(result, filter.query);
705
+ if (isMatch && filter.search)
706
+ isMatch = searchData(result, filter.search);
707
+ }
708
+
709
+ return isMatch
710
+ }
711
+
572
712
  function getGlobalOperators(data, object = {}) {
573
713
  for (let operator of Object.keys(data)) {
574
714
  if (!operator.startsWith('$'))
@@ -694,128 +834,6 @@ function dotNotationToObjectUpdate(data, object = {}) {
694
834
  }
695
835
  }
696
836
 
697
- function openCursor(objectStore, range, direction, data, newData, isFilter, limit, database, array, upsert, type, i, globalOperators) {
698
- return new Promise(async (resolve, reject) => {
699
- const request = objectStore.openCursor(range || null, direction);// next, prev
700
-
701
- request.onsuccess = async () => {
702
- let cursor = request.result
703
-
704
- if (!cursor && data.method === 'update.object' && upsert) {
705
- let isMatch = true
706
- if (isFilter)
707
- isMatch = filter(objectStore, data[type][i], data[type][i])
708
-
709
- if (isMatch !== false) {
710
- try {
711
- data[type][i]._id = ObjectId(data[type][i]._id);
712
- } catch (error) {
713
- data[type][i]._id = ObjectId()
714
- }
715
-
716
- let result = await put(objectStore, data[type][i])
717
- newData.push({ $storage: 'indexeddb', $database: database, $array: array, ...result })
718
- }
719
- } else if (cursor) {
720
- let isMatch = true
721
-
722
- if (isFilter)
723
- isMatch = filter(objectStore, data, data[type][i], cursor.value)
724
-
725
- if (isMatch !== false) {
726
- let result = cursor.value
727
- if (data.method == 'create.object') {
728
- result = await add(objectStore, data[type][i])
729
- } else if (data.method == 'update.object') {
730
- let update = createUpdate(cursor.value, data[type][i], globalOperators)
731
- if (update)
732
- result = await put(objectStore, update)
733
- // set dotnotation for keys with $operators for items that end in [] to be used by socket.id
734
- // TODO: if update.$<operator>.someKey[] requires the index of inerted item added to the field name. update.$<operator>.someKey[<index>]
735
- // TODO: if $addToSet get field name and item if it does not exist.
736
- // TODO: if $pull get field name and find if item exist and delete.
737
- } else if (data.method == 'delete.object') {
738
- result = cursor.value
739
- cursor.delete()
740
- }
741
-
742
- newData.push({ $storage: 'indexeddb', $database: database, $array: array, ...result })
743
- }
744
-
745
- if (!limit || limit && limit < newData.length) {
746
- cursor.continue();
747
- } else {
748
- cursor.close()
749
- }
750
- } else {
751
- resolve();
752
- }
753
- }
754
-
755
- request.onerror = () => {
756
- reject(request.error)
757
- }
758
- });
759
- }
760
-
761
- function add(objectStore, object) {
762
- return new Promise((resolve, reject) => {
763
- const request = objectStore.add(object);
764
- request.onsuccess = () => resolve(object);
765
- request.onerror = () => reject(request.error);
766
- });
767
- }
768
-
769
- function get(objectStore, object) {
770
- return new Promise((resolve, reject) => {
771
- const request = objectStore.get(object._id);
772
- request.onsuccess = () => resolve(request.result);
773
- request.onerror = () => reject(request.error);
774
- });
775
- }
776
-
777
- function put(objectStore, object) {
778
- return new Promise((resolve, reject) => {
779
- const request = objectStore.put(object);
780
- request.onsuccess = () => resolve(object);
781
- request.onerror = () => reject(request.error);
782
- });
783
- }
784
-
785
- function deleteObject(objectStore, object) {
786
- return new Promise((resolve, reject) => {
787
- const request = objectStore.delete(object._id);
788
- request.onsuccess = () => resolve(object);
789
- request.onerror = () => reject(request.error);
790
- });
791
- }
792
-
793
-
794
- function filter(objectStore, data, object, result) {
795
- let isMatch = true;
796
-
797
- if (data.$filter || object.$filter) {
798
- let count = objectStore.count();
799
- count.onsuccess = function () {
800
- data.$filter.count = count.result
801
- }
802
- }
803
-
804
- if (data.$filter) {
805
- if (data.$filter.query)
806
- isMatch = queryData(result, data.$filter.query);
807
- if (isMatch && data.$filter.search)
808
- isMatch = searchData(result, data.$filter.search);
809
- }
810
-
811
- let modify = true
812
- if (result && object.modified && result.modified)
813
- if (object.modified.on < result.modified.on)
814
- modify = false
815
-
816
- return isMatch
817
- }
818
-
819
837
  function createData(data, newData, type) {
820
838
  data.request = data[type] || {}
821
839