61 lines
1.8 KiB
JavaScript
61 lines
1.8 KiB
JavaScript
// ============================================================
|
||
// 💾 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);
|
||
});
|
||
} |