public/js/idb.js
//COLLECT DATA from IndexedDB and POST to SERVER
COLLECT DATA from IndexedDB and POST to SERVER
COLLECT DATA from IndexedDB and POST to SERVER
function uploadBudget() {
  const transaction = db.transaction(["new_budget"], "readwrite");
  const budgetObjectStore = transaction.objectStore("new_budget");
  const getAll = budgetObjectStore.getAll();
  getAll.onsuccess = function () {
    if (getAll.result.length > 0) {
      fetch("/api/transaction/bulk", {
        method: "POST",
        body: JSON.stringify(getAll.result),
        headers: {
          Accept: "application/json, text/plain, */*",
          "Content-Type": "application/json",
        },
      })
        .then((response) => response.json())
        .then((serverResponse) => {
          if (serverResponse.message) {
            throw new Error(serverResponse);
          }
          const transaction = db.transaction(["new_budget"], "readwrite");
          const budgetObjectStore = transaction.objectStore("new_budget");
          budgetObjectStore.clear();
          alert("All saved budget has been submitted!");
        })
        .catch((err) => {
          console.log(err);
        });
    }
  };
}
function uploadBudget ( ) { const transaction = db . transaction ( [ "new_budget" ] , "readwrite" ) ; const budgetObjectStore = transaction . objectStore ( "new_budget" ) ; const getAll = budgetObjectStore . getAll ( ) ; getAll . onsuccess = function ( ) { if ( getAll . result . length > 0 ) { fetch ( "/api/transaction/bulk" , { method : "POST" , body : JSON . stringify ( getAll . result ) , headers : { Accept : "application/json, text/plain, */*" , "Content-Type" : "application/json" , } , } ) . then ( ( response ) => response . json ( ) ) . then ( ( serverResponse ) => { if ( serverResponse . message ) { throw new Error ( serverResponse ) ; } const transaction = db . transaction ( [ "new_budget" ] , "readwrite" ) ; const budgetObjectStore = transaction . objectStore ( "new_budget" ) ; budgetObjectStore . clear ( ) ; alert ( "All saved budget has been submitted!" ) ; } ) . catch ( ( err ) => { console . log ( err ) ; } ) ; } } ; }
COLLECT DATA from IndexedDB and POST to SERVER
COLLECT DATA from IndexedDB and POST to SERVER
// open a transaction on your db // access your object store // get all records from store and set to a variable // upon a successful .getAll() execution, run this function // if there was data in indexedDb's store, let's send it to the api server // open one more transaction // access the new_budget object store // clear all items in your store