Skip to content

Instantly share code, notes, and snippets.

@hwclass
Created October 10, 2022 10:48
Show Gist options
  • Save hwclass/067d6f112232e3fd80ca49f5c82c4f08 to your computer and use it in GitHub Desktop.
Save hwclass/067d6f112232e3fd80ca49f5c82c4f08 to your computer and use it in GitHub Desktop.
A boilerplate on how to deal with IndexedDB
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