@cocreate/indexeddb 1.4.3 → 1.4.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.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## [1.4.4](https://github.com/CoCreate-app/CoCreate-indexeddb/compare/v1.4.3...v1.4.4) (2022-11-28)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * if condition data.filter ([7d1aee2](https://github.com/CoCreate-app/CoCreate-indexeddb/commit/7d1aee263307709e82ee522e954ffa8b37ad0c29))
7
+
1
8
  ## [1.4.3](https://github.com/CoCreate-app/CoCreate-indexeddb/compare/v1.4.2...v1.4.3) (2022-11-28)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/indexeddb",
3
- "version": "1.4.3",
3
+ "version": "1.4.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",
@@ -61,8 +61,8 @@
61
61
  "webpack-log": "^3.0.1"
62
62
  },
63
63
  "dependencies": {
64
- "@cocreate/docs": "^1.4.7",
65
- "@cocreate/hosting": "^1.6.6",
66
- "@cocreate/utils": "^1.12.4"
64
+ "@cocreate/docs": "^1.4.8",
65
+ "@cocreate/hosting": "^1.6.7",
66
+ "@cocreate/utils": "^1.12.6"
67
67
  }
68
68
  }
package/src/index.js CHANGED
@@ -865,8 +865,19 @@ const readDocuments = (data, database, collection) => {
865
865
  count.onsuccess = function() {
866
866
  data.filter.count = count.result
867
867
  }
868
-
868
+
869
+ let isIndex = false
870
+ let indexName;
869
871
  if (data.filter && data.filter.sort && data.filter.sort[0] && data.filter.sort[0].name) {
872
+ indexName = data.filter.sort[0].name
873
+ if (indexName.includes('-')) {
874
+ isIndex = false
875
+ }
876
+ else
877
+ isIndex = true
878
+ }
879
+
880
+ if (isIndex) {
870
881
  let sortType = data.filter.sort[0].type
871
882
  if (sortType == -1)
872
883
  sortType = 'prev'
@@ -874,7 +885,6 @@ const readDocuments = (data, database, collection) => {
874
885
  sortType = 'next'
875
886
 
876
887
  let indexNames = Array.from(objectStore.indexNames)
877
- let indexName = data.filter.sort[0].name
878
888
  let indexExist = indexNames.includes(indexName)
879
889
  if (!indexExist) {
880
890
  db.close()
@@ -884,20 +894,20 @@ const readDocuments = (data, database, collection) => {
884
894
  objectStore.createIndex(indexName, indexName, {unique: false})
885
895
  const indexStore = objectStore.index(indexName);
886
896
 
887
- readDocs(data, database, collection, indexStore, sortType).then((results) => {
897
+ readDocs(data, database, collection, indexStore, isIndex, sortType).then((results) => {
888
898
  db2.close()
889
899
  resolve(results)
890
900
  })
891
901
  })
892
902
  } else {
893
903
  const indexStore = objectStore.index(indexName);
894
- readDocs(data, database, collection, indexStore, sortType).then((results) => {
904
+ readDocs(data, database, collection, indexStore, isIndex, sortType).then((results) => {
895
905
  db.close()
896
906
  resolve(results)
897
907
  })
898
908
  }
899
909
  } else {
900
- readDocs(data, database, collection, objectStore).then((results) => {
910
+ readDocs(data, database, collection, objectStore, isIndex).then((results) => {
901
911
  db.close()
902
912
  resolve(results)
903
913
  })
@@ -918,14 +928,18 @@ const readDocuments = (data, database, collection) => {
918
928
  })
919
929
  }
920
930
 
921
- async function readDocs(data, database, collection, objectStore, sortType) {
931
+ async function readDocs(data, database, collection, objectStore, isIndex, sortType) {
922
932
  return new Promise((resolve, reject) => {
923
933
 
924
- let results = [];
925
- let index = data.filter.startIndex || 0
926
- let limit = data.filter.limit
927
- if (limit)
928
- limit = index + limit;
934
+ let results = [], index = 0, limit
935
+ if (data.filter) {
936
+ if (data.filter.startIndex)
937
+ index = data.filter.startIndex
938
+ if (data.filter.limit)
939
+ limit = data.filter.limit
940
+ if (limit)
941
+ limit = index + limit;
942
+ }
929
943
 
930
944
  const request = objectStore.openCursor(null, sortType);
931
945
  request.onsuccess = function() {
@@ -933,13 +947,15 @@ async function readDocs(data, database, collection, objectStore, sortType) {
933
947
  if (cursor) {
934
948
  let isFilter = true;
935
949
  let value = cursor.value;
936
- if (data.filter && data.filter.query)
937
- isFilter = queryData(value, data.filter.query);
938
- if (isFilter && data.filter.search)
939
- isFilter = searchData(value, data.filter.search);
950
+ if (data.filter) {
951
+ if (data.filter.query)
952
+ isFilter = queryData(value, data.filter.query);
953
+ if (isFilter && data.filter.search)
954
+ isFilter = searchData(value, data.filter.search);
955
+ }
940
956
  if (isFilter !== false)
941
957
  results.push(value)
942
- if (limit && limit == results.length) {
958
+ if (isIndex && limit && limit == results.length) {
943
959
  results = results.slice(index, limit)
944
960
  resolve(results)
945
961
  } else