Created
March 7, 2024 17:20
-
-
Save andrei0x309/9af01e74e26b7cd10686e9ddec1ecfa6 to your computer and use it in GitHub Desktop.
Add something to index DB, Browser don't allow direct modification of index DB from dev tools so you need to run a script like this.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const addToIndexedDB = async (dbName, version, storeName, keyPath, key, value) => { | |
try { | |
// Open a connection to the database | |
const dbRequest = indexedDB.open(dbName, version); | |
dbRequest.onupgradeneeded = (event) => { | |
const db = event.target.result; | |
// Create an object store if it doesn't exist | |
db.createObjectStore(storeName, { keyPath }); | |
}; | |
// Handle success and failure | |
dbRequest.onsuccess = (event) => { | |
console.log("Data added successfully:", event.target.result); | |
const db = dbRequest.result | |
const transaction = db.transaction(storeName, "readwrite"); | |
const store = transaction.objectStore(storeName); | |
// Add the key-value pair to the object store | |
const request = store.put( value, key); | |
}; | |
dbRequest.onerror = (event) => { | |
console.error("Error adding data:", event.target.error); | |
}; | |
await transaction.complete; // Ensure transaction completion | |
} catch (error) { | |
console.error("Error accessing IndexedDB:", error); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment