@cocreate/indexeddb 1.3.12 → 1.4.1
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 +14 -0
- package/package.json +4 -4
- package/src/index.js +84 -34
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
1
|
+
## [1.4.1](https://github.com/CoCreate-app/CoCreate-indexeddb/compare/v1.4.0...v1.4.1) (2022-11-27)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* bump dependencies ([c09276c](https://github.com/CoCreate-app/CoCreate-indexeddb/commit/c09276caeac1cc4e84d7fa21cca14bac49afb949))
|
|
7
|
+
|
|
8
|
+
# [1.4.0](https://github.com/CoCreate-app/CoCreate-indexeddb/compare/v1.3.12...v1.4.0) (2022-11-27)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Features
|
|
12
|
+
|
|
13
|
+
* if sort is applied creates index on object store before opening cursor ([ce667c5](https://github.com/CoCreate-app/CoCreate-indexeddb/commit/ce667c59582cd34c4ed2d89e597cb240452c4497))
|
|
14
|
+
|
|
1
15
|
## [1.3.12](https://github.com/CoCreate-app/CoCreate-indexeddb/compare/v1.3.11...v1.3.12) (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
|
+
"version": "1.4.1",
|
|
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.
|
|
65
|
-
"@cocreate/hosting": "^1.6.
|
|
66
|
-
"@cocreate/utils": "^1.12.
|
|
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
|
|
427
|
+
for (let index of indexNames) {
|
|
428
428
|
indexArray.push({name: index, db: 'indexeddb', database, collection})
|
|
429
429
|
}
|
|
430
430
|
collectionsLength -= 1
|
|
@@ -860,43 +860,51 @@ const readDocuments = (data, database, collection) => {
|
|
|
860
860
|
try {
|
|
861
861
|
let transaction = db.transaction([collection], "readonly");
|
|
862
862
|
let objectStore = transaction.objectStore(collection);
|
|
863
|
-
let results = [];
|
|
864
|
-
// let index = objectStore.index(filter.name);
|
|
865
|
-
// let request = index.openCursor(IDBKeyRange.upperBound(filter.value));
|
|
866
|
-
let request = objectStore.openCursor();
|
|
867
|
-
// transaction.oncomplete = resolve(results);
|
|
868
|
-
|
|
869
|
-
request.onsuccess = function() {
|
|
870
|
-
let isFilter;
|
|
871
|
-
// if (data['organization_id']) {
|
|
872
|
-
// data['filter']['query']['organization_id'] = data['organization_id'];
|
|
873
|
-
// }
|
|
874
|
-
|
|
875
|
-
let cursor = request.result;
|
|
876
|
-
if (cursor) {
|
|
877
|
-
let value = cursor.value;
|
|
878
|
-
if (data.filter && data.filter.query)
|
|
879
|
-
isFilter = queryData(value, data.filter.query);
|
|
880
|
-
if (isFilter !== false)
|
|
881
|
-
results.push(value)
|
|
882
|
-
cursor.continue();
|
|
883
|
-
} else {
|
|
884
|
-
let docs = results
|
|
885
|
-
if (data.filter) {
|
|
886
|
-
if (data.filter.search)
|
|
887
|
-
docs = searchData(docs, data.filter)
|
|
888
|
-
}
|
|
889
863
|
|
|
890
|
-
|
|
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
|
+
})
|
|
891
898
|
}
|
|
892
|
-
}
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
}
|
|
899
|
+
} else {
|
|
900
|
+
readDocs(data, database, collection, objectStore).then((results) => {
|
|
901
|
+
db.close()
|
|
902
|
+
resolve(results)
|
|
903
|
+
})
|
|
904
|
+
}
|
|
898
905
|
} catch (err) {
|
|
899
906
|
errorHandler(data, err, database)
|
|
907
|
+
db.close()
|
|
900
908
|
resolve([])
|
|
901
909
|
return
|
|
902
910
|
}
|
|
@@ -910,6 +918,48 @@ const readDocuments = (data, database, collection) => {
|
|
|
910
918
|
})
|
|
911
919
|
}
|
|
912
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
|
+
|
|
913
963
|
async function generateDB(data) {
|
|
914
964
|
const organization_id = data.organization_id;
|
|
915
965
|
const apiKey = data.apiKey;
|