@ztimson/utils 0.25.2 → 0.25.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/dist/cache.d.ts +3 -2
- package/dist/database.d.ts +27 -0
- package/dist/index.cjs +62 -31
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.mjs +62 -31
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/dist/collection.d.ts +0 -17
package/dist/index.d.ts
CHANGED
package/dist/index.mjs
CHANGED
|
@@ -392,28 +392,52 @@ function makeUnique(arr) {
|
|
|
392
392
|
function makeArray(value) {
|
|
393
393
|
return Array.isArray(value) ? value : [value];
|
|
394
394
|
}
|
|
395
|
-
class
|
|
396
|
-
constructor(
|
|
397
|
-
__publicField(this, "
|
|
398
|
-
this.
|
|
399
|
-
this.
|
|
395
|
+
class Database {
|
|
396
|
+
constructor(database, tables, version) {
|
|
397
|
+
__publicField(this, "connection");
|
|
398
|
+
this.database = database;
|
|
399
|
+
this.tables = tables;
|
|
400
400
|
this.version = version;
|
|
401
|
-
this.
|
|
402
|
-
|
|
403
|
-
const
|
|
401
|
+
this.connection = new Promise((resolve, reject) => {
|
|
402
|
+
const req = indexedDB.open(this.database, this.version);
|
|
403
|
+
const tableNames = new ASet(tables.map((t) => (typeof t == "object" ? t.name : t).toString()));
|
|
404
404
|
req.onerror = () => reject(req.error);
|
|
405
|
-
req.onsuccess = () =>
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
405
|
+
req.onsuccess = () => {
|
|
406
|
+
const db = req.result;
|
|
407
|
+
if (tableNames.symmetricDifference(new ASet(Array.from(db.objectStoreNames))).length) {
|
|
408
|
+
db.close();
|
|
409
|
+
Object.assign(this, new Database(this.database, this.tables, db.version + 1));
|
|
410
|
+
} else {
|
|
411
|
+
this.version = db.version;
|
|
412
|
+
resolve(db);
|
|
413
|
+
}
|
|
414
|
+
};
|
|
415
|
+
req.onupgradeneeded = () => {
|
|
416
|
+
const db = req.result;
|
|
417
|
+
const existingTables = new ASet(Array.from(db.objectStoreNames));
|
|
418
|
+
console.log("delete", existingTables.difference(tableNames));
|
|
419
|
+
existingTables.difference(tableNames).forEach((name) => db.deleteObjectStore(name));
|
|
420
|
+
console.log("create", tableNames.difference(existingTables));
|
|
421
|
+
tableNames.difference(existingTables).forEach((name) => db.createObjectStore(name));
|
|
410
422
|
};
|
|
411
423
|
});
|
|
412
424
|
}
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
425
|
+
includes(name) {
|
|
426
|
+
return this.tables.some((t) => typeof t === "string" ? name === t : name === t.name);
|
|
427
|
+
}
|
|
428
|
+
table(name) {
|
|
429
|
+
return new Table(this, name.toString());
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
class Table {
|
|
433
|
+
constructor(database, name) {
|
|
434
|
+
this.database = database;
|
|
435
|
+
this.name = name;
|
|
436
|
+
}
|
|
437
|
+
async tx(table, fn2, readonly = false) {
|
|
438
|
+
const db = await this.database.connection;
|
|
439
|
+
const tx = db.transaction(table, readonly ? "readonly" : "readwrite");
|
|
440
|
+
const store = tx.objectStore(table);
|
|
417
441
|
return new Promise((resolve, reject) => {
|
|
418
442
|
const request = fn2(store);
|
|
419
443
|
request.onsuccess = () => resolve(request.result);
|
|
@@ -421,28 +445,28 @@ class Collection {
|
|
|
421
445
|
});
|
|
422
446
|
}
|
|
423
447
|
add(value, key) {
|
|
424
|
-
return this.tx(this.
|
|
448
|
+
return this.tx(this.name, (store) => store.add(value, key));
|
|
425
449
|
}
|
|
426
450
|
count() {
|
|
427
|
-
return this.tx(this.
|
|
451
|
+
return this.tx(this.name, (store) => store.count(), true);
|
|
428
452
|
}
|
|
429
453
|
put(key, value) {
|
|
430
|
-
return this.tx(this.
|
|
454
|
+
return this.tx(this.name, (store) => store.put(value, key));
|
|
431
455
|
}
|
|
432
456
|
getAll() {
|
|
433
|
-
return this.tx(this.
|
|
457
|
+
return this.tx(this.name, (store) => store.getAll(), true);
|
|
434
458
|
}
|
|
435
459
|
getAllKeys() {
|
|
436
|
-
return this.tx(this.
|
|
460
|
+
return this.tx(this.name, (store) => store.getAllKeys(), true);
|
|
437
461
|
}
|
|
438
462
|
get(key) {
|
|
439
|
-
return this.tx(this.
|
|
463
|
+
return this.tx(this.name, (store) => store.get(key), true);
|
|
440
464
|
}
|
|
441
465
|
delete(key) {
|
|
442
|
-
return this.tx(this.
|
|
466
|
+
return this.tx(this.name, (store) => store.delete(key));
|
|
443
467
|
}
|
|
444
468
|
clear() {
|
|
445
|
-
return this.tx(this.
|
|
469
|
+
return this.tx(this.name, (store) => store.clear());
|
|
446
470
|
}
|
|
447
471
|
}
|
|
448
472
|
class Cache {
|
|
@@ -465,10 +489,13 @@ class Cache {
|
|
|
465
489
|
this.options = options;
|
|
466
490
|
if (options.storageKey && !options.storage && typeof Storage !== "undefined") options.storage = localStorage;
|
|
467
491
|
if (options.storage) {
|
|
468
|
-
if (options.storage instanceof
|
|
492
|
+
if (options.storage instanceof Table) {
|
|
469
493
|
(async () => {
|
|
470
494
|
var _a2;
|
|
471
|
-
(await ((_a2 = options.storage) == null ? void 0 : _a2.getAll())).forEach((v) =>
|
|
495
|
+
return (await ((_a2 = options.storage) == null ? void 0 : _a2.getAll())).forEach((v) => {
|
|
496
|
+
console.log(v);
|
|
497
|
+
this.add(v);
|
|
498
|
+
});
|
|
472
499
|
})();
|
|
473
500
|
} else if (options.storageKey) {
|
|
474
501
|
const stored = (_a = options.storage) == null ? void 0 : _a.getItem(options.storageKey);
|
|
@@ -496,7 +523,7 @@ class Cache {
|
|
|
496
523
|
}
|
|
497
524
|
save(key) {
|
|
498
525
|
if (this.options.storage) {
|
|
499
|
-
if (this.options.storage instanceof
|
|
526
|
+
if (this.options.storage instanceof Table) {
|
|
500
527
|
this.options.storage.put(key, this.store[key]);
|
|
501
528
|
} else if (this.options.storageKey) {
|
|
502
529
|
this.options.storage.setItem(this.options.storageKey, JSONSanitize(this.store));
|
|
@@ -563,13 +590,16 @@ class Cache {
|
|
|
563
590
|
*/
|
|
564
591
|
expire(key) {
|
|
565
592
|
this.complete = false;
|
|
566
|
-
if (this.options.expiryPolicy == "keep")
|
|
567
|
-
|
|
593
|
+
if (this.options.expiryPolicy == "keep") {
|
|
594
|
+
this.store[key]._expired = true;
|
|
595
|
+
this.save(key);
|
|
596
|
+
} else this.delete(key);
|
|
568
597
|
return this;
|
|
569
598
|
}
|
|
570
599
|
/**
|
|
571
600
|
* Get item from the cache
|
|
572
601
|
* @param {K} key Key to lookup
|
|
602
|
+
* @param expired Include expired items
|
|
573
603
|
* @return {T} Cached item
|
|
574
604
|
*/
|
|
575
605
|
get(key, expired) {
|
|
@@ -1998,8 +2028,8 @@ export {
|
|
|
1998
2028
|
CliBackground,
|
|
1999
2029
|
CliEffects,
|
|
2000
2030
|
CliForeground,
|
|
2001
|
-
Collection,
|
|
2002
2031
|
CustomError,
|
|
2032
|
+
Database,
|
|
2003
2033
|
ForbiddenError,
|
|
2004
2034
|
GatewayTimeoutError,
|
|
2005
2035
|
Http,
|
|
@@ -2024,6 +2054,7 @@ export {
|
|
|
2024
2054
|
PromiseProgress,
|
|
2025
2055
|
SYMBOL_LIST,
|
|
2026
2056
|
ServiceUnavailableError,
|
|
2057
|
+
Table,
|
|
2027
2058
|
TypedEmitter,
|
|
2028
2059
|
UnauthorizedError,
|
|
2029
2060
|
addUnique,
|