Replacing Web Storage
We will now replace our Web Storage implementation with new versions of load
and save
in our Order.js
file. Also, we are creating a new loadDB
method, as in the following code:
openDB: async () => {
return await idb.openDB("cm-storage", 1, {
async upgrade(db) {
await db.createObjectStore("order");
}
})
}
load: async () => {
const db = await Order.openDB();
const cartString = await db.get("order", "cart");
if (cartString) {
try {
Order.cart = JSON.parse(cartString);
} catch (e) {
console.error("Data in Storage is corrupted");
}
}
Order.render();
}
save: async () => {
const db = await Order.openDB();
await db.put("order", JSON.stringify(Order.cart), "cart")
}