@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/cache.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Table } from './database.ts';
|
|
2
2
|
export type CacheOptions = {
|
|
3
3
|
/** Delete keys automatically after x amount of seconds */
|
|
4
4
|
ttl?: number;
|
|
5
5
|
/** Storage to persist cache */
|
|
6
|
-
storage?: Storage |
|
|
6
|
+
storage?: Storage | Table<any, any>;
|
|
7
7
|
/** Key cache will be stored under */
|
|
8
8
|
storageKey?: string;
|
|
9
9
|
/** Keep or delete cached items once expired, defaults to delete */
|
|
@@ -72,6 +72,7 @@ export declare class Cache<K extends string | number | symbol, T> {
|
|
|
72
72
|
/**
|
|
73
73
|
* Get item from the cache
|
|
74
74
|
* @param {K} key Key to lookup
|
|
75
|
+
* @param expired Include expired items
|
|
75
76
|
* @return {T} Cached item
|
|
76
77
|
*/
|
|
77
78
|
get(key: K, expired?: boolean): CachedValue<T> | null;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export type TableOptions = {
|
|
2
|
+
name: string;
|
|
3
|
+
key?: string;
|
|
4
|
+
};
|
|
5
|
+
export declare class Database {
|
|
6
|
+
readonly database: string;
|
|
7
|
+
readonly tables: (string | TableOptions)[];
|
|
8
|
+
version?: number | undefined;
|
|
9
|
+
connection: Promise<IDBDatabase>;
|
|
10
|
+
constructor(database: string, tables: (string | TableOptions)[], version?: number | undefined);
|
|
11
|
+
includes(name: string): boolean;
|
|
12
|
+
table<K extends IDBValidKey = any, T = any>(name: any): Table<K, T>;
|
|
13
|
+
}
|
|
14
|
+
export declare class Table<K extends IDBValidKey = any, T = any> {
|
|
15
|
+
private readonly database;
|
|
16
|
+
readonly name: string;
|
|
17
|
+
constructor(database: Database, name: string);
|
|
18
|
+
tx<R>(table: string, fn: (store: IDBObjectStore) => IDBRequest, readonly?: boolean): Promise<R>;
|
|
19
|
+
add(value: T, key?: K): Promise<void>;
|
|
20
|
+
count(): Promise<number>;
|
|
21
|
+
put(key: K, value: T): Promise<void>;
|
|
22
|
+
getAll(): Promise<T[]>;
|
|
23
|
+
getAllKeys(): Promise<K[]>;
|
|
24
|
+
get(key: K): Promise<T>;
|
|
25
|
+
delete(key: K): Promise<void>;
|
|
26
|
+
clear(): Promise<void>;
|
|
27
|
+
}
|
package/dist/index.cjs
CHANGED
|
@@ -396,28 +396,52 @@ ${opts.message || this.desc}`;
|
|
|
396
396
|
function makeArray(value) {
|
|
397
397
|
return Array.isArray(value) ? value : [value];
|
|
398
398
|
}
|
|
399
|
-
class
|
|
400
|
-
constructor(
|
|
401
|
-
__publicField(this, "
|
|
402
|
-
this.
|
|
403
|
-
this.
|
|
399
|
+
class Database {
|
|
400
|
+
constructor(database, tables, version) {
|
|
401
|
+
__publicField(this, "connection");
|
|
402
|
+
this.database = database;
|
|
403
|
+
this.tables = tables;
|
|
404
404
|
this.version = version;
|
|
405
|
-
this.
|
|
406
|
-
|
|
407
|
-
const
|
|
405
|
+
this.connection = new Promise((resolve, reject) => {
|
|
406
|
+
const req = indexedDB.open(this.database, this.version);
|
|
407
|
+
const tableNames = new ASet(tables.map((t) => (typeof t == "object" ? t.name : t).toString()));
|
|
408
408
|
req.onerror = () => reject(req.error);
|
|
409
|
-
req.onsuccess = () =>
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
409
|
+
req.onsuccess = () => {
|
|
410
|
+
const db = req.result;
|
|
411
|
+
if (tableNames.symmetricDifference(new ASet(Array.from(db.objectStoreNames))).length) {
|
|
412
|
+
db.close();
|
|
413
|
+
Object.assign(this, new Database(this.database, this.tables, db.version + 1));
|
|
414
|
+
} else {
|
|
415
|
+
this.version = db.version;
|
|
416
|
+
resolve(db);
|
|
417
|
+
}
|
|
418
|
+
};
|
|
419
|
+
req.onupgradeneeded = () => {
|
|
420
|
+
const db = req.result;
|
|
421
|
+
const existingTables = new ASet(Array.from(db.objectStoreNames));
|
|
422
|
+
console.log("delete", existingTables.difference(tableNames));
|
|
423
|
+
existingTables.difference(tableNames).forEach((name) => db.deleteObjectStore(name));
|
|
424
|
+
console.log("create", tableNames.difference(existingTables));
|
|
425
|
+
tableNames.difference(existingTables).forEach((name) => db.createObjectStore(name));
|
|
414
426
|
};
|
|
415
427
|
});
|
|
416
428
|
}
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
429
|
+
includes(name) {
|
|
430
|
+
return this.tables.some((t) => typeof t === "string" ? name === t : name === t.name);
|
|
431
|
+
}
|
|
432
|
+
table(name) {
|
|
433
|
+
return new Table(this, name.toString());
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
class Table {
|
|
437
|
+
constructor(database, name) {
|
|
438
|
+
this.database = database;
|
|
439
|
+
this.name = name;
|
|
440
|
+
}
|
|
441
|
+
async tx(table, fn2, readonly = false) {
|
|
442
|
+
const db = await this.database.connection;
|
|
443
|
+
const tx = db.transaction(table, readonly ? "readonly" : "readwrite");
|
|
444
|
+
const store = tx.objectStore(table);
|
|
421
445
|
return new Promise((resolve, reject) => {
|
|
422
446
|
const request = fn2(store);
|
|
423
447
|
request.onsuccess = () => resolve(request.result);
|
|
@@ -425,28 +449,28 @@ ${opts.message || this.desc}`;
|
|
|
425
449
|
});
|
|
426
450
|
}
|
|
427
451
|
add(value, key) {
|
|
428
|
-
return this.tx(this.
|
|
452
|
+
return this.tx(this.name, (store) => store.add(value, key));
|
|
429
453
|
}
|
|
430
454
|
count() {
|
|
431
|
-
return this.tx(this.
|
|
455
|
+
return this.tx(this.name, (store) => store.count(), true);
|
|
432
456
|
}
|
|
433
457
|
put(key, value) {
|
|
434
|
-
return this.tx(this.
|
|
458
|
+
return this.tx(this.name, (store) => store.put(value, key));
|
|
435
459
|
}
|
|
436
460
|
getAll() {
|
|
437
|
-
return this.tx(this.
|
|
461
|
+
return this.tx(this.name, (store) => store.getAll(), true);
|
|
438
462
|
}
|
|
439
463
|
getAllKeys() {
|
|
440
|
-
return this.tx(this.
|
|
464
|
+
return this.tx(this.name, (store) => store.getAllKeys(), true);
|
|
441
465
|
}
|
|
442
466
|
get(key) {
|
|
443
|
-
return this.tx(this.
|
|
467
|
+
return this.tx(this.name, (store) => store.get(key), true);
|
|
444
468
|
}
|
|
445
469
|
delete(key) {
|
|
446
|
-
return this.tx(this.
|
|
470
|
+
return this.tx(this.name, (store) => store.delete(key));
|
|
447
471
|
}
|
|
448
472
|
clear() {
|
|
449
|
-
return this.tx(this.
|
|
473
|
+
return this.tx(this.name, (store) => store.clear());
|
|
450
474
|
}
|
|
451
475
|
}
|
|
452
476
|
class Cache {
|
|
@@ -469,10 +493,13 @@ ${opts.message || this.desc}`;
|
|
|
469
493
|
this.options = options;
|
|
470
494
|
if (options.storageKey && !options.storage && typeof Storage !== "undefined") options.storage = localStorage;
|
|
471
495
|
if (options.storage) {
|
|
472
|
-
if (options.storage instanceof
|
|
496
|
+
if (options.storage instanceof Table) {
|
|
473
497
|
(async () => {
|
|
474
498
|
var _a2;
|
|
475
|
-
(await ((_a2 = options.storage) == null ? void 0 : _a2.getAll())).forEach((v) =>
|
|
499
|
+
return (await ((_a2 = options.storage) == null ? void 0 : _a2.getAll())).forEach((v) => {
|
|
500
|
+
console.log(v);
|
|
501
|
+
this.add(v);
|
|
502
|
+
});
|
|
476
503
|
})();
|
|
477
504
|
} else if (options.storageKey) {
|
|
478
505
|
const stored = (_a = options.storage) == null ? void 0 : _a.getItem(options.storageKey);
|
|
@@ -500,7 +527,7 @@ ${opts.message || this.desc}`;
|
|
|
500
527
|
}
|
|
501
528
|
save(key) {
|
|
502
529
|
if (this.options.storage) {
|
|
503
|
-
if (this.options.storage instanceof
|
|
530
|
+
if (this.options.storage instanceof Table) {
|
|
504
531
|
this.options.storage.put(key, this.store[key]);
|
|
505
532
|
} else if (this.options.storageKey) {
|
|
506
533
|
this.options.storage.setItem(this.options.storageKey, JSONSanitize(this.store));
|
|
@@ -567,13 +594,16 @@ ${opts.message || this.desc}`;
|
|
|
567
594
|
*/
|
|
568
595
|
expire(key) {
|
|
569
596
|
this.complete = false;
|
|
570
|
-
if (this.options.expiryPolicy == "keep")
|
|
571
|
-
|
|
597
|
+
if (this.options.expiryPolicy == "keep") {
|
|
598
|
+
this.store[key]._expired = true;
|
|
599
|
+
this.save(key);
|
|
600
|
+
} else this.delete(key);
|
|
572
601
|
return this;
|
|
573
602
|
}
|
|
574
603
|
/**
|
|
575
604
|
* Get item from the cache
|
|
576
605
|
* @param {K} key Key to lookup
|
|
606
|
+
* @param expired Include expired items
|
|
577
607
|
* @return {T} Cached item
|
|
578
608
|
*/
|
|
579
609
|
get(key, expired) {
|
|
@@ -2001,8 +2031,8 @@ ${opts.message || this.desc}`;
|
|
|
2001
2031
|
exports2.CliBackground = CliBackground;
|
|
2002
2032
|
exports2.CliEffects = CliEffects;
|
|
2003
2033
|
exports2.CliForeground = CliForeground;
|
|
2004
|
-
exports2.Collection = Collection;
|
|
2005
2034
|
exports2.CustomError = CustomError;
|
|
2035
|
+
exports2.Database = Database;
|
|
2006
2036
|
exports2.ForbiddenError = ForbiddenError;
|
|
2007
2037
|
exports2.GatewayTimeoutError = GatewayTimeoutError;
|
|
2008
2038
|
exports2.Http = Http;
|
|
@@ -2027,6 +2057,7 @@ ${opts.message || this.desc}`;
|
|
|
2027
2057
|
exports2.PromiseProgress = PromiseProgress;
|
|
2028
2058
|
exports2.SYMBOL_LIST = SYMBOL_LIST;
|
|
2029
2059
|
exports2.ServiceUnavailableError = ServiceUnavailableError;
|
|
2060
|
+
exports2.Table = Table;
|
|
2030
2061
|
exports2.TypedEmitter = TypedEmitter;
|
|
2031
2062
|
exports2.UnauthorizedError = UnauthorizedError;
|
|
2032
2063
|
exports2.addUnique = addUnique;
|