idb-ts 3.13.0 → 3.15.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 +198 -74
- package/lib/index.cjs +1169 -0
- package/lib/index.d.ts +3 -0
- package/lib/index.esm.js +73 -69
- package/lib/index.js +39 -44
- package/package.json +8 -5
- package/lib/jest.setup.d.ts +0 -1
- package/lib/jest.setup.js +0 -6
package/lib/index.d.ts
CHANGED
|
@@ -173,6 +173,9 @@ declare class Database {
|
|
|
173
173
|
private retentionTimer;
|
|
174
174
|
private retentionCleanupRunning;
|
|
175
175
|
private retentionPolicies;
|
|
176
|
+
private printEnabled;
|
|
177
|
+
private printDebug;
|
|
178
|
+
private printError;
|
|
176
179
|
private constructor();
|
|
177
180
|
private calculateDatabaseVersion;
|
|
178
181
|
static build<T extends Record<string, EntityRepository<any>>>(dbName: string, classes: Function[]): Promise<DatabaseWithRepositories<T>>;
|
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;
|
|
@@ -525,12 +517,23 @@ function DataClass(options = {}) {
|
|
|
525
517
|
};
|
|
526
518
|
}
|
|
527
519
|
class Database {
|
|
528
|
-
constructor(dbName, classes) {
|
|
520
|
+
constructor(dbName, classes, printEnabled = false) {
|
|
529
521
|
this.db = null;
|
|
530
522
|
this.entityRepositories = new Map();
|
|
531
523
|
this.retentionTimer = null;
|
|
532
524
|
this.retentionCleanupRunning = false;
|
|
525
|
+
this.printDebug = (...data) => {
|
|
526
|
+
if (!this.printEnabled)
|
|
527
|
+
return;
|
|
528
|
+
console.debug('[idb-ts]:DEBUG:', ...data);
|
|
529
|
+
};
|
|
530
|
+
this.printError = (...error) => {
|
|
531
|
+
if (!this.printEnabled)
|
|
532
|
+
return;
|
|
533
|
+
console.error('[idb-ts]:ERROR:', ...error);
|
|
534
|
+
};
|
|
533
535
|
this.dbName = dbName;
|
|
536
|
+
this.printEnabled = printEnabled;
|
|
534
537
|
if (!classes.every((cls) => Reflect.getMetadata('dataclass', cls))) {
|
|
535
538
|
throw new Error('All classes should be decorated with @DataClass.');
|
|
536
539
|
}
|
|
@@ -555,7 +558,7 @@ class Database {
|
|
|
555
558
|
return Math.max(...versions);
|
|
556
559
|
}
|
|
557
560
|
static build(dbName, classes) {
|
|
558
|
-
return
|
|
561
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
559
562
|
const instance = new Database(dbName, classes);
|
|
560
563
|
yield instance.initDB();
|
|
561
564
|
instance.generateEntityRepositories();
|
|
@@ -563,14 +566,14 @@ class Database {
|
|
|
563
566
|
});
|
|
564
567
|
}
|
|
565
568
|
initDB() {
|
|
566
|
-
return
|
|
569
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
567
570
|
return new Promise((resolve, reject) => {
|
|
568
571
|
const request = indexedDB.open(this.dbName, this.dbVersion);
|
|
569
572
|
request.onupgradeneeded = (event) => {
|
|
570
573
|
const db = request.result;
|
|
571
574
|
const oldVersion = event.oldVersion;
|
|
572
575
|
const newVersion = event.newVersion || this.dbVersion;
|
|
573
|
-
|
|
576
|
+
this.printDebug(`Database upgrade from version ${oldVersion} to ${newVersion}`);
|
|
574
577
|
this.classes.forEach((cls) => {
|
|
575
578
|
var _a;
|
|
576
579
|
const keyPathMetadata = Reflect.getMetadata('keypath', cls);
|
|
@@ -579,7 +582,7 @@ class Database {
|
|
|
579
582
|
const storeName = cls.name.toLowerCase();
|
|
580
583
|
if (classVersion > oldVersion) {
|
|
581
584
|
if (!db.objectStoreNames.contains(storeName)) {
|
|
582
|
-
|
|
585
|
+
this.printDebug(`Creating object store: ${storeName} (version ${classVersion})`);
|
|
583
586
|
const storeOptions = {};
|
|
584
587
|
if (keyPathMetadata) {
|
|
585
588
|
storeOptions.keyPath = keyPathMetadata.fields;
|
|
@@ -602,7 +605,7 @@ class Database {
|
|
|
602
605
|
});
|
|
603
606
|
}
|
|
604
607
|
else {
|
|
605
|
-
|
|
608
|
+
this.printDebug(`Updating object store: ${storeName} (version ${classVersion})`);
|
|
606
609
|
const transaction = request.transaction;
|
|
607
610
|
if (transaction) {
|
|
608
611
|
const store = transaction.objectStore(storeName);
|
|
@@ -615,7 +618,7 @@ class Database {
|
|
|
615
618
|
? { unique: false }
|
|
616
619
|
: ((_a = indexField.options) !== null && _a !== void 0 ? _a : { unique: false });
|
|
617
620
|
if (!store.indexNames.contains(indexName)) {
|
|
618
|
-
|
|
621
|
+
this.printDebug(`Adding index: ${indexName} to ${storeName}`);
|
|
619
622
|
store.createIndex(indexName, indexName, indexOptions);
|
|
620
623
|
}
|
|
621
624
|
});
|
|
@@ -626,12 +629,12 @@ class Database {
|
|
|
626
629
|
};
|
|
627
630
|
request.onsuccess = () => {
|
|
628
631
|
this.db = request.result;
|
|
629
|
-
|
|
632
|
+
this.printDebug(`Database initialized (version ${this.dbVersion}) with object stores for: ${this.classes.map((cls) => `${cls.name}(v${Reflect.getMetadata('version', cls) || 1})`).join(', ')}`);
|
|
630
633
|
this.startRetentionCleanup();
|
|
631
634
|
resolve();
|
|
632
635
|
};
|
|
633
636
|
request.onerror = () => {
|
|
634
|
-
|
|
637
|
+
this.printError('Error initializing database:', request.error);
|
|
635
638
|
reject(request.error);
|
|
636
639
|
};
|
|
637
640
|
});
|
|
@@ -672,14 +675,14 @@ class Database {
|
|
|
672
675
|
if (!cleanupIntervalMs || !this.db || this.retentionTimer) {
|
|
673
676
|
return;
|
|
674
677
|
}
|
|
675
|
-
|
|
678
|
+
this.printDebug(`Retention cleanup enabled for ${this.retentionPolicies.length} entities every ${cleanupIntervalMs}ms`);
|
|
676
679
|
void this.runRetentionCleanup();
|
|
677
680
|
this.retentionTimer = setInterval(() => {
|
|
678
681
|
void this.runRetentionCleanup();
|
|
679
682
|
}, cleanupIntervalMs);
|
|
680
683
|
}
|
|
681
684
|
runRetentionCleanup() {
|
|
682
|
-
return
|
|
685
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
683
686
|
if (!this.db ||
|
|
684
687
|
this.retentionCleanupRunning ||
|
|
685
688
|
!this.retentionPolicies.length) {
|
|
@@ -687,11 +690,11 @@ class Database {
|
|
|
687
690
|
}
|
|
688
691
|
this.retentionCleanupRunning = true;
|
|
689
692
|
try {
|
|
690
|
-
|
|
693
|
+
this.printDebug('Retention cleanup tick started');
|
|
691
694
|
for (const { storeName, className, policy } of this.retentionPolicies) {
|
|
692
695
|
yield this.cleanupExpiredRecords(storeName, className, policy);
|
|
693
696
|
}
|
|
694
|
-
|
|
697
|
+
this.printDebug('Retention cleanup tick finished');
|
|
695
698
|
}
|
|
696
699
|
finally {
|
|
697
700
|
this.retentionCleanupRunning = false;
|
|
@@ -715,11 +718,11 @@ class Database {
|
|
|
715
718
|
}
|
|
716
719
|
const value = cursor.value;
|
|
717
720
|
const timestamp = value === null || value === void 0 ? void 0 : value[policy.field];
|
|
718
|
-
|
|
721
|
+
this.printDebug(`Retention cleanup inspecting ${className}.${policy.field}:`, timestamp, 'cutoff:', cutoff);
|
|
719
722
|
if (typeof timestamp === 'number' && timestamp <= cutoff) {
|
|
720
723
|
const deleteRequest = cursor.delete();
|
|
721
724
|
deleteRequest.onsuccess = () => {
|
|
722
|
-
|
|
725
|
+
this.printDebug(`Retention cleanup removed expired record from ${className}`);
|
|
723
726
|
cursor.continue();
|
|
724
727
|
};
|
|
725
728
|
deleteRequest.onerror = () => {
|
|
@@ -813,7 +816,7 @@ class Database {
|
|
|
813
816
|
request.onerror = () => reject(request.error);
|
|
814
817
|
});
|
|
815
818
|
};
|
|
816
|
-
const updateStoredItem = (store, item) =>
|
|
819
|
+
const updateStoredItem = (store, item) => __awaiter(this, void 0, void 0, function* () {
|
|
817
820
|
const key = extractKey(item);
|
|
818
821
|
let existingItem;
|
|
819
822
|
if (key !== undefined && key !== null) {
|
|
@@ -861,7 +864,7 @@ class Database {
|
|
|
861
864
|
const storeName = cls.name.toLowerCase();
|
|
862
865
|
return new QueryBuilder(self.db, storeName, transaction);
|
|
863
866
|
},
|
|
864
|
-
create: (item) =>
|
|
867
|
+
create: (item) => __awaiter(this, void 0, void 0, function* () {
|
|
865
868
|
var _a;
|
|
866
869
|
const keyPathMetadata = Reflect.getMetadata('keypath', cls);
|
|
867
870
|
if (((_a = keyPathMetadata === null || keyPathMetadata === void 0 ? void 0 : keyPathMetadata.options) === null || _a === void 0 ? void 0 : _a.generator) &&
|
|
@@ -880,56 +883,56 @@ class Database {
|
|
|
880
883
|
applyTimestampFields(item);
|
|
881
884
|
return this.performOperation(cls.name, 'readwrite', (store) => {
|
|
882
885
|
return createStoredItem(store, item).then(() => {
|
|
883
|
-
|
|
886
|
+
this.printDebug(`Item added to ${cls.name}:`, item);
|
|
884
887
|
});
|
|
885
888
|
}, transaction);
|
|
886
889
|
}),
|
|
887
|
-
createMany: (items) =>
|
|
890
|
+
createMany: (items) => __awaiter(this, void 0, void 0, function* () {
|
|
888
891
|
const repository = this.createEntityRepository(cls, transaction);
|
|
889
892
|
for (const item of items) {
|
|
890
893
|
yield repository.create(item);
|
|
891
894
|
}
|
|
892
895
|
}),
|
|
893
|
-
read: (key) =>
|
|
896
|
+
read: (key) => __awaiter(this, void 0, void 0, function* () {
|
|
894
897
|
return this.performOperation(cls.name, 'readonly', (store) => {
|
|
895
898
|
const request = store.get(key);
|
|
896
899
|
return new Promise((resolve, reject) => {
|
|
897
900
|
request.onsuccess = () => {
|
|
898
|
-
|
|
901
|
+
this.printDebug(`Item read from ${cls.name}:`, request.result);
|
|
899
902
|
resolve(request.result);
|
|
900
903
|
};
|
|
901
904
|
request.onerror = () => reject(request.error);
|
|
902
905
|
});
|
|
903
906
|
}, transaction);
|
|
904
907
|
}),
|
|
905
|
-
update: (item) =>
|
|
908
|
+
update: (item) => __awaiter(this, void 0, void 0, function* () {
|
|
906
909
|
validateItem(item);
|
|
907
910
|
return this.performOperation(cls.name, 'readwrite', (store) => {
|
|
908
911
|
return updateStoredItem(store, item).then(() => {
|
|
909
|
-
|
|
912
|
+
this.printDebug(`Item updated in ${cls.name}:`, item);
|
|
910
913
|
});
|
|
911
914
|
}, transaction);
|
|
912
915
|
}),
|
|
913
|
-
updateMany: (items) =>
|
|
916
|
+
updateMany: (items) => __awaiter(this, void 0, void 0, function* () {
|
|
914
917
|
const repository = this.createEntityRepository(cls, transaction);
|
|
915
918
|
for (const item of items) {
|
|
916
919
|
yield repository.update(item);
|
|
917
920
|
}
|
|
918
921
|
}),
|
|
919
|
-
delete: (key) =>
|
|
922
|
+
delete: (key) => __awaiter(this, void 0, void 0, function* () {
|
|
920
923
|
return this.performOperation(cls.name, 'readwrite', (store) => {
|
|
921
924
|
return deleteStoredItem(store, key).then(() => {
|
|
922
|
-
|
|
925
|
+
this.printDebug(`Item deleted from ${cls.name}:`, key);
|
|
923
926
|
});
|
|
924
927
|
}, transaction);
|
|
925
928
|
}),
|
|
926
|
-
deleteMany: (keys) =>
|
|
929
|
+
deleteMany: (keys) => __awaiter(this, void 0, void 0, function* () {
|
|
927
930
|
const repository = this.createEntityRepository(cls, transaction);
|
|
928
931
|
for (const key of keys) {
|
|
929
932
|
yield repository.delete(key);
|
|
930
933
|
}
|
|
931
934
|
}),
|
|
932
|
-
deleteWhere: (predicate) =>
|
|
935
|
+
deleteWhere: (predicate) => __awaiter(this, void 0, void 0, function* () {
|
|
933
936
|
var _a;
|
|
934
937
|
const repository = this.createEntityRepository(cls, transaction);
|
|
935
938
|
const query = repository.query();
|
|
@@ -940,33 +943,33 @@ class Database {
|
|
|
940
943
|
.filter((key) => key !== undefined && key !== null);
|
|
941
944
|
yield repository.deleteMany(keys);
|
|
942
945
|
}),
|
|
943
|
-
list: () =>
|
|
946
|
+
list: () => __awaiter(this, void 0, void 0, function* () {
|
|
944
947
|
return this.performOperation(cls.name, 'readonly', (store) => {
|
|
945
948
|
const request = store.getAll();
|
|
946
949
|
return new Promise((resolve, reject) => {
|
|
947
950
|
request.onsuccess = () => {
|
|
948
|
-
|
|
951
|
+
this.printDebug(`All items from ${cls.name}:`, request.result);
|
|
949
952
|
resolve(request.result);
|
|
950
953
|
};
|
|
951
954
|
request.onerror = () => reject(request.error);
|
|
952
955
|
});
|
|
953
956
|
}, transaction);
|
|
954
957
|
}),
|
|
955
|
-
listPaginated: (page, pageSize) =>
|
|
958
|
+
listPaginated: (page, pageSize) => __awaiter(this, void 0, void 0, function* () {
|
|
956
959
|
return this.performOperation(cls.name, 'readonly', (store) => {
|
|
957
960
|
const request = store.getAll();
|
|
958
961
|
return new Promise((resolve, reject) => {
|
|
959
962
|
request.onsuccess = () => {
|
|
960
963
|
const items = request.result;
|
|
961
964
|
const paginatedItems = items.slice((page - 1) * pageSize, page * pageSize);
|
|
962
|
-
|
|
965
|
+
this.printDebug(`Paginated items from ${cls.name}:`, paginatedItems);
|
|
963
966
|
resolve(paginatedItems);
|
|
964
967
|
};
|
|
965
968
|
request.onerror = () => reject(request.error);
|
|
966
969
|
});
|
|
967
970
|
}, transaction);
|
|
968
971
|
}),
|
|
969
|
-
findByIndex: (indexName, value) =>
|
|
972
|
+
findByIndex: (indexName, value) => __awaiter(this, void 0, void 0, function* () {
|
|
970
973
|
return this.performOperation(cls.name, 'readonly', (store) => {
|
|
971
974
|
if (!store.indexNames.contains(indexName)) {
|
|
972
975
|
throw new Error(`Index '${indexName}' does not exist on ${cls.name}`);
|
|
@@ -975,14 +978,14 @@ class Database {
|
|
|
975
978
|
const request = index.getAll(value);
|
|
976
979
|
return new Promise((resolve, reject) => {
|
|
977
980
|
request.onsuccess = () => {
|
|
978
|
-
|
|
981
|
+
this.printDebug(`Items found by index ${indexName} with value ${value}:`, request.result);
|
|
979
982
|
resolve(request.result);
|
|
980
983
|
};
|
|
981
984
|
request.onerror = () => reject(request.error);
|
|
982
985
|
});
|
|
983
986
|
}, transaction);
|
|
984
987
|
}),
|
|
985
|
-
findOneByIndex: (indexName, value) =>
|
|
988
|
+
findOneByIndex: (indexName, value) => __awaiter(this, void 0, void 0, function* () {
|
|
986
989
|
return this.performOperation(cls.name, 'readonly', (store) => {
|
|
987
990
|
if (!store.indexNames.contains(indexName)) {
|
|
988
991
|
throw new Error(`Index '${indexName}' does not exist on ${cls.name}`);
|
|
@@ -991,44 +994,44 @@ class Database {
|
|
|
991
994
|
const request = index.get(value);
|
|
992
995
|
return new Promise((resolve, reject) => {
|
|
993
996
|
request.onsuccess = () => {
|
|
994
|
-
|
|
997
|
+
this.printDebug(`Item found by index ${indexName} with value ${value}:`, request.result);
|
|
995
998
|
resolve(request.result);
|
|
996
999
|
};
|
|
997
1000
|
request.onerror = () => reject(request.error);
|
|
998
1001
|
});
|
|
999
1002
|
}, transaction);
|
|
1000
1003
|
}),
|
|
1001
|
-
count: () =>
|
|
1004
|
+
count: () => __awaiter(this, void 0, void 0, function* () {
|
|
1002
1005
|
return this.performOperation(cls.name, 'readonly', (store) => {
|
|
1003
1006
|
const request = store.count();
|
|
1004
1007
|
return new Promise((resolve, reject) => {
|
|
1005
1008
|
request.onsuccess = () => {
|
|
1006
|
-
|
|
1009
|
+
this.printDebug(`Count for ${cls.name}:`, request.result);
|
|
1007
1010
|
resolve(request.result);
|
|
1008
1011
|
};
|
|
1009
1012
|
request.onerror = () => reject(request.error);
|
|
1010
1013
|
});
|
|
1011
1014
|
}, transaction);
|
|
1012
1015
|
}),
|
|
1013
|
-
exists: (key) =>
|
|
1016
|
+
exists: (key) => __awaiter(this, void 0, void 0, function* () {
|
|
1014
1017
|
return this.performOperation(cls.name, 'readonly', (store) => {
|
|
1015
1018
|
const request = store.count(key);
|
|
1016
1019
|
return new Promise((resolve, reject) => {
|
|
1017
1020
|
request.onsuccess = () => {
|
|
1018
1021
|
const exists = request.result > 0;
|
|
1019
|
-
|
|
1022
|
+
this.printDebug(`Exists check for ${cls.name} with key ${key}:`, exists);
|
|
1020
1023
|
resolve(exists);
|
|
1021
1024
|
};
|
|
1022
1025
|
request.onerror = () => reject(request.error);
|
|
1023
1026
|
});
|
|
1024
1027
|
}, transaction);
|
|
1025
1028
|
}),
|
|
1026
|
-
clear: () =>
|
|
1029
|
+
clear: () => __awaiter(this, void 0, void 0, function* () {
|
|
1027
1030
|
return this.performOperation(cls.name, 'readwrite', (store) => {
|
|
1028
1031
|
const request = store.clear();
|
|
1029
1032
|
return new Promise((resolve, reject) => {
|
|
1030
1033
|
request.onsuccess = () => {
|
|
1031
|
-
|
|
1034
|
+
this.printDebug(`Cleared all items from ${cls.name}`);
|
|
1032
1035
|
resolve();
|
|
1033
1036
|
};
|
|
1034
1037
|
request.onerror = () => reject(request.error);
|
|
@@ -1038,7 +1041,7 @@ class Database {
|
|
|
1038
1041
|
};
|
|
1039
1042
|
}
|
|
1040
1043
|
performOperation(className, mode, operation, transaction) {
|
|
1041
|
-
return
|
|
1044
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1042
1045
|
if (!this.db && !transaction) {
|
|
1043
1046
|
throw new Error('Database not initialized.');
|
|
1044
1047
|
}
|
|
@@ -1088,13 +1091,13 @@ class Database {
|
|
|
1088
1091
|
handle[entityName] = this.createEntityRepository(entityClass, nativeTransaction);
|
|
1089
1092
|
}
|
|
1090
1093
|
});
|
|
1091
|
-
handle.commit = () =>
|
|
1094
|
+
handle.commit = () => __awaiter(this, void 0, void 0, function* () {
|
|
1092
1095
|
if (typeof nativeTransaction.commit === 'function') {
|
|
1093
1096
|
nativeTransaction.commit();
|
|
1094
1097
|
}
|
|
1095
1098
|
yield completion;
|
|
1096
1099
|
});
|
|
1097
|
-
handle.rollback = () =>
|
|
1100
|
+
handle.rollback = () => __awaiter(this, void 0, void 0, function* () {
|
|
1098
1101
|
rollbackRequested = true;
|
|
1099
1102
|
try {
|
|
1100
1103
|
nativeTransaction.abort();
|
|
@@ -1106,12 +1109,12 @@ class Database {
|
|
|
1106
1109
|
return handle;
|
|
1107
1110
|
}
|
|
1108
1111
|
beginTransaction(entityNames_1) {
|
|
1109
|
-
return
|
|
1112
|
+
return __awaiter(this, arguments, void 0, function* (entityNames, mode = 'readwrite') {
|
|
1110
1113
|
return this.createTransactionHandle(entityNames, mode);
|
|
1111
1114
|
});
|
|
1112
1115
|
}
|
|
1113
1116
|
transaction(callback) {
|
|
1114
|
-
return
|
|
1117
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1115
1118
|
const tx = this.createTransactionHandle(this.classes.map((cls) => cls.name), 'readwrite');
|
|
1116
1119
|
try {
|
|
1117
1120
|
const result = yield callback(tx);
|
|
@@ -1153,4 +1156,5 @@ class Database {
|
|
|
1153
1156
|
return cls ? Reflect.getMetadata('version', cls) || 1 : undefined;
|
|
1154
1157
|
}
|
|
1155
1158
|
}
|
|
1156
|
-
|
|
1159
|
+
|
|
1160
|
+
export { CompositeKeyPath, DataClass, Database, Index, KeyGenerators, KeyPath, RetentionPolicy, Validate };
|