Compare commits

..

2 Commits

Author SHA1 Message Date
71552aa243 Fixed cache save to db
Some checks failed
Build / Build NPM Project (push) Successful in 43s
Build / Publish Documentation (push) Failing after 5s
Build / Tag Version (push) Successful in 8s
2025-07-07 15:27:07 -04:00
ce3878e18b Added db safe guards
Some checks failed
Build / Build NPM Project (push) Successful in 44s
Build / Publish Documentation (push) Failing after 5s
Build / Tag Version (push) Successful in 9s
2025-07-07 14:53:14 -04:00
3 changed files with 10 additions and 6 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@ztimson/utils",
"version": "0.26.2",
"version": "0.26.4",
"description": "Utility library",
"author": "Zak Timson",
"license": "MIT",

View File

@ -80,7 +80,7 @@ export class Cache<K extends string | number | symbol, T> {
if(persists.storage?.constructor.name == 'Database') {
(<Database>persists.storage).createTable({name: persists.key, key: <string>this.key}).then(table => {
if(key) {
table.set(key, this.get(key));
table.set(this.get(key), key);
} else {
table.clear();
this.all().forEach(row => table.add(row));

View File

@ -166,8 +166,11 @@ export class Table<K extends IDBValidKey = any, T = any> {
return this.tx(this.name, store => store.getAllKeys(), true);
}
put(key: K, value: T): Promise<void> {
return this.tx(this.name, store => store.put(value, key));
put(value: T, key?: string): Promise<void> {
return this.tx(this.name, store => {
if (store.keyPath) return store.put(value);
return store.put(value, key);
});
}
read(): Promise<T[]>;
@ -177,8 +180,9 @@ export class Table<K extends IDBValidKey = any, T = any> {
}
set(value: T, key?: K): Promise<void> {
if(!key && !(<any>value)[this.key]) return this.add(value);
return this.put(key || (<any>value)[this.key], value);
if(key) (<any>value)[this.key] = key;
if(!(<any>value)[this.key]) return this.add(value);
return this.put(value);
}
update = this.set;