Added indexdb wrapper & cache support for it
All checks were successful
Build / Build NPM Project (push) Successful in 1m24s
Build / Tag Version (push) Successful in 53s
Build / Publish Documentation (push) Successful in 1m28s

This commit is contained in:
Zakary Timson 2025-05-15 16:06:32 -04:00
parent fec373ca4c
commit 2a7fa18c0e
6 changed files with 166 additions and 16 deletions

15
index.html Normal file
View File

@ -0,0 +1,15 @@
<html>
<body>
<script type="module">
import {Cache, Collection} from './dist/index.mjs';
const cache = new Cache('id', {
ttl: 60,
storage: new Collection('test', 'test'),
expiryPolicy: 'delete'
});
cache.add({id: 1, name: 'zak', age: 27});
</script>
</body>
</html>

View File

@ -1,6 +1,6 @@
{
"name": "@ztimson/utils",
"version": "0.25.0",
"version": "0.25.1",
"description": "Utility library",
"author": "Zak Timson",
"license": "MIT",
@ -31,6 +31,7 @@
},
"devDependencies": {
"@types/jest": "^29.5.12",
"fake-indexeddb": "^6.0.1",
"jest": "^29.7.0",
"jest-junit": "^16.0.0",
"ts-jest": "^29.1.2",

View File

@ -1,10 +1,11 @@
import {deepCopy} from './objects.ts';
import {Collection} from './database.ts';
import {deepCopy, JSONSanitize} from './objects.ts';
export type CacheOptions = {
/** Delete keys automatically after x amount of seconds */
ttl?: number;
/** Storage to persist cache */
storage?: Storage;
storage?: Storage | Collection<any, any>;
/** Key cache will be stored under */
storageKey?: string;
/** Keep or delete cached items once expired, defaults to delete */
@ -30,13 +31,15 @@ export class Cache<K extends string | number | symbol, T> {
* @param options
*/
constructor(public readonly key?: keyof T, public readonly options: CacheOptions = {}) {
if(options.storageKey && !options.storage && typeof(Storage) !== 'undefined')
options.storage = localStorage;
if(options.storageKey && options.storage) {
const stored = options.storage.getItem(options.storageKey);
if(stored) {
try { Object.assign(this.store, JSON.parse(stored)); }
catch { }
if(options.storageKey && !options.storage && typeof(Storage) !== 'undefined') options.storage = localStorage;
if(options.storage) {
if(options.storage instanceof Collection) {
(async () => {
(await options.storage?.getAll()).forEach((v: any) => this.add(v));
})()
} else if(options.storageKey) {
const stored = options.storage?.getItem(options.storageKey);
if(stored != null) try { Object.assign(this.store, JSON.parse(stored)); } catch { }
}
}
return new Proxy(this, {
@ -57,9 +60,14 @@ export class Cache<K extends string | number | symbol, T> {
return <K>value[this.key];
}
private save() {
if(this.options.storageKey && this.options.storage)
this.options.storage.setItem(this.options.storageKey, JSON.stringify(this.store));
private save(key: K) {
if(this.options.storage) {
if(this.options.storage instanceof Collection) {
this.options.storage.put(key, this.store[key]);
} else if(this.options.storageKey) {
this.options.storage.setItem(this.options.storageKey, JSONSanitize(this.store));
}
}
}
/**
@ -111,7 +119,7 @@ export class Cache<K extends string | number | symbol, T> {
*/
delete(key: K): this {
delete this.store[key];
this.save();
this.save(key);
return this;
}
@ -177,10 +185,10 @@ export class Cache<K extends string | number | symbol, T> {
set(key: K, value: T, ttl = this.options.ttl): this {
if(this.options.expiryPolicy == 'keep') delete (<any>value)._expired;
this.store[key] = value;
this.save();
this.save(key);
if(ttl) setTimeout(() => {
this.expire(key);
this.save();
this.save(key);
}, (ttl || 0) * 1000);
return this;
}

60
src/database.ts Normal file
View File

@ -0,0 +1,60 @@
export class Collection<K extends IDBValidKey, T> {
private database: Promise<IDBDatabase>;
constructor(public readonly db: string, public readonly collection: string, public readonly version?: number, private setup?: (db: IDBDatabase, event: IDBVersionChangeEvent) => void) {
this.database = new Promise((resolve, reject) => {
const req = indexedDB.open(this.db, this.version);
req.onerror = () => reject(req.error);
req.onsuccess = () => resolve(req.result);
req.onupgradeneeded = (event) => {
const db = req.result;
if(!db.objectStoreNames.contains(collection)) db.createObjectStore(collection, {keyPath: undefined});
if (this.setup) this.setup(db, event as IDBVersionChangeEvent);
};
});
}
async tx<T>(collection: string, fn: ((store: IDBObjectStore) => IDBRequest), readonly?: boolean): Promise<T> {
const db = await this.database;
const tx = db.transaction(collection, readonly ? 'readonly' : 'readwrite');
const store = tx.objectStore(collection);
return new Promise((resolve, reject) => {
const request = fn(store);
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
});
}
add(value: T, key?: K): Promise<void> {
return this.tx(this.collection, store => store.add(value, key));
}
count(): Promise<number> {
return this.tx(this.collection, store => store.count(), true);
}
put(key: K, value: T): Promise<void> {
debugger;
return this.tx(this.collection, store => store.put(value, key));
}
getAll(): Promise<T[]> {
return this.tx(this.collection, store => store.getAll(), true);
}
getAllKeys(): Promise<K[]> {
return this.tx(this.collection, store => store.getAllKeys(), true);
}
get(key: K): Promise<T> {
return this.tx(this.collection, store => store.get(key), true);
}
delete(key: K): Promise<void> {
return this.tx(this.collection, store => store.delete(key));
}
clear(): Promise<void> {
return this.tx(this.collection, store => store.clear());
}
}

View File

@ -4,6 +4,7 @@ export * from './aset';
export * from './cache';
export * from './color';
export * from './csv';
export * from './database';
export * from './files';
export * from './emitter';
export * from './errors';

65
tests/indexed-db.spec.ts Normal file
View File

@ -0,0 +1,65 @@
import {Collection} from '../src';
import 'fake-indexeddb/auto';
describe('IndexedDb and Collection', () => {
const data = {id: 1, name: 'Alice', age: 30, email: '<EMAIL>'};
const data2 = {id: 2, name: 'Bob', age: 39, email: '<EMAIL>'};
let collection = new Collection<number, typeof data>('database', 'collection');
afterEach(() => collection.clear());
test('can set and get an item', async () => {
await collection.put(data.id, data);
const result = await collection.get(data.id);
expect(result).toEqual(data);
});
test('can remove an item', async () => {
await collection.put(data.id, data);
await collection.put(data2.id, data2);
await collection.delete(data.id);
const result = await collection.get(data.id);
const result2 = await collection.get(data2.id);
expect(result).toBeUndefined();
expect(result2).toEqual(data2);
});
test('can clear all items', async () => {
await collection.put(data.id, data);
await collection.put(data2.id, data2);
await collection.clear();
const result = await collection.get(data.id);
const result2 = await collection.get(data2.id);
expect(result).toBeUndefined();
expect(result2).toBeUndefined();
});
test('getItem on missing key returns undefined', async () => {
const result = await collection.get(-1);
expect(result).toBeUndefined();
});
test('count returns number of items', async () => {
expect(await collection.count()).toBe(0);
await collection.put(data.id, data);
expect(await collection.count()).toBe(1);
await collection.delete(data.id);
expect(await collection.count()).toBe(0);
});
test('can get all items', async () => {
await collection.put(data.id, data);
await collection.put(data2.id, data2);
const allItems = await collection.getAll();
expect(allItems).toEqual(expect.arrayContaining([data, data2]));
expect(allItems.length).toBe(2);
});
test('can get all keys', async () => {
await collection.put(data.id, data);
await collection.put(data2.id, data2);
const keys = await collection.getAllKeys();
expect(keys).toEqual(expect.arrayContaining([data.id, data2.id]));
expect(keys.length).toBe(2);
});
});