Header mit indexedDB
This commit is contained in:
@@ -1,33 +1,40 @@
|
||||
import { openDB } from "idb"; // utils/indexedDB.js
|
||||
|
||||
const dbPromise = openDB("my-pdf-store", 2, {
|
||||
upgrade(db) {
|
||||
// Überprüfe und erstelle den Object Store für PDFs
|
||||
if (!db.objectStoreNames.contains("pdfs")) {
|
||||
db.createObjectStore("pdfs");
|
||||
}
|
||||
// Überprüfe und erstelle den Object Store für Seiten (pages)
|
||||
if (!db.objectStoreNames.contains("pages")) {
|
||||
db.createObjectStore("pages"); // Korrekte Erstellung des "pages" Object Stores
|
||||
}
|
||||
},
|
||||
});
|
||||
// Überprüfe, ob `window` verfügbar ist, um zu bestimmen, ob der Code im Browser läuft
|
||||
const dbPromise =
|
||||
typeof window !== "undefined"
|
||||
? openDB("my-pdf-store", 2, {
|
||||
upgrade(db) {
|
||||
if (!db.objectStoreNames.contains("pdfs")) {
|
||||
db.createObjectStore("pdfs");
|
||||
}
|
||||
if (!db.objectStoreNames.contains("pages")) {
|
||||
db.createObjectStore("pages");
|
||||
}
|
||||
if (!db.objectStoreNames.contains("cplVariables")) {
|
||||
db.createObjectStore("cplVariables");
|
||||
}
|
||||
},
|
||||
})
|
||||
: null;
|
||||
|
||||
// Store PDF
|
||||
export async function storePDF(name, file) {
|
||||
if (!dbPromise) return;
|
||||
const db = await dbPromise;
|
||||
await db.put("pdfs", file, name);
|
||||
}
|
||||
|
||||
export async function getPDF(name) {
|
||||
if (!dbPromise) return null;
|
||||
const db = await dbPromise;
|
||||
return await db.get("pdfs", name);
|
||||
}
|
||||
|
||||
// Store page
|
||||
export async function storePage(name, file) {
|
||||
if (!dbPromise) return;
|
||||
const db = await dbPromise;
|
||||
// Überprüfe, ob der Object Store "pages" existiert
|
||||
const transaction = db.transaction("pages", "readwrite");
|
||||
const store = transaction.objectStore("pages");
|
||||
await store.put(file, name);
|
||||
@@ -35,6 +42,24 @@ export async function storePage(name, file) {
|
||||
}
|
||||
|
||||
export async function getPage(name) {
|
||||
if (!dbPromise) return null;
|
||||
const db = await dbPromise;
|
||||
return await db.get("pages", name);
|
||||
}
|
||||
|
||||
// Store and retrieve data for CPL variables
|
||||
|
||||
export async function storeCPLVariable(key, value) {
|
||||
if (!dbPromise) return;
|
||||
const db = await dbPromise;
|
||||
const transaction = db.transaction("cplVariables", "readwrite");
|
||||
const store = transaction.objectStore("cplVariables");
|
||||
await store.put(value, key);
|
||||
await transaction.done;
|
||||
}
|
||||
|
||||
export async function getFromIndexedDB(key) {
|
||||
if (!dbPromise) return null;
|
||||
const db = await dbPromise;
|
||||
return await db.get("cplVariables", key);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user