[wip] idb doth not work

This commit is contained in:
Theis Pieter Hollebeek 2025-10-13 19:11:32 +02:00
parent 21126c5852
commit 0d683f1538
2 changed files with 48 additions and 16 deletions

View File

@ -2,32 +2,62 @@ export class Gesundheit {
static #isInternalConstructing = false; static #isInternalConstructing = false;
static #idb = globalThis.indexedDB; static #idb = globalThis.indexedDB;
constructor() { constructor(db) {
if (!Gesundheit.#isInternalConstructing) { if (!Gesundheit.#isInternalConstructing) {
throw new TypeError("Gesundheit is not constructable - use Gesundheit.load()"); throw new TypeError("Gesundheit is not constructable - use Gesundheit.load()");
} }
Gesundheit.#isInternalConstructing = false; Gesundheit.#isInternalConstructing = false;
this.db = db;
} }
static load(name) { async doSomethingOrOtter() {
console.log(this.db.objectStoreNames);
const transaction = this.db.transaction(["asset"], "readwrite");
return await new Promise((resolve, reject) => {
transaction.oncomplete = () => {
resolve();
};
transaction.onerror = () => {
reject();
};
const assetStore = transaction.objectStore("asset");
const req = assetStore.add({
name: "idk",
mime: "idk",
bytes: [],
}, "asset");
});
}
static async load(name) {
/* remove when not developing db */
this.#idb.deleteDatabase(name); this.#idb.deleteDatabase(name);
const req = this.#idb.open(name); const req = this.#idb.open(name);
const db = await new Promise((resolve, reject) => {
req.onerror = () => { req.onerror = () => {
console.log("error", req.error); reject(req.error);
}; };
req.onupgradeneeded = () => { req.onupgradeneeded = () => {
const db = req.result; const db = req.result;
const objectStore = db.createObjectStore("asset", { const assetStore = db.createObjectStore("asset", {
keyPath: "filename", keyPath: "name",
}); });
console.log("upgrade pls", db); assetStore.createIndex("mime", "mime", { unique: false });
assetStore.createIndex("bytes", "bytes", { unique: false });
}; };
req.onsuccess = () => { req.onsuccess = () => {
console.log("success", req.result); resolve(req.result);
}; };
});
this.#isInternalConstructing = true;
return new Gesundheit(db);
} }
} }

View File

@ -152,4 +152,6 @@ addEventListener("keydown", (ev) => {
toggleAssetEditorButton.addEventListener("click", () => assetEditor.toggleEditor()); toggleAssetEditorButton.addEventListener("click", () => assetEditor.toggleEditor());
Gesundheit.load("assetdb"); const gsh = await Gesundheit.load("assetdb");
await gsh.doSomethingOrOtter();