@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/cache.d.ts +5 -4
- package/dist/index.cjs +32 -31
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +32 -31
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
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,
|
|
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,
|
|
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
|
|
723
|
-
this.loading = new Promise((r) =>
|
|
724
|
-
if (
|
|
725
|
-
|
|
726
|
-
|
|
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
|
-
|
|
729
|
-
|
|
730
|
-
|
|
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
|
|
733
|
-
const stored =
|
|
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
|
-
|
|
743
|
+
done();
|
|
739
744
|
}
|
|
740
|
-
}
|
|
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
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
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
|
}
|