@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 CHANGED
@@ -1,9 +1,9 @@
1
- import { Collection } from './database.ts';
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 | Collection<any, any>;
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;
@@ -1,11 +1,21 @@
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>;
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: string): 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>(schema: string, fn: (store: IDBObjectStore) => IDBRequest, readonly?: boolean): Promise<R>;
9
19
  add(value: T, key?: K): Promise<void>;
10
20
  count(): Promise<number>;
11
21
  put(key: K, value: T): Promise<void>;
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 Collection {
400
- constructor(db, collection, version, setup) {
401
- __publicField(this, "database");
402
- this.db = db;
403
- this.collection = collection;
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.setup = setup;
406
- this.database = new Promise((resolve, reject) => {
407
- const req = indexedDB.open(this.db, this.version);
405
+ this.connection = new Promise((resolve, reject) => {
406
+ const req = indexedDB.open(this.database, this.version);
408
407
  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);
408
+ req.onsuccess = () => {
409
+ const db = req.result;
410
+ if (tables.find((s) => !db.objectStoreNames.contains(typeof s === "string" ? s : s.name))) {
411
+ db.close();
412
+ Object.assign(this, new Database(this.database, this.tables, db.version + 1));
413
+ } else {
414
+ this.version = db.version;
415
+ resolve(db);
416
+ }
417
+ };
418
+ req.onupgradeneeded = () => {
419
+ const db = req.result;
420
+ Array.from(db.objectStoreNames).filter((s) => !this.tables.find((t) => typeof t === "string" ? t : t.name == s)).forEach((name) => db.deleteObjectStore(name));
421
+ tables.filter((t) => !db.objectStoreNames.contains(typeof t === "string" ? t : t.name)).forEach((t) => {
422
+ db.createObjectStore(typeof t === "string" ? t : t.name, {
423
+ keyPath: typeof t === "string" ? void 0 : t.key
424
+ });
425
+ });
414
426
  };
415
427
  });
416
428
  }
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);
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);
434
+ }
435
+ }
436
+ class Table {
437
+ constructor(database, name) {
438
+ this.database = database;
439
+ this.name = name;
440
+ }
441
+ async tx(schema, fn2, readonly = false) {
442
+ const db = await this.database.connection;
443
+ const tx = db.transaction(schema, readonly ? "readonly" : "readwrite");
444
+ const store = tx.objectStore(schema);
421
445
  return new Promise((resolve, reject) => {
422
446
  const request = fn2(store);
423
447
  request.onsuccess = () => resolve(request.result);
@@ -425,29 +449,28 @@ ${opts.message || this.desc}`;
425
449
  });
426
450
  }
427
451
  add(value, key) {
428
- return this.tx(this.collection, (store) => store.add(value, key));
452
+ return this.tx(this.name, (store) => store.add(value, key));
429
453
  }
430
454
  count() {
431
- return this.tx(this.collection, (store) => store.count(), true);
455
+ return this.tx(this.name, (store) => store.count(), true);
432
456
  }
433
457
  put(key, value) {
434
- debugger;
435
- return this.tx(this.collection, (store) => store.put(value, key));
458
+ return this.tx(this.name, (store) => store.put(value, key));
436
459
  }
437
460
  getAll() {
438
- return this.tx(this.collection, (store) => store.getAll(), true);
461
+ return this.tx(this.name, (store) => store.getAll(), true);
439
462
  }
440
463
  getAllKeys() {
441
- return this.tx(this.collection, (store) => store.getAllKeys(), true);
464
+ return this.tx(this.name, (store) => store.getAllKeys(), true);
442
465
  }
443
466
  get(key) {
444
- return this.tx(this.collection, (store) => store.get(key), true);
467
+ return this.tx(this.name, (store) => store.get(key), true);
445
468
  }
446
469
  delete(key) {
447
- return this.tx(this.collection, (store) => store.delete(key));
470
+ return this.tx(this.name, (store) => store.delete(key));
448
471
  }
449
472
  clear() {
450
- return this.tx(this.collection, (store) => store.clear());
473
+ return this.tx(this.name, (store) => store.clear());
451
474
  }
452
475
  }
453
476
  class Cache {
@@ -470,10 +493,13 @@ ${opts.message || this.desc}`;
470
493
  this.options = options;
471
494
  if (options.storageKey && !options.storage && typeof Storage !== "undefined") options.storage = localStorage;
472
495
  if (options.storage) {
473
- if (options.storage instanceof Collection) {
496
+ if (options.storage instanceof Table) {
474
497
  (async () => {
475
498
  var _a2;
476
- (await ((_a2 = options.storage) == null ? void 0 : _a2.getAll())).forEach((v) => this.add(v));
499
+ return (await ((_a2 = options.storage) == null ? void 0 : _a2.getAll())).forEach((v) => {
500
+ console.log(v);
501
+ this.add(v);
502
+ });
477
503
  })();
478
504
  } else if (options.storageKey) {
479
505
  const stored = (_a = options.storage) == null ? void 0 : _a.getItem(options.storageKey);
@@ -501,7 +527,7 @@ ${opts.message || this.desc}`;
501
527
  }
502
528
  save(key) {
503
529
  if (this.options.storage) {
504
- if (this.options.storage instanceof Collection) {
530
+ if (this.options.storage instanceof Table) {
505
531
  this.options.storage.put(key, this.store[key]);
506
532
  } else if (this.options.storageKey) {
507
533
  this.options.storage.setItem(this.options.storageKey, JSONSanitize(this.store));
@@ -568,13 +594,16 @@ ${opts.message || this.desc}`;
568
594
  */
569
595
  expire(key) {
570
596
  this.complete = false;
571
- if (this.options.expiryPolicy == "keep") this.store[key]._expired = true;
572
- else this.delete(key);
597
+ if (this.options.expiryPolicy == "keep") {
598
+ this.store[key]._expired = true;
599
+ this.save(key);
600
+ } else this.delete(key);
573
601
  return this;
574
602
  }
575
603
  /**
576
604
  * Get item from the cache
577
605
  * @param {K} key Key to lookup
606
+ * @param expired Include expired items
578
607
  * @return {T} Cached item
579
608
  */
580
609
  get(key, expired) {
@@ -2002,8 +2031,8 @@ ${opts.message || this.desc}`;
2002
2031
  exports2.CliBackground = CliBackground;
2003
2032
  exports2.CliEffects = CliEffects;
2004
2033
  exports2.CliForeground = CliForeground;
2005
- exports2.Collection = Collection;
2006
2034
  exports2.CustomError = CustomError;
2035
+ exports2.Database = Database;
2007
2036
  exports2.ForbiddenError = ForbiddenError;
2008
2037
  exports2.GatewayTimeoutError = GatewayTimeoutError;
2009
2038
  exports2.Http = Http;
@@ -2028,6 +2057,7 @@ ${opts.message || this.desc}`;
2028
2057
  exports2.PromiseProgress = PromiseProgress;
2029
2058
  exports2.SYMBOL_LIST = SYMBOL_LIST;
2030
2059
  exports2.ServiceUnavailableError = ServiceUnavailableError;
2060
+ exports2.Table = Table;
2031
2061
  exports2.TypedEmitter = TypedEmitter;
2032
2062
  exports2.UnauthorizedError = UnauthorizedError;
2033
2063
  exports2.addUnique = addUnique;