From fccd49596de4910f2b665d8144aec4b59f5a58aa Mon Sep 17 00:00:00 2001 From: tim Date: Thu, 1 Oct 2026 05:58:48 +0000 Subject: [PATCH] =?UTF-8?q?db.js=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db.js | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 db.js diff --git a/db.js b/db.js new file mode 100644 index 0000000..50a0cf3 --- /dev/null +++ b/db.js @@ -0,0 +1,61 @@ +// ============================================================ +// 💾 IndexedDB-Schicht – speichert alle "Geschäfte" offline +// ============================================================ + +const DB_NAME = "hunde-business"; +const DB_VERSION = 1; +const STORE = "entries"; + +function openDB() { + return new Promise((resolve, reject) => { + const req = indexedDB.open(DB_NAME, DB_VERSION); + req.onupgradeneeded = (e) => { + const db = e.target.result; + if (!db.objectStoreNames.contains(STORE)) { + db.createObjectStore(STORE, { keyPath: "id" }); + } + }; + req.onsuccess = () => resolve(req.result); + req.onerror = () => reject(req.error); + }); +} + +async function dbAdd(entry) { + const db = await openDB(); + return new Promise((resolve, reject) => { + const tx = db.transaction(STORE, "readwrite"); + tx.objectStore(STORE).add(entry); + tx.oncomplete = () => resolve(entry); + tx.onerror = () => reject(tx.error); + }); +} + +async function dbGetAll() { + const db = await openDB(); + return new Promise((resolve, reject) => { + const tx = db.transaction(STORE, "readonly"); + const req = tx.objectStore(STORE).getAll(); + req.onsuccess = () => resolve(req.result); + req.onerror = () => reject(req.error); + }); +} + +async function dbDelete(id) { + const db = await openDB(); + return new Promise((resolve, reject) => { + const tx = db.transaction(STORE, "readwrite"); + tx.objectStore(STORE).delete(id); + tx.oncomplete = () => resolve(); + tx.onerror = () => reject(tx.error); + }); +} + +async function dbClearAll() { + const db = await openDB(); + return new Promise((resolve, reject) => { + const tx = db.transaction(STORE, "readwrite"); + tx.objectStore(STORE).clear(); + tx.oncomplete = () => resolve(); + tx.onerror = () => reject(tx.error); + }); +} \ No newline at end of file