|
|
|
@ -1,3 +1,5 @@
|
|
|
|
|
import {ASet} from './aset.ts';
|
|
|
|
|
|
|
|
|
|
export type TableOptions = {
|
|
|
|
|
name: string;
|
|
|
|
|
key?: string;
|
|
|
|
@ -5,16 +7,19 @@ export type TableOptions = {
|
|
|
|
|
|
|
|
|
|
export class Database {
|
|
|
|
|
connection!: Promise<IDBDatabase>;
|
|
|
|
|
tables!: TableOptions[];
|
|
|
|
|
|
|
|
|
|
constructor(public readonly database: string, public readonly tables: (string | TableOptions)[], public version?: number) {
|
|
|
|
|
constructor(public readonly database: string, tables: (string | TableOptions)[], public version?: number) {
|
|
|
|
|
this.connection = new Promise((resolve, reject) => {
|
|
|
|
|
const req = indexedDB.open(this.database, this.version);
|
|
|
|
|
this.tables = tables.map(t => typeof t == 'object' ? t : {name: t});
|
|
|
|
|
const tableNames = new ASet(this.tables.map(t => t.name));
|
|
|
|
|
|
|
|
|
|
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,34 +30,29 @@ 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));
|
|
|
|
|
existingTables.difference(tableNames).forEach(name => db.deleteObjectStore(name));
|
|
|
|
|
tableNames.difference(existingTables).forEach(name => db.createObjectStore(name));
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
includes(name: string): boolean {
|
|
|
|
|
return this.tables.some(t => (typeof t === 'string' ? name === t : name === t.name));
|
|
|
|
|
includes(name: any): boolean {
|
|
|
|
|
return !!this.tables.find(t => t.name == name.toString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
table<K extends IDBValidKey = any, T = any>(name: string): Table<K, T> {
|
|
|
|
|
return new Table<K, T>(this, name);
|
|
|
|
|
table<K extends IDBValidKey = any, T = any>(name: any): Table<K, T> {
|
|
|
|
|
return new Table<K, T>(this, name.toString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class Table<K extends IDBValidKey = any, T = any> {
|
|
|
|
|
constructor(private readonly database: Database, public readonly name: string) {}
|
|
|
|
|
|
|
|
|
|
async tx<R>(schema: string, fn: (store: IDBObjectStore) => IDBRequest, readonly = false): Promise<R> {
|
|
|
|
|
async tx<R>(table: string, fn: (store: IDBObjectStore) => IDBRequest, readonly = false): Promise<R> {
|
|
|
|
|
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<R>((resolve, reject) => {
|
|
|
|
|
const request = fn(store);
|
|
|
|
|
request.onsuccess = () => resolve(request.result as R); // ✅ explicit cast
|
|
|
|
|