From 0d683f15385c7add78559eec828bdd6a17fc316b Mon Sep 17 00:00:00 2001 From: Theis Pieter Hollebeek Date: Mon, 13 Oct 2025 19:11:32 +0200 Subject: [PATCH] [wip] idb doth not work --- src/gesundheit.js | 60 +++++++++++++++++++++++++++++++++++------------ src/index.js | 4 +++- 2 files changed, 48 insertions(+), 16 deletions(-) diff --git a/src/gesundheit.js b/src/gesundheit.js index cda32f2..f06a9db 100644 --- a/src/gesundheit.js +++ b/src/gesundheit.js @@ -2,32 +2,62 @@ export class Gesundheit { static #isInternalConstructing = false; static #idb = globalThis.indexedDB; - constructor() { + constructor(db) { if (!Gesundheit.#isInternalConstructing) { throw new TypeError("Gesundheit is not constructable - use Gesundheit.load()"); } 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); const req = this.#idb.open(name); - req.onerror = () => { - console.log("error", req.error); - }; + const db = await new Promise((resolve, reject) => { + req.onerror = () => { + reject(req.error); + }; - req.onupgradeneeded = () => { - const db = req.result; + req.onupgradeneeded = () => { + const db = req.result; - const objectStore = db.createObjectStore("asset", { - keyPath: "filename", - }); + const assetStore = db.createObjectStore("asset", { + keyPath: "name", + }); - console.log("upgrade pls", db); - }; + assetStore.createIndex("mime", "mime", { unique: false }); + assetStore.createIndex("bytes", "bytes", { unique: false }); + }; - req.onsuccess = () => { - console.log("success", req.result); - }; + req.onsuccess = () => { + resolve(req.result); + }; + }); + this.#isInternalConstructing = true; + return new Gesundheit(db); } } diff --git a/src/index.js b/src/index.js index 71aced0..d2bee60 100644 --- a/src/index.js +++ b/src/index.js @@ -152,4 +152,6 @@ addEventListener("keydown", (ev) => { toggleAssetEditorButton.addEventListener("click", () => assetEditor.toggleEditor()); -Gesundheit.load("assetdb"); +const gsh = await Gesundheit.load("assetdb"); + +await gsh.doSomethingOrOtter();