diff --git a/index.html b/index.html index 6d4f724..9eed009 100644 --- a/index.html +++ b/index.html @@ -3,27 +3,7 @@ diff --git a/package.json b/package.json index 7ba44db..254edbf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ztimson/utils", - "version": "0.25.3", + "version": "0.25.4", "description": "Utility library", "author": "Zak Timson", "license": "MIT", diff --git a/src/database.ts b/src/database.ts index 90020db..6ec9af7 100644 --- a/src/database.ts +++ b/src/database.ts @@ -1,3 +1,5 @@ +import {ASet} from './aset.ts'; + export type TableOptions = { name: string; key?: string; @@ -9,12 +11,13 @@ export class Database { constructor(public readonly database: string, public readonly tables: (string | TableOptions)[], public version?: number) { this.connection = new Promise((resolve, reject) => { const req = indexedDB.open(this.database, this.version); + const tableNames = new ASet(tables.map(t => (typeof t == 'object' ? t.name : t).toString())); req.onerror = () => reject(req.error); req.onsuccess = () => { const db = req.result; - if(tables.find(s => !db.objectStoreNames.contains(typeof s === 'string' ? s : s.name))) { + if(tableNames.symmetricDifference(new ASet(Array.from(db.objectStoreNames))).length) { db.close(); Object.assign(this, new Database(this.database, this.tables, db.version + 1)); } else { @@ -25,14 +28,11 @@ export class Database { req.onupgradeneeded = () => { const db = req.result; - Array.from(db.objectStoreNames) - .filter(s => !this.tables.find(t => typeof t === 'string' ? t : t.name == s)) - .forEach(name => db.deleteObjectStore(name)); - tables.filter(t => !db.objectStoreNames.contains(typeof t === 'string' ? t : t.name)) - .forEach(t => {db.createObjectStore(typeof t === 'string' ? t : t.name, { - keyPath: typeof t === 'string' ? undefined : t.key - }); - }); + const existingTables = new ASet(Array.from(db.objectStoreNames)); + console.log('delete', existingTables.difference(tableNames)); + existingTables.difference(tableNames).forEach(name => db.deleteObjectStore(name)); + console.log('create', tableNames.difference(existingTables)); + tableNames.difference(existingTables).forEach(name => db.createObjectStore(name)); }; }); } @@ -41,18 +41,18 @@ export class Database { return this.tables.some(t => (typeof t === 'string' ? name === t : name === t.name)); } - table(name: string): Table { - return new Table(this, name); + table(name: any): Table { + return new Table(this, name.toString()); } } export class Table { constructor(private readonly database: Database, public readonly name: string) {} - async tx(schema: string, fn: (store: IDBObjectStore) => IDBRequest, readonly = false): Promise { + async tx(table: string, fn: (store: IDBObjectStore) => IDBRequest, readonly = false): Promise { const db = await this.database.connection; - const tx = db.transaction(schema, readonly ? 'readonly' : 'readwrite'); - const store = tx.objectStore(schema); + const tx = db.transaction(table, readonly ? 'readonly' : 'readwrite'); + const store = tx.objectStore(table); return new Promise((resolve, reject) => { const request = fn(store); request.onsuccess = () => resolve(request.result as R); // ✅ explicit cast