idb-ts 3.13.0 → 3.14.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/README.md +1 -1
- package/lib/index.cjs +1158 -0
- package/lib/index.esm.js +39 -46
- package/lib/index.js +5 -21
- package/package.json +3 -3
- package/lib/jest.setup.d.ts +0 -1
- package/lib/jest.setup.js +0 -6
package/lib/index.esm.js
CHANGED
|
@@ -1,13 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
exports.CompositeKeyPath = CompositeKeyPath;
|
|
5
|
-
exports.DataClass = DataClass;
|
|
6
|
-
exports.Index = Index;
|
|
7
|
-
exports.Validate = Validate;
|
|
8
|
-
exports.RetentionPolicy = RetentionPolicy;
|
|
9
|
-
const tslib_1 = require("tslib");
|
|
10
|
-
require("reflect-metadata");
|
|
1
|
+
import { __awaiter } from 'tslib';
|
|
2
|
+
import 'reflect-metadata';
|
|
3
|
+
|
|
11
4
|
class FieldQueryBuilder {
|
|
12
5
|
constructor(parent, field) {
|
|
13
6
|
this.parent = parent;
|
|
@@ -225,7 +218,7 @@ class QueryBuilder {
|
|
|
225
218
|
return this;
|
|
226
219
|
}
|
|
227
220
|
loadCandidates() {
|
|
228
|
-
return
|
|
221
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
229
222
|
const store = this.transaction
|
|
230
223
|
? this.transaction.objectStore(this.storeName)
|
|
231
224
|
: this.db
|
|
@@ -325,7 +318,7 @@ class QueryBuilder {
|
|
|
325
318
|
return result;
|
|
326
319
|
}
|
|
327
320
|
collectMatches() {
|
|
328
|
-
return
|
|
321
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
329
322
|
const candidates = yield this.loadCandidates();
|
|
330
323
|
return candidates.filter((item) => this.evaluateClauses(item, this.clauses));
|
|
331
324
|
});
|
|
@@ -355,7 +348,7 @@ class QueryBuilder {
|
|
|
355
348
|
return nextResults;
|
|
356
349
|
}
|
|
357
350
|
aggregateValues(field, reducer) {
|
|
358
|
-
return
|
|
351
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
359
352
|
const results = yield this.collectMatches();
|
|
360
353
|
const values = field
|
|
361
354
|
? results.map((item) => item[field])
|
|
@@ -364,7 +357,7 @@ class QueryBuilder {
|
|
|
364
357
|
});
|
|
365
358
|
}
|
|
366
359
|
aggregateGroupedCount(field) {
|
|
367
|
-
return
|
|
360
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
368
361
|
var _a;
|
|
369
362
|
const results = yield this.collectMatches();
|
|
370
363
|
const groups = new Map();
|
|
@@ -384,13 +377,13 @@ class QueryBuilder {
|
|
|
384
377
|
});
|
|
385
378
|
}
|
|
386
379
|
execute() {
|
|
387
|
-
return
|
|
380
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
388
381
|
const results = yield this.collectMatches();
|
|
389
382
|
return this.applyPagination(this.sortResults(results));
|
|
390
383
|
});
|
|
391
384
|
}
|
|
392
385
|
count() {
|
|
393
|
-
return
|
|
386
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
394
387
|
if (this.groupField) {
|
|
395
388
|
return this.aggregateGroupedCount(this.groupField);
|
|
396
389
|
}
|
|
@@ -399,13 +392,13 @@ class QueryBuilder {
|
|
|
399
392
|
});
|
|
400
393
|
}
|
|
401
394
|
sum(field) {
|
|
402
|
-
return
|
|
395
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
403
396
|
const total = yield this.aggregateValues(field, (values) => values.reduce((accumulator, value) => accumulator + (Number(value) || 0), 0));
|
|
404
397
|
return total;
|
|
405
398
|
});
|
|
406
399
|
}
|
|
407
400
|
avg(field) {
|
|
408
|
-
return
|
|
401
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
409
402
|
const values = yield this.aggregateValues(field, (items) => items);
|
|
410
403
|
if (!values.length) {
|
|
411
404
|
return 0;
|
|
@@ -415,7 +408,7 @@ class QueryBuilder {
|
|
|
415
408
|
});
|
|
416
409
|
}
|
|
417
410
|
min(field) {
|
|
418
|
-
return
|
|
411
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
419
412
|
const values = yield this.aggregateValues(field, (items) => items);
|
|
420
413
|
if (!values.length) {
|
|
421
414
|
return null;
|
|
@@ -424,7 +417,7 @@ class QueryBuilder {
|
|
|
424
417
|
});
|
|
425
418
|
}
|
|
426
419
|
max(field) {
|
|
427
|
-
return
|
|
420
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
428
421
|
const values = yield this.aggregateValues(field, (items) => items);
|
|
429
422
|
if (!values.length) {
|
|
430
423
|
return null;
|
|
@@ -450,7 +443,6 @@ class KeyGenerators {
|
|
|
450
443
|
return Math.random().toString(36).substring(2, 15);
|
|
451
444
|
}
|
|
452
445
|
}
|
|
453
|
-
exports.KeyGenerators = KeyGenerators;
|
|
454
446
|
function KeyPath(options) {
|
|
455
447
|
return (target, propertyKey) => {
|
|
456
448
|
const constructor = target.constructor;
|
|
@@ -555,7 +547,7 @@ class Database {
|
|
|
555
547
|
return Math.max(...versions);
|
|
556
548
|
}
|
|
557
549
|
static build(dbName, classes) {
|
|
558
|
-
return
|
|
550
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
559
551
|
const instance = new Database(dbName, classes);
|
|
560
552
|
yield instance.initDB();
|
|
561
553
|
instance.generateEntityRepositories();
|
|
@@ -563,7 +555,7 @@ class Database {
|
|
|
563
555
|
});
|
|
564
556
|
}
|
|
565
557
|
initDB() {
|
|
566
|
-
return
|
|
558
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
567
559
|
return new Promise((resolve, reject) => {
|
|
568
560
|
const request = indexedDB.open(this.dbName, this.dbVersion);
|
|
569
561
|
request.onupgradeneeded = (event) => {
|
|
@@ -679,7 +671,7 @@ class Database {
|
|
|
679
671
|
}, cleanupIntervalMs);
|
|
680
672
|
}
|
|
681
673
|
runRetentionCleanup() {
|
|
682
|
-
return
|
|
674
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
683
675
|
if (!this.db ||
|
|
684
676
|
this.retentionCleanupRunning ||
|
|
685
677
|
!this.retentionPolicies.length) {
|
|
@@ -813,7 +805,7 @@ class Database {
|
|
|
813
805
|
request.onerror = () => reject(request.error);
|
|
814
806
|
});
|
|
815
807
|
};
|
|
816
|
-
const updateStoredItem = (store, item) =>
|
|
808
|
+
const updateStoredItem = (store, item) => __awaiter(this, void 0, void 0, function* () {
|
|
817
809
|
const key = extractKey(item);
|
|
818
810
|
let existingItem;
|
|
819
811
|
if (key !== undefined && key !== null) {
|
|
@@ -861,7 +853,7 @@ class Database {
|
|
|
861
853
|
const storeName = cls.name.toLowerCase();
|
|
862
854
|
return new QueryBuilder(self.db, storeName, transaction);
|
|
863
855
|
},
|
|
864
|
-
create: (item) =>
|
|
856
|
+
create: (item) => __awaiter(this, void 0, void 0, function* () {
|
|
865
857
|
var _a;
|
|
866
858
|
const keyPathMetadata = Reflect.getMetadata('keypath', cls);
|
|
867
859
|
if (((_a = keyPathMetadata === null || keyPathMetadata === void 0 ? void 0 : keyPathMetadata.options) === null || _a === void 0 ? void 0 : _a.generator) &&
|
|
@@ -884,13 +876,13 @@ class Database {
|
|
|
884
876
|
});
|
|
885
877
|
}, transaction);
|
|
886
878
|
}),
|
|
887
|
-
createMany: (items) =>
|
|
879
|
+
createMany: (items) => __awaiter(this, void 0, void 0, function* () {
|
|
888
880
|
const repository = this.createEntityRepository(cls, transaction);
|
|
889
881
|
for (const item of items) {
|
|
890
882
|
yield repository.create(item);
|
|
891
883
|
}
|
|
892
884
|
}),
|
|
893
|
-
read: (key) =>
|
|
885
|
+
read: (key) => __awaiter(this, void 0, void 0, function* () {
|
|
894
886
|
return this.performOperation(cls.name, 'readonly', (store) => {
|
|
895
887
|
const request = store.get(key);
|
|
896
888
|
return new Promise((resolve, reject) => {
|
|
@@ -902,7 +894,7 @@ class Database {
|
|
|
902
894
|
});
|
|
903
895
|
}, transaction);
|
|
904
896
|
}),
|
|
905
|
-
update: (item) =>
|
|
897
|
+
update: (item) => __awaiter(this, void 0, void 0, function* () {
|
|
906
898
|
validateItem(item);
|
|
907
899
|
return this.performOperation(cls.name, 'readwrite', (store) => {
|
|
908
900
|
return updateStoredItem(store, item).then(() => {
|
|
@@ -910,26 +902,26 @@ class Database {
|
|
|
910
902
|
});
|
|
911
903
|
}, transaction);
|
|
912
904
|
}),
|
|
913
|
-
updateMany: (items) =>
|
|
905
|
+
updateMany: (items) => __awaiter(this, void 0, void 0, function* () {
|
|
914
906
|
const repository = this.createEntityRepository(cls, transaction);
|
|
915
907
|
for (const item of items) {
|
|
916
908
|
yield repository.update(item);
|
|
917
909
|
}
|
|
918
910
|
}),
|
|
919
|
-
delete: (key) =>
|
|
911
|
+
delete: (key) => __awaiter(this, void 0, void 0, function* () {
|
|
920
912
|
return this.performOperation(cls.name, 'readwrite', (store) => {
|
|
921
913
|
return deleteStoredItem(store, key).then(() => {
|
|
922
914
|
console.debug(`Item deleted from ${cls.name}:`, key);
|
|
923
915
|
});
|
|
924
916
|
}, transaction);
|
|
925
917
|
}),
|
|
926
|
-
deleteMany: (keys) =>
|
|
918
|
+
deleteMany: (keys) => __awaiter(this, void 0, void 0, function* () {
|
|
927
919
|
const repository = this.createEntityRepository(cls, transaction);
|
|
928
920
|
for (const key of keys) {
|
|
929
921
|
yield repository.delete(key);
|
|
930
922
|
}
|
|
931
923
|
}),
|
|
932
|
-
deleteWhere: (predicate) =>
|
|
924
|
+
deleteWhere: (predicate) => __awaiter(this, void 0, void 0, function* () {
|
|
933
925
|
var _a;
|
|
934
926
|
const repository = this.createEntityRepository(cls, transaction);
|
|
935
927
|
const query = repository.query();
|
|
@@ -940,7 +932,7 @@ class Database {
|
|
|
940
932
|
.filter((key) => key !== undefined && key !== null);
|
|
941
933
|
yield repository.deleteMany(keys);
|
|
942
934
|
}),
|
|
943
|
-
list: () =>
|
|
935
|
+
list: () => __awaiter(this, void 0, void 0, function* () {
|
|
944
936
|
return this.performOperation(cls.name, 'readonly', (store) => {
|
|
945
937
|
const request = store.getAll();
|
|
946
938
|
return new Promise((resolve, reject) => {
|
|
@@ -952,7 +944,7 @@ class Database {
|
|
|
952
944
|
});
|
|
953
945
|
}, transaction);
|
|
954
946
|
}),
|
|
955
|
-
listPaginated: (page, pageSize) =>
|
|
947
|
+
listPaginated: (page, pageSize) => __awaiter(this, void 0, void 0, function* () {
|
|
956
948
|
return this.performOperation(cls.name, 'readonly', (store) => {
|
|
957
949
|
const request = store.getAll();
|
|
958
950
|
return new Promise((resolve, reject) => {
|
|
@@ -966,7 +958,7 @@ class Database {
|
|
|
966
958
|
});
|
|
967
959
|
}, transaction);
|
|
968
960
|
}),
|
|
969
|
-
findByIndex: (indexName, value) =>
|
|
961
|
+
findByIndex: (indexName, value) => __awaiter(this, void 0, void 0, function* () {
|
|
970
962
|
return this.performOperation(cls.name, 'readonly', (store) => {
|
|
971
963
|
if (!store.indexNames.contains(indexName)) {
|
|
972
964
|
throw new Error(`Index '${indexName}' does not exist on ${cls.name}`);
|
|
@@ -982,7 +974,7 @@ class Database {
|
|
|
982
974
|
});
|
|
983
975
|
}, transaction);
|
|
984
976
|
}),
|
|
985
|
-
findOneByIndex: (indexName, value) =>
|
|
977
|
+
findOneByIndex: (indexName, value) => __awaiter(this, void 0, void 0, function* () {
|
|
986
978
|
return this.performOperation(cls.name, 'readonly', (store) => {
|
|
987
979
|
if (!store.indexNames.contains(indexName)) {
|
|
988
980
|
throw new Error(`Index '${indexName}' does not exist on ${cls.name}`);
|
|
@@ -998,7 +990,7 @@ class Database {
|
|
|
998
990
|
});
|
|
999
991
|
}, transaction);
|
|
1000
992
|
}),
|
|
1001
|
-
count: () =>
|
|
993
|
+
count: () => __awaiter(this, void 0, void 0, function* () {
|
|
1002
994
|
return this.performOperation(cls.name, 'readonly', (store) => {
|
|
1003
995
|
const request = store.count();
|
|
1004
996
|
return new Promise((resolve, reject) => {
|
|
@@ -1010,7 +1002,7 @@ class Database {
|
|
|
1010
1002
|
});
|
|
1011
1003
|
}, transaction);
|
|
1012
1004
|
}),
|
|
1013
|
-
exists: (key) =>
|
|
1005
|
+
exists: (key) => __awaiter(this, void 0, void 0, function* () {
|
|
1014
1006
|
return this.performOperation(cls.name, 'readonly', (store) => {
|
|
1015
1007
|
const request = store.count(key);
|
|
1016
1008
|
return new Promise((resolve, reject) => {
|
|
@@ -1023,7 +1015,7 @@ class Database {
|
|
|
1023
1015
|
});
|
|
1024
1016
|
}, transaction);
|
|
1025
1017
|
}),
|
|
1026
|
-
clear: () =>
|
|
1018
|
+
clear: () => __awaiter(this, void 0, void 0, function* () {
|
|
1027
1019
|
return this.performOperation(cls.name, 'readwrite', (store) => {
|
|
1028
1020
|
const request = store.clear();
|
|
1029
1021
|
return new Promise((resolve, reject) => {
|
|
@@ -1038,7 +1030,7 @@ class Database {
|
|
|
1038
1030
|
};
|
|
1039
1031
|
}
|
|
1040
1032
|
performOperation(className, mode, operation, transaction) {
|
|
1041
|
-
return
|
|
1033
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1042
1034
|
if (!this.db && !transaction) {
|
|
1043
1035
|
throw new Error('Database not initialized.');
|
|
1044
1036
|
}
|
|
@@ -1088,13 +1080,13 @@ class Database {
|
|
|
1088
1080
|
handle[entityName] = this.createEntityRepository(entityClass, nativeTransaction);
|
|
1089
1081
|
}
|
|
1090
1082
|
});
|
|
1091
|
-
handle.commit = () =>
|
|
1083
|
+
handle.commit = () => __awaiter(this, void 0, void 0, function* () {
|
|
1092
1084
|
if (typeof nativeTransaction.commit === 'function') {
|
|
1093
1085
|
nativeTransaction.commit();
|
|
1094
1086
|
}
|
|
1095
1087
|
yield completion;
|
|
1096
1088
|
});
|
|
1097
|
-
handle.rollback = () =>
|
|
1089
|
+
handle.rollback = () => __awaiter(this, void 0, void 0, function* () {
|
|
1098
1090
|
rollbackRequested = true;
|
|
1099
1091
|
try {
|
|
1100
1092
|
nativeTransaction.abort();
|
|
@@ -1106,12 +1098,12 @@ class Database {
|
|
|
1106
1098
|
return handle;
|
|
1107
1099
|
}
|
|
1108
1100
|
beginTransaction(entityNames_1) {
|
|
1109
|
-
return
|
|
1101
|
+
return __awaiter(this, arguments, void 0, function* (entityNames, mode = 'readwrite') {
|
|
1110
1102
|
return this.createTransactionHandle(entityNames, mode);
|
|
1111
1103
|
});
|
|
1112
1104
|
}
|
|
1113
1105
|
transaction(callback) {
|
|
1114
|
-
return
|
|
1106
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1115
1107
|
const tx = this.createTransactionHandle(this.classes.map((cls) => cls.name), 'readwrite');
|
|
1116
1108
|
try {
|
|
1117
1109
|
const result = yield callback(tx);
|
|
@@ -1153,4 +1145,5 @@ class Database {
|
|
|
1153
1145
|
return cls ? Reflect.getMetadata('version', cls) || 1 : undefined;
|
|
1154
1146
|
}
|
|
1155
1147
|
}
|
|
1156
|
-
|
|
1148
|
+
|
|
1149
|
+
export { CompositeKeyPath, DataClass, Database, Index, KeyGenerators, KeyPath, RetentionPolicy, Validate };
|
package/lib/index.js
CHANGED
|
@@ -1,22 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.KeyGenerators = exports.Database = void 0;
|
|
13
|
-
exports.KeyPath = KeyPath;
|
|
14
|
-
exports.CompositeKeyPath = CompositeKeyPath;
|
|
15
|
-
exports.DataClass = DataClass;
|
|
16
|
-
exports.Index = Index;
|
|
17
|
-
exports.Validate = Validate;
|
|
18
|
-
exports.RetentionPolicy = RetentionPolicy;
|
|
19
|
-
require("reflect-metadata");
|
|
1
|
+
import { __awaiter } from 'tslib';
|
|
2
|
+
import 'reflect-metadata';
|
|
3
|
+
|
|
20
4
|
class FieldQueryBuilder {
|
|
21
5
|
constructor(parent, field) {
|
|
22
6
|
this.parent = parent;
|
|
@@ -459,7 +443,6 @@ class KeyGenerators {
|
|
|
459
443
|
return Math.random().toString(36).substring(2, 15);
|
|
460
444
|
}
|
|
461
445
|
}
|
|
462
|
-
exports.KeyGenerators = KeyGenerators;
|
|
463
446
|
function KeyPath(options) {
|
|
464
447
|
return (target, propertyKey) => {
|
|
465
448
|
const constructor = target.constructor;
|
|
@@ -1162,4 +1145,5 @@ class Database {
|
|
|
1162
1145
|
return cls ? Reflect.getMetadata('version', cls) || 1 : undefined;
|
|
1163
1146
|
}
|
|
1164
1147
|
}
|
|
1165
|
-
|
|
1148
|
+
|
|
1149
|
+
export { CompositeKeyPath, DataClass, Database, Index, KeyGenerators, KeyPath, RetentionPolicy, Validate };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "idb-ts",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.14.0",
|
|
4
4
|
"description": "Easy CRUD for indexed-db, written in TypeScript",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"module": "lib/index.esm.js",
|
|
@@ -15,8 +15,8 @@
|
|
|
15
15
|
},
|
|
16
16
|
"scripts": {
|
|
17
17
|
"prepare": "pnpm run build",
|
|
18
|
-
"build": "pnpm run build:tsc && pnpm run build:rollup",
|
|
19
|
-
"build:tsc": "tsc",
|
|
18
|
+
"build": "rm -rf lib && pnpm run build:tsc && pnpm run build:rollup",
|
|
19
|
+
"build:tsc": "tsc -p tsconfig.build.json",
|
|
20
20
|
"build:rollup": "rollup -c",
|
|
21
21
|
"lint": "eslint .",
|
|
22
22
|
"lint:fix": "prettier --write . && eslint . --fix",
|
package/lib/jest.setup.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import 'fake-indexeddb/auto';
|
package/lib/jest.setup.js
DELETED