@ztimson/utils 0.25.0 → 0.25.1

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 CHANGED
@@ -1,8 +1,9 @@
1
+ import { Collection } from './database.ts';
1
2
  export type CacheOptions = {
2
3
  /** Delete keys automatically after x amount of seconds */
3
4
  ttl?: number;
4
5
  /** Storage to persist cache */
5
- storage?: Storage;
6
+ storage?: Storage | Collection<any, any>;
6
7
  /** Key cache will be stored under */
7
8
  storageKey?: string;
8
9
  /** Keep or delete cached items once expired, defaults to delete */
@@ -0,0 +1,17 @@
1
+ export declare class Collection<K extends IDBValidKey, T> {
2
+ readonly db: string;
3
+ readonly collection: string;
4
+ readonly version?: number | undefined;
5
+ private setup?;
6
+ private database;
7
+ constructor(db: string, collection: string, version?: number | undefined, setup?: ((db: IDBDatabase, event: IDBVersionChangeEvent) => void) | undefined);
8
+ tx<T>(collection: string, fn: ((store: IDBObjectStore) => IDBRequest), readonly?: boolean): Promise<T>;
9
+ add(value: T, key?: K): Promise<void>;
10
+ count(): Promise<number>;
11
+ put(key: K, value: T): Promise<void>;
12
+ getAll(): Promise<T[]>;
13
+ getAllKeys(): Promise<K[]>;
14
+ get(key: K): Promise<T>;
15
+ delete(key: K): Promise<void>;
16
+ clear(): Promise<void>;
17
+ }
package/dist/index.cjs CHANGED
@@ -396,6 +396,60 @@ ${opts.message || this.desc}`;
396
396
  function makeArray(value) {
397
397
  return Array.isArray(value) ? value : [value];
398
398
  }
399
+ class Collection {
400
+ constructor(db, collection, version, setup) {
401
+ __publicField(this, "database");
402
+ this.db = db;
403
+ this.collection = collection;
404
+ this.version = version;
405
+ this.setup = setup;
406
+ this.database = new Promise((resolve, reject) => {
407
+ const req = indexedDB.open(this.db, this.version);
408
+ req.onerror = () => reject(req.error);
409
+ req.onsuccess = () => resolve(req.result);
410
+ req.onupgradeneeded = (event) => {
411
+ const db2 = req.result;
412
+ if (!db2.objectStoreNames.contains(collection)) db2.createObjectStore(collection, { keyPath: void 0 });
413
+ if (this.setup) this.setup(db2, event);
414
+ };
415
+ });
416
+ }
417
+ async tx(collection, fn2, readonly) {
418
+ const db = await this.database;
419
+ const tx = db.transaction(collection, readonly ? "readonly" : "readwrite");
420
+ const store = tx.objectStore(collection);
421
+ return new Promise((resolve, reject) => {
422
+ const request = fn2(store);
423
+ request.onsuccess = () => resolve(request.result);
424
+ request.onerror = () => reject(request.error);
425
+ });
426
+ }
427
+ add(value, key) {
428
+ return this.tx(this.collection, (store) => store.add(value, key));
429
+ }
430
+ count() {
431
+ return this.tx(this.collection, (store) => store.count(), true);
432
+ }
433
+ put(key, value) {
434
+ debugger;
435
+ return this.tx(this.collection, (store) => store.put(value, key));
436
+ }
437
+ getAll() {
438
+ return this.tx(this.collection, (store) => store.getAll(), true);
439
+ }
440
+ getAllKeys() {
441
+ return this.tx(this.collection, (store) => store.getAllKeys(), true);
442
+ }
443
+ get(key) {
444
+ return this.tx(this.collection, (store) => store.get(key), true);
445
+ }
446
+ delete(key) {
447
+ return this.tx(this.collection, (store) => store.delete(key));
448
+ }
449
+ clear() {
450
+ return this.tx(this.collection, (store) => store.clear());
451
+ }
452
+ }
399
453
  class Cache {
400
454
  /**
401
455
  * Create new cache
@@ -411,14 +465,19 @@ ${opts.message || this.desc}`;
411
465
  * @return {T[]} Array of items
412
466
  */
413
467
  __publicField(this, "values", this.all());
468
+ var _a;
414
469
  this.key = key;
415
470
  this.options = options;
416
- if (options.storageKey && !options.storage && typeof Storage !== "undefined")
417
- options.storage = localStorage;
418
- if (options.storageKey && options.storage) {
419
- const stored = options.storage.getItem(options.storageKey);
420
- if (stored) {
421
- try {
471
+ if (options.storageKey && !options.storage && typeof Storage !== "undefined") options.storage = localStorage;
472
+ if (options.storage) {
473
+ if (options.storage instanceof Collection) {
474
+ (async () => {
475
+ var _a2;
476
+ (await ((_a2 = options.storage) == null ? void 0 : _a2.getAll())).forEach((v) => this.add(v));
477
+ })();
478
+ } else if (options.storageKey) {
479
+ const stored = (_a = options.storage) == null ? void 0 : _a.getItem(options.storageKey);
480
+ if (stored != null) try {
422
481
  Object.assign(this.store, JSON.parse(stored));
423
482
  } catch {
424
483
  }
@@ -440,9 +499,14 @@ ${opts.message || this.desc}`;
440
499
  if (!this.key) throw new Error("No key defined");
441
500
  return value[this.key];
442
501
  }
443
- save() {
444
- if (this.options.storageKey && this.options.storage)
445
- this.options.storage.setItem(this.options.storageKey, JSON.stringify(this.store));
502
+ save(key) {
503
+ if (this.options.storage) {
504
+ if (this.options.storage instanceof Collection) {
505
+ this.options.storage.put(key, this.store[key]);
506
+ } else if (this.options.storageKey) {
507
+ this.options.storage.setItem(this.options.storageKey, JSONSanitize(this.store));
508
+ }
509
+ }
446
510
  }
447
511
  /**
448
512
  * Get all cached items
@@ -488,7 +552,7 @@ ${opts.message || this.desc}`;
488
552
  */
489
553
  delete(key) {
490
554
  delete this.store[key];
491
- this.save();
555
+ this.save(key);
492
556
  return this;
493
557
  }
494
558
  /**
@@ -546,10 +610,10 @@ ${opts.message || this.desc}`;
546
610
  set(key, value, ttl = this.options.ttl) {
547
611
  if (this.options.expiryPolicy == "keep") delete value._expired;
548
612
  this.store[key] = value;
549
- this.save();
613
+ this.save(key);
550
614
  if (ttl) setTimeout(() => {
551
615
  this.expire(key);
552
- this.save();
616
+ this.save(key);
553
617
  }, (ttl || 0) * 1e3);
554
618
  return this;
555
619
  }
@@ -1938,6 +2002,7 @@ ${opts.message || this.desc}`;
1938
2002
  exports2.CliBackground = CliBackground;
1939
2003
  exports2.CliEffects = CliEffects;
1940
2004
  exports2.CliForeground = CliForeground;
2005
+ exports2.Collection = Collection;
1941
2006
  exports2.CustomError = CustomError;
1942
2007
  exports2.ForbiddenError = ForbiddenError;
1943
2008
  exports2.GatewayTimeoutError = GatewayTimeoutError;