@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/cache.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import {
|
|
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
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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,
|
|
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,
|
|
641
|
+
Object.assign(this, newDb);
|
|
640
642
|
await this.connection;
|
|
641
643
|
});
|
|
642
644
|
}
|
|
@@ -723,25 +725,28 @@ ${opts.message || this.desc}`;
|
|
|
723
725
|
var _a;
|
|
724
726
|
this.key = key;
|
|
725
727
|
this.options = options;
|
|
726
|
-
let
|
|
727
|
-
this.loading = new Promise((r) =>
|
|
728
|
-
if (
|
|
729
|
-
|
|
730
|
-
|
|
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 (((_a = this.options.persistentStorage) == null ? void 0 : _a.storage) instanceof Database) {
|
|
731
734
|
(async () => {
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
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
|
|
737
|
-
const stored =
|
|
741
|
+
} else {
|
|
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
|
-
|
|
747
|
+
done();
|
|
743
748
|
}
|
|
744
|
-
}
|
|
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
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
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
|
}
|