@ztimson/utils 0.25.1 → 0.25.3
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 +18 -8
- package/dist/index.cjs +62 -32
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +62 -32
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
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 req = indexedDB.open(this.db, this.version);
|
|
401
|
+
this.connection = new Promise((resolve, reject) => {
|
|
402
|
+
const req = indexedDB.open(this.database, this.version);
|
|
404
403
|
req.onerror = () => reject(req.error);
|
|
405
|
-
req.onsuccess = () =>
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
404
|
+
req.onsuccess = () => {
|
|
405
|
+
const db = req.result;
|
|
406
|
+
if (tables.find((s) => !db.objectStoreNames.contains(typeof s === "string" ? s : s.name))) {
|
|
407
|
+
db.close();
|
|
408
|
+
Object.assign(this, new Database(this.database, this.tables, db.version + 1));
|
|
409
|
+
} else {
|
|
410
|
+
this.version = db.version;
|
|
411
|
+
resolve(db);
|
|
412
|
+
}
|
|
413
|
+
};
|
|
414
|
+
req.onupgradeneeded = () => {
|
|
415
|
+
const db = req.result;
|
|
416
|
+
Array.from(db.objectStoreNames).filter((s) => !this.tables.find((t) => typeof t === "string" ? t : t.name == s)).forEach((name) => db.deleteObjectStore(name));
|
|
417
|
+
tables.filter((t) => !db.objectStoreNames.contains(typeof t === "string" ? t : t.name)).forEach((t) => {
|
|
418
|
+
db.createObjectStore(typeof t === "string" ? t : t.name, {
|
|
419
|
+
keyPath: typeof t === "string" ? void 0 : t.key
|
|
420
|
+
});
|
|
421
|
+
});
|
|
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);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
class Table {
|
|
433
|
+
constructor(database, name) {
|
|
434
|
+
this.database = database;
|
|
435
|
+
this.name = name;
|
|
436
|
+
}
|
|
437
|
+
async tx(schema, fn2, readonly = false) {
|
|
438
|
+
const db = await this.database.connection;
|
|
439
|
+
const tx = db.transaction(schema, readonly ? "readonly" : "readwrite");
|
|
440
|
+
const store = tx.objectStore(schema);
|
|
417
441
|
return new Promise((resolve, reject) => {
|
|
418
442
|
const request = fn2(store);
|
|
419
443
|
request.onsuccess = () => resolve(request.result);
|
|
@@ -421,29 +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
|
-
|
|
431
|
-
return this.tx(this.collection, (store) => store.put(value, key));
|
|
454
|
+
return this.tx(this.name, (store) => store.put(value, key));
|
|
432
455
|
}
|
|
433
456
|
getAll() {
|
|
434
|
-
return this.tx(this.
|
|
457
|
+
return this.tx(this.name, (store) => store.getAll(), true);
|
|
435
458
|
}
|
|
436
459
|
getAllKeys() {
|
|
437
|
-
return this.tx(this.
|
|
460
|
+
return this.tx(this.name, (store) => store.getAllKeys(), true);
|
|
438
461
|
}
|
|
439
462
|
get(key) {
|
|
440
|
-
return this.tx(this.
|
|
463
|
+
return this.tx(this.name, (store) => store.get(key), true);
|
|
441
464
|
}
|
|
442
465
|
delete(key) {
|
|
443
|
-
return this.tx(this.
|
|
466
|
+
return this.tx(this.name, (store) => store.delete(key));
|
|
444
467
|
}
|
|
445
468
|
clear() {
|
|
446
|
-
return this.tx(this.
|
|
469
|
+
return this.tx(this.name, (store) => store.clear());
|
|
447
470
|
}
|
|
448
471
|
}
|
|
449
472
|
class Cache {
|
|
@@ -466,10 +489,13 @@ class Cache {
|
|
|
466
489
|
this.options = options;
|
|
467
490
|
if (options.storageKey && !options.storage && typeof Storage !== "undefined") options.storage = localStorage;
|
|
468
491
|
if (options.storage) {
|
|
469
|
-
if (options.storage instanceof
|
|
492
|
+
if (options.storage instanceof Table) {
|
|
470
493
|
(async () => {
|
|
471
494
|
var _a2;
|
|
472
|
-
(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
|
+
});
|
|
473
499
|
})();
|
|
474
500
|
} else if (options.storageKey) {
|
|
475
501
|
const stored = (_a = options.storage) == null ? void 0 : _a.getItem(options.storageKey);
|
|
@@ -497,7 +523,7 @@ class Cache {
|
|
|
497
523
|
}
|
|
498
524
|
save(key) {
|
|
499
525
|
if (this.options.storage) {
|
|
500
|
-
if (this.options.storage instanceof
|
|
526
|
+
if (this.options.storage instanceof Table) {
|
|
501
527
|
this.options.storage.put(key, this.store[key]);
|
|
502
528
|
} else if (this.options.storageKey) {
|
|
503
529
|
this.options.storage.setItem(this.options.storageKey, JSONSanitize(this.store));
|
|
@@ -564,13 +590,16 @@ class Cache {
|
|
|
564
590
|
*/
|
|
565
591
|
expire(key) {
|
|
566
592
|
this.complete = false;
|
|
567
|
-
if (this.options.expiryPolicy == "keep")
|
|
568
|
-
|
|
593
|
+
if (this.options.expiryPolicy == "keep") {
|
|
594
|
+
this.store[key]._expired = true;
|
|
595
|
+
this.save(key);
|
|
596
|
+
} else this.delete(key);
|
|
569
597
|
return this;
|
|
570
598
|
}
|
|
571
599
|
/**
|
|
572
600
|
* Get item from the cache
|
|
573
601
|
* @param {K} key Key to lookup
|
|
602
|
+
* @param expired Include expired items
|
|
574
603
|
* @return {T} Cached item
|
|
575
604
|
*/
|
|
576
605
|
get(key, expired) {
|
|
@@ -1999,8 +2028,8 @@ export {
|
|
|
1999
2028
|
CliBackground,
|
|
2000
2029
|
CliEffects,
|
|
2001
2030
|
CliForeground,
|
|
2002
|
-
Collection,
|
|
2003
2031
|
CustomError,
|
|
2032
|
+
Database,
|
|
2004
2033
|
ForbiddenError,
|
|
2005
2034
|
GatewayTimeoutError,
|
|
2006
2035
|
Http,
|
|
@@ -2025,6 +2054,7 @@ export {
|
|
|
2025
2054
|
PromiseProgress,
|
|
2026
2055
|
SYMBOL_LIST,
|
|
2027
2056
|
ServiceUnavailableError,
|
|
2057
|
+
Table,
|
|
2028
2058
|
TypedEmitter,
|
|
2029
2059
|
UnauthorizedError,
|
|
2030
2060
|
addUnique,
|