[wip] idb doth not work
This commit is contained in:
parent
21126c5852
commit
0d683f1538
@ -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);
|
||||
const db = await new Promise((resolve, reject) => {
|
||||
req.onerror = () => {
|
||||
console.log("error", req.error);
|
||||
reject(req.error);
|
||||
};
|
||||
|
||||
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);
|
||||
resolve(req.result);
|
||||
};
|
||||
});
|
||||
this.#isInternalConstructing = true;
|
||||
return new Gesundheit(db);
|
||||
}
|
||||
}
|
||||
|
@ -152,4 +152,6 @@ addEventListener("keydown", (ev) => {
|
||||
|
||||
toggleAssetEditorButton.addEventListener("click", () => assetEditor.toggleEditor());
|
||||
|
||||
Gesundheit.load("assetdb");
|
||||
const gsh = await Gesundheit.load("assetdb");
|
||||
|
||||
await gsh.doSomethingOrOtter();
|
||||
|
Loading…
x
Reference in New Issue
Block a user