Created
October 10, 2022 10:48
-
-
Save hwclass/067d6f112232e3fd80ca49f5c82c4f08 to your computer and use it in GitHub Desktop.
A boilerplate on how to deal with IndexedDB
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 request = indexedDB.open("FlightApp", 1); | |
request.onerror = function (event) { | |
console.error("An error occurred with IndexedDB"); | |
console.error(event); | |
}; | |
request.onupgradeneeded = function () { | |
const db = request.result; | |
const store = db.createObjectStore("flights", { keyPath: "id", autoIncrement:true }); | |
store.createIndex("nextFlight", ["nextFlight"]); | |
}; | |
request.onsuccess = function () { | |
console.log("Database opened successfully"); | |
const db = request.result; | |
const transaction = db.transaction("flights", "readwrite"); | |
const store = transaction.objectStore("flights"); | |
const nextFlightIndex = store.index("nextFlight"); | |
store.put({ flightDate: Date.now() }); | |
const query = store.get(1); | |
query.onsuccess = function () { | |
console.log("next flight", query.result); | |
}; | |
transaction.oncomplete = function () { | |
db.close(); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment