javascript - How to work with IndexedDB asynchronously? -


loading data , store them in indexeddb database. periodically have database crashes , lost access it. give me, please, solution how use indexeddb asynchronously!

sample code i'm use now:

var datatotal = 0; var threads = 6;  //openindexeddbconnection();  function start(total){  datatotal = total;   (var = 0; < threads; i++) {     loaddata(i);   } }  function loaddata(datanum){   var datanext = datanum + threads;   if(datanext > datatotal){     //checkend();     return;   }    $.ajax({     url: baseurl,     data: {offset: datanum},     success: function (data) {       successdata(datanext, data);     },     type: 'get'   }); }  function successdata(datanext, data){   var dataarray = data.split(';');    savedata(dataarray);    loaddata(datanext); }  function savedata(dataarray){    putitem();   function putitem(i) {     var count = || 0;     if(dataarray.length <= i){       return;     }      var transaction = indexeddb.transaction([datatablename], "readwrite");     transaction.onsuccess = function (event) {       //continue       putitem(count);     };     var objectstore = transaction.objectstore(datatablename);      var request = objectstore.add({data: dataarray[count++]});   } } 

you can use promises load , save data asynchronously indexeddb. here 2 example functions load , save data simple objectstore in indexeddb.

asynchronous loading indexeddb:

function loadfromindexeddb(storename, id){    return new promise(      function(resolve, reject) {        var dbrequest = indexeddb.open(storename);          dbrequest.onerror = function(event) {          reject(error("error text"));        };          dbrequest.onupgradeneeded = function(event) {          // objectstore not exist. nothing load          event.target.transaction.abort();          reject(error('not found'));        };          dbrequest.onsuccess = function(event) {          var database      = event.target.result;          var transaction   = database.transaction([storename]);          var objectstore   = transaction.objectstore(storename);          var objectrequest = objectstore.get(id);            objectrequest.onerror = function(event) {            reject(error('error text'));          };            objectrequest.onsuccess = function(event) {            if (objectrequest.result) resolve(objectrequest.result);            else reject(error('object not found'));          };        };      }    );  }

asynchronous saving indexeddb:

function savetoindexeddb(storename, object){    return new promise(      function(resolve, reject) {        if (object.id === undefined) reject(error('object has no id.'));        var dbrequest = indexeddb.open(storename);          dbrequest.onerror = function(event) {          reject(error("indexeddb database error"));        };          dbrequest.onupgradeneeded = function(event) {          var database    = event.target.result;          var objectstore = database.createobjectstore(storename, {keypath: "id"});        };          dbrequest.onsuccess = function(event) {          var database      = event.target.result;          var transaction   = database.transaction([storename], 'readwrite');          var objectstore   = transaction.objectstore(storename);          var objectrequest = objectstore.put(object); // overwrite if exists            objectrequest.onerror = function(event) {            reject(error('error text'));          };            objectrequest.onsuccess = function(event) {            resolve('data saved ok');          };        };      }    );  }

example usage code

var data = {'id' : 1, 'name' : 'bla'};    savetoindexeddb('objectstorename', data).then(function (response) {    alert('data saved');  }).catch(function (error) {    alert(error.message);  });    // load data  var id = 1;  loadfromindexeddb('objectstorename', id ).then(function (reponse) {    data = reponse;    alert('data loaded ok');  }).catch(function (error) {    alert(error.message);  });


Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -