@ztimson/utils 0.25.25 → 0.25.26

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/index.mjs CHANGED
@@ -619,8 +619,9 @@ class Database {
619
619
  if (typeof table == "string") table = { name: table };
620
620
  const conn = await this.connection;
621
621
  if (!this.includes(table.name)) {
622
+ const newDb = new Database(this.database, [...this.tables, table], (this.version ?? 0) + 1);
622
623
  conn.close();
623
- Object.assign(this, new Database(this.database, [...this.tables, table], (this.version ?? 0) + 1));
624
+ Object.assign(this, newDb);
624
625
  await this.connection;
625
626
  }
626
627
  return this.table(table.name);
@@ -631,8 +632,9 @@ class Database {
631
632
  if (typeof table == "string") table = { name: table };
632
633
  if (!this.includes(table.name)) return;
633
634
  const conn = await this.connection;
635
+ const newDb = new Database(this.database, this.tables.filter((t) => t.name != table.name), (this.version ?? 0) + 1);
634
636
  conn.close();
635
- Object.assign(this, new Database(this.database, this.tables.filter((t) => t.name != table.name), (this.version ?? 0) + 1));
637
+ Object.assign(this, newDb);
636
638
  await this.connection;
637
639
  });
638
640
  }
@@ -719,25 +721,28 @@ class Cache {
719
721
  var _a;
720
722
  this.key = key;
721
723
  this.options = options;
722
- let resolve;
723
- this.loading = new Promise((r) => resolve = r);
724
- if (options.storageKey && !options.storage && typeof Storage !== "undefined") options.storage = localStorage;
725
- if (options.storage) {
726
- if (options.storage instanceof Table) {
724
+ let done;
725
+ this.loading = new Promise((r) => done = r);
726
+ if (this.options.persistentStorage != null) {
727
+ if (typeof this.options.persistentStorage == "string")
728
+ this.options.persistentStorage = { storage: localStorage, key: this.options.persistentStorage };
729
+ if (((_a = this.options.persistentStorage) == null ? void 0 : _a.storage) instanceof Database) {
727
730
  (async () => {
728
- var _a2;
729
- this.addAll(await ((_a2 = options.storage) == null ? void 0 : _a2.getAll()), false);
730
- resolve();
731
+ const persists = this.options.persistentStorage;
732
+ const table = await persists.storage.createTable({ name: persists.key, key: this.key });
733
+ const rows = await table.getAll();
734
+ Object.assign(this.store, rows.reduce((acc, row) => ({ ...acc, [this.getKey(row)]: row }), {}));
735
+ done();
731
736
  })();
732
- } else if (options.storageKey) {
733
- const stored = (_a = options.storage) == null ? void 0 : _a.getItem(options.storageKey);
737
+ } else {
738
+ const stored = this.options.persistentStorage.storage.getItem(this.options.persistentStorage.key);
734
739
  if (stored != null) try {
735
740
  Object.assign(this.store, JSON.parse(stored));
736
741
  } catch {
737
742
  }
738
- resolve();
743
+ done();
739
744
  }
740
- } else resolve();
745
+ }
741
746
  return new Proxy(this, {
742
747
  get: (target, prop) => {
743
748
  if (prop in target) return target[prop];
@@ -756,23 +761,19 @@ class Cache {
756
761
  return value[this.key];
757
762
  }
758
763
  save(key) {
759
- if (this.options.storage) {
760
- if (this.options.storage instanceof Table) {
761
- if (key == null) {
762
- let rows = this.entries();
763
- rows.forEach(([k, v]) => {
764
- var _a;
765
- return (_a = this.options.storage) == null ? void 0 : _a.put(k, v);
766
- });
767
- rows = rows.map(([k]) => k);
768
- this.options.storage.getAllKeys().then((keys) => keys.filter((k) => !rows.includes(k)).forEach((k) => {
769
- var _a;
770
- return (_a = this.options.storage) == null ? void 0 : _a.delete(k);
771
- }));
772
- } else if (this.store[key] === void 0) this.options.storage.delete(key);
773
- else this.options.storage.put(key, this.store[key]);
774
- } else if (this.options.storageKey) {
775
- this.options.storage.setItem(this.options.storageKey, JSONSanitize(this.store));
764
+ const persists = this.options.persistentStorage;
765
+ if (!!(persists == null ? void 0 : persists.storage)) {
766
+ if (persists.storage instanceof Database) {
767
+ persists.storage.createTable({ name: persists.storage.key, key: this.key }).then((table) => {
768
+ if (key) {
769
+ table.set(key, this.get(key));
770
+ } else {
771
+ table.clear();
772
+ this.all().forEach((row) => table.add(row));
773
+ }
774
+ });
775
+ } else {
776
+ persists.storage.setItem(persists.storage.key, JSONSanitize(this.all(true)));
776
777
  }
777
778
  }
778
779
  }