From fbb11c86ccc8410d3f569ea630f0faf68746e14c Mon Sep 17 00:00:00 2001 From: Jack Case Date: Thu, 11 Dec 2025 16:11:11 +0000 Subject: [PATCH] need to close DB before deleting and this was a pain. --- spec/indexeddb_spec.js | 30 ++++++++++++++++-- src/indexed-db.ts | 71 ++++++++++++------------------------------ 2 files changed, 47 insertions(+), 54 deletions(-) diff --git a/spec/indexeddb_spec.js b/spec/indexeddb_spec.js index b34fac5..6bf0274 100644 --- a/spec/indexeddb_spec.js +++ b/spec/indexeddb_spec.js @@ -1,5 +1,5 @@ import { SlopDB } from "../scripts/indexed-db.js" -import { deleteDB } from "../scripts/idb/index.js" +import { openDB, deleteDB } from "../scripts/idb/index.js" describe("sanity check", () => { it("works", () => { @@ -7,15 +7,39 @@ describe("sanity check", () => { }) }) -describe("SlopDB Version 1", () => { +describe("SlopDB", () => { + + let db beforeEach(async () => { await deleteDB("SlopDB") }) + afterEach(() => { + db.close() + }) + it("creates a version 1 indexeddb", async () => { const slopdb_v1 = new SlopDB(1) + await slopdb_v1.db_opened() + db = slopdb_v1.db + const object_stores = slopdb_v1.db.objectStoreNames + expect(object_stores).toContain("slop") + expect(object_stores).not.toContain("checkcache") + // slopdb_v1.db.close() }) -}) \ No newline at end of file + + it("creates a version 2 indexeddb", async () => { + const slopdb_v2 = new SlopDB(2) + await slopdb_v2.db_opened() + db = slopdb_v2.db + + const object_stores = slopdb_v2.db.objectStoreNames + expect(object_stores).toContain("slop") + expect(object_stores).toContain("checkcache") + + // slopdb_v2.db.close() + }) +}) diff --git a/src/indexed-db.ts b/src/indexed-db.ts index bd04837..4b32fdf 100644 --- a/src/indexed-db.ts +++ b/src/indexed-db.ts @@ -40,22 +40,7 @@ export class SlopDB { open_promise: Promise db: IDBPDatabase - constructor(idb_version: number) { - this.version = idb_version - this.open_promise = openDB("SlopDB", idb_version, { - upgrade(db, oldVersion, newVersion, transaction, event) { - for (let version = oldVersion + 1; version <= newVersion; version++) { - this.apply_db_upgrade(db, version) - } - } - }) - } - - async db_opened() { - this.db = await this.open_promise - } - - apply_db_upgrade(db: IDBDatabase, idb_version: number) { + static apply_db_upgrade(db: IDBPDatabase, idb_version: number) { switch (idb_version) { case 1: db.createObjectStore("slop", { keyPath: "domain" }) @@ -66,43 +51,27 @@ export class SlopDB { } } - // async open_database(idb_version: number): Promise { - // const db_promise = new Promise((resolve, reject) => { - // const db_request = window.indexedDB.open("SlopDB", idb_version) + constructor(idb_version: number) { + this.version = idb_version + this.open_promise = openDB("SlopDB", idb_version, { + upgrade(db, oldVersion, newVersion, transaction, event) { + for (let version = oldVersion + 1; version <= newVersion; version++) { + SlopDB.apply_db_upgrade(db, version) + } + }, + blocked(curVer, bloVer, event) { + console.error("IDB Open blocked on " + curVer + " blocking ver " + bloVer) + }, + blocking(curVer, bloVer, event) { + console.error("IDB Open blocking " + curVer + " blocked ver " + bloVer) + } + }) + } - // // success and upgradeneeded will both fire, so this doesn't work right - - // db_request.onerror = (_event) => { - // reject(db_request.error) - // } + async db_opened() { + this.db = await this.open_promise + } - // db_request.onsuccess = (_event) => { - // console.log("success") - // resolve(db_request.result) - // } - - // db_request.onupgradeneeded = (upgrade_event) => { - // console.log("upgradeneeded") - // const oldVersion = upgrade_event.oldVersion - // const newVersion = upgrade_event.newVersion - - // const db = db_request.result - - // // make updates - // for (let version = oldVersion + 1; version <= newVersion; version++) { - // this.apply_db_upgrade(db, version) - // } - - // resolve(db) - // } - // }) - - // return db_promise - // } - - // start_transaction(storeNames: string | Array, mode: IDBTransactionMode, options: IDBTransactionOptions = undefined): IDBTransaction { - // return this.db.transaction(storeNames, mode, options) - // } }