@cocreate/indexeddb 1.3.11 → 1.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.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ # [1.4.0](https://github.com/CoCreate-app/CoCreate-indexeddb/compare/v1.3.12...v1.4.0) (2022-11-27)
2
+
3
+
4
+ ### Features
5
+
6
+ * if sort is applied creates index on object store before opening cursor ([ce667c5](https://github.com/CoCreate-app/CoCreate-indexeddb/commit/ce667c59582cd34c4ed2d89e597cb240452c4497))
7
+
8
+ ## [1.3.12](https://github.com/CoCreate-app/CoCreate-indexeddb/compare/v1.3.11...v1.3.12) (2022-11-26)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * delete db, database and collection from doc before storing in indexeddb ([642bca1](https://github.com/CoCreate-app/CoCreate-indexeddb/commit/642bca11d98fb89b6f3c2750286971309b1417bc))
14
+
1
15
  ## [1.3.11](https://github.com/CoCreate-app/CoCreate-indexeddb/compare/v1.3.10...v1.3.11) (2022-11-26)
2
16
 
3
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/indexeddb",
3
- "version": "1.3.11",
3
+ "version": "1.4.0",
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.5",
65
- "@cocreate/hosting": "^1.6.4",
66
- "@cocreate/utils": "^1.12.0"
64
+ "@cocreate/docs": "^1.4.6",
65
+ "@cocreate/hosting": "^1.6.5",
66
+ "@cocreate/utils": "^1.12.1"
67
67
  }
68
68
  }
package/src/index.js CHANGED
@@ -424,7 +424,7 @@ const index = (action, data) => {
424
424
  let indexNames = Array.from(objectStore.indexNames)
425
425
 
426
426
  if (action == 'readIndex') {
427
- for (let index of Array.from(indexNames)) {
427
+ for (let index of indexNames) {
428
428
  indexArray.push({name: index, db: 'indexeddb', database, collection})
429
429
  }
430
430
  collectionsLength -= 1
@@ -704,6 +704,10 @@ function runDocs({action, data, objectStore, documents, filterDocs, database, co
704
704
  if (docsLength > 0) {
705
705
  for (let doc of docs) {
706
706
  // ToDo deDuplcate document_id per collection
707
+ delete doc.db
708
+ delete doc.database
709
+ delete doc.collection
710
+
707
711
  if (action == 'updateDocument' || action == 'deleteDocument') {
708
712
  if (doc._id) {
709
713
  doc['organization_id'] = data.organization_id
@@ -856,43 +860,51 @@ const readDocuments = (data, database, collection) => {
856
860
  try {
857
861
  let transaction = db.transaction([collection], "readonly");
858
862
  let objectStore = transaction.objectStore(collection);
859
- let results = [];
860
- // let index = objectStore.index(filter.name);
861
- // let request = index.openCursor(IDBKeyRange.upperBound(filter.value));
862
- let request = objectStore.openCursor();
863
- // transaction.oncomplete = resolve(results);
864
-
865
- request.onsuccess = function() {
866
- let isFilter;
867
- // if (data['organization_id']) {
868
- // data['filter']['query']['organization_id'] = data['organization_id'];
869
- // }
870
-
871
- let cursor = request.result;
872
- if (cursor) {
873
- let value = cursor.value;
874
- if (data.filter && data.filter.query)
875
- isFilter = queryData(value, data.filter.query);
876
- if (isFilter !== false)
877
- results.push(value)
878
- cursor.continue();
879
- } else {
880
- let docs = results
881
- if (data.filter) {
882
- if (data.filter.search)
883
- docs = searchData(docs, data.filter)
884
- }
885
863
 
886
- resolve(docs)
864
+ let count = objectStore.count();
865
+ count.onsuccess = function() {
866
+ data.filter.count = count.result
867
+ }
868
+
869
+ if (data.filter && data.filter.sort && data.filter.sort[0] && data.filter.sort[0].name) {
870
+ let sortType = data.filter.sort[0].type
871
+ if (sortType == -1)
872
+ sortType = 'prev'
873
+ else
874
+ sortType = 'next'
875
+
876
+ let indexNames = Array.from(objectStore.indexNames)
877
+ let indexName = data.filter.sort[0].name
878
+ let indexExist = indexNames.includes(indexName)
879
+ if (!indexExist) {
880
+ db.close()
881
+ getDatabase({database, upgrade: true}).then((db2) => {
882
+ let transaction = db2.transaction;
883
+ let objectStore = transaction.objectStore(collection);
884
+ objectStore.createIndex(indexName, indexName, {unique: false})
885
+ const indexStore = objectStore.index(indexName);
886
+
887
+ readDocs(data, database, collection, indexStore, sortType).then((results) => {
888
+ db2.close()
889
+ resolve(results)
890
+ })
891
+ })
892
+ } else {
893
+ const indexStore = objectStore.index(indexName);
894
+ readDocs(data, database, collection, indexStore, sortType).then((results) => {
895
+ db.close()
896
+ resolve(results)
897
+ })
887
898
  }
888
- };
889
-
890
- request.onerror = function() {
891
- errorHandler(data, request.error, database, collection)
892
- resolve([])
893
- };
899
+ } else {
900
+ readDocs(data, database, collection, objectStore).then((results) => {
901
+ db.close()
902
+ resolve(results)
903
+ })
904
+ }
894
905
  } catch (err) {
895
906
  errorHandler(data, err, database)
907
+ db.close()
896
908
  resolve([])
897
909
  return
898
910
  }
@@ -906,6 +918,48 @@ const readDocuments = (data, database, collection) => {
906
918
  })
907
919
  }
908
920
 
921
+ async function readDocs(data, database, collection, objectStore, sortType) {
922
+ return new Promise((resolve, reject) => {
923
+
924
+ let results = [];
925
+ let index = data.filter.startIndex || 0
926
+ let limit = data.filter.limit
927
+ if (limit)
928
+ limit = index + limit;
929
+
930
+ const request = objectStore.openCursor(null, sortType);
931
+ request.onsuccess = function() {
932
+ let cursor = request.result;
933
+ if (cursor) {
934
+ let isFilter = true;
935
+ 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);
940
+ if (isFilter !== false)
941
+ results.push(value)
942
+ if (limit && limit == results.length) {
943
+ results = results.slice(index, limit)
944
+ resolve(results)
945
+ } else
946
+ cursor.continue();
947
+ } else {
948
+ if (index)
949
+ results = results.slice(index)
950
+ resolve(results)
951
+ }
952
+ }
953
+
954
+ request.onerror = function() {
955
+ errorHandler(data, request.error, database, collection)
956
+ db.close()
957
+ resolve([])
958
+ };
959
+
960
+ });
961
+ }
962
+
909
963
  async function generateDB(data) {
910
964
  const organization_id = data.organization_id;
911
965
  const apiKey = data.apiKey;