@ztimson/utils 0.25.25 → 0.25.27

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,11 +1,12 @@
1
- import { Table } from './database.ts';
1
+ import { Database } 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 | Table<any, any>;
7
- /** Key cache will be stored under */
8
- storageKey?: string;
6
+ persistentStorage?: {
7
+ storage: Storage | Database;
8
+ key: string;
9
+ } | string;
9
10
  /** Keep or delete cached items once expired, defaults to delete */
10
11
  expiryPolicy?: 'delete' | 'keep';
11
12
  };
package/dist/index.cjs CHANGED
@@ -623,8 +623,9 @@ ${opts.message || this.desc}`;
623
623
  if (typeof table == "string") table = { name: table };
624
624
  const conn = await this.connection;
625
625
  if (!this.includes(table.name)) {
626
+ const newDb = new Database(this.database, [...this.tables, table], (this.version ?? 0) + 1);
626
627
  conn.close();
627
- Object.assign(this, new Database(this.database, [...this.tables, table], (this.version ?? 0) + 1));
628
+ Object.assign(this, newDb);
628
629
  await this.connection;
629
630
  }
630
631
  return this.table(table.name);
@@ -635,8 +636,9 @@ ${opts.message || this.desc}`;
635
636
  if (typeof table == "string") table = { name: table };
636
637
  if (!this.includes(table.name)) return;
637
638
  const conn = await this.connection;
639
+ const newDb = new Database(this.database, this.tables.filter((t) => t.name != table.name), (this.version ?? 0) + 1);
638
640
  conn.close();
639
- Object.assign(this, new Database(this.database, this.tables.filter((t) => t.name != table.name), (this.version ?? 0) + 1));
641
+ Object.assign(this, newDb);
640
642
  await this.connection;
641
643
  });
642
644
  }
@@ -720,28 +722,31 @@ ${opts.message || this.desc}`;
720
722
  * @return {T[]} Array of items
721
723
  */
722
724
  __publicField(this, "values", this.all());
723
- var _a;
725
+ var _a, _b, _c, _d;
724
726
  this.key = key;
725
727
  this.options = options;
726
- let resolve;
727
- this.loading = new Promise((r) => resolve = r);
728
- if (options.storageKey && !options.storage && typeof Storage !== "undefined") options.storage = localStorage;
729
- if (options.storage) {
730
- if (options.storage instanceof Table) {
728
+ let done;
729
+ this.loading = new Promise((r) => done = r);
730
+ if (this.options.persistentStorage != null) {
731
+ if (typeof this.options.persistentStorage == "string")
732
+ this.options.persistentStorage = { storage: localStorage, key: this.options.persistentStorage };
733
+ if (((_b = (_a = this.options.persistentStorage) == null ? void 0 : _a.storage) == null ? void 0 : _b.constructor.name) == "Database") {
731
734
  (async () => {
732
- var _a2;
733
- this.addAll(await ((_a2 = options.storage) == null ? void 0 : _a2.getAll()), false);
734
- resolve();
735
+ const persists = this.options.persistentStorage;
736
+ const table = await persists.storage.createTable({ name: persists.key, key: this.key });
737
+ const rows = await table.getAll();
738
+ Object.assign(this.store, rows.reduce((acc, row) => ({ ...acc, [this.getKey(row)]: row }), {}));
739
+ done();
735
740
  })();
736
- } else if (options.storageKey) {
737
- const stored = (_a = options.storage) == null ? void 0 : _a.getItem(options.storageKey);
741
+ } else if (((_d = (_c = this.options.persistentStorage) == null ? void 0 : _c.storage) == null ? void 0 : _d.constructor.name) == "Storage") {
742
+ const stored = this.options.persistentStorage.storage.getItem(this.options.persistentStorage.key);
738
743
  if (stored != null) try {
739
744
  Object.assign(this.store, JSON.parse(stored));
740
745
  } catch {
741
746
  }
742
- resolve();
747
+ done();
743
748
  }
744
- } else resolve();
749
+ }
745
750
  return new Proxy(this, {
746
751
  get: (target, prop) => {
747
752
  if (prop in target) return target[prop];
@@ -760,23 +765,19 @@ ${opts.message || this.desc}`;
760
765
  return value[this.key];
761
766
  }
762
767
  save(key) {
763
- if (this.options.storage) {
764
- if (this.options.storage instanceof Table) {
765
- if (key == null) {
766
- let rows = this.entries();
767
- rows.forEach(([k, v]) => {
768
- var _a;
769
- return (_a = this.options.storage) == null ? void 0 : _a.put(k, v);
770
- });
771
- rows = rows.map(([k]) => k);
772
- this.options.storage.getAllKeys().then((keys) => keys.filter((k) => !rows.includes(k)).forEach((k) => {
773
- var _a;
774
- return (_a = this.options.storage) == null ? void 0 : _a.delete(k);
775
- }));
776
- } else if (this.store[key] === void 0) this.options.storage.delete(key);
777
- else this.options.storage.put(key, this.store[key]);
778
- } else if (this.options.storageKey) {
779
- this.options.storage.setItem(this.options.storageKey, JSONSanitize(this.store));
768
+ const persists = this.options.persistentStorage;
769
+ if (!!(persists == null ? void 0 : persists.storage)) {
770
+ if (persists.storage instanceof Database) {
771
+ persists.storage.createTable({ name: persists.storage.key, key: this.key }).then((table) => {
772
+ if (key) {
773
+ table.set(key, this.get(key));
774
+ } else {
775
+ table.clear();
776
+ this.all().forEach((row) => table.add(row));
777
+ }
778
+ });
779
+ } else {
780
+ persists.storage.setItem(persists.storage.key, JSONSanitize(this.all(true)));
780
781
  }
781
782
  }
782
783
  }