need to close DB before deleting and this was a pain.
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
import { SlopDB } from "../scripts/indexed-db.js"
|
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", () => {
|
describe("sanity check", () => {
|
||||||
it("works", () => {
|
it("works", () => {
|
||||||
@@ -7,15 +7,39 @@ describe("sanity check", () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("SlopDB Version 1", () => {
|
describe("SlopDB", () => {
|
||||||
|
|
||||||
|
let db
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
await deleteDB("SlopDB")
|
await deleteDB("SlopDB")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
db.close()
|
||||||
|
})
|
||||||
|
|
||||||
it("creates a version 1 indexeddb", async () => {
|
it("creates a version 1 indexeddb", async () => {
|
||||||
const slopdb_v1 = new SlopDB(1)
|
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()
|
||||||
})
|
})
|
||||||
})
|
|
||||||
|
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()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|||||||
@@ -40,22 +40,7 @@ export class SlopDB {
|
|||||||
open_promise: Promise<IDBPDatabase>
|
open_promise: Promise<IDBPDatabase>
|
||||||
db: IDBPDatabase
|
db: IDBPDatabase
|
||||||
|
|
||||||
constructor(idb_version: number) {
|
static apply_db_upgrade(db: IDBPDatabase, 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) {
|
|
||||||
switch (idb_version) {
|
switch (idb_version) {
|
||||||
case 1:
|
case 1:
|
||||||
db.createObjectStore("slop", { keyPath: "domain" })
|
db.createObjectStore("slop", { keyPath: "domain" })
|
||||||
@@ -66,43 +51,27 @@ export class SlopDB {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// async open_database(idb_version: number): Promise<IDBDatabase> {
|
constructor(idb_version: number) {
|
||||||
// const db_promise = new Promise<IDBDatabase>((resolve, reject) => {
|
this.version = idb_version
|
||||||
// const db_request = window.indexedDB.open("SlopDB", 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
|
async db_opened() {
|
||||||
|
this.db = await this.open_promise
|
||||||
// db_request.onerror = (_event) => {
|
}
|
||||||
// reject(db_request.error)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// 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<string>, mode: IDBTransactionMode, options: IDBTransactionOptions = undefined): IDBTransaction {
|
|
||||||
// return this.db.transaction(storeNames, mode, options)
|
|
||||||
// }
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user