The code comes from the Preach Intelligence podcast
1Angular.module (' Indexdb ', [])2. Factory (' Indexdbjs ', [' $ionicPopup ',function($ionicPopup) {3 4WINDOW.INDEXEDDB = WINDOW.INDEXEDDB | | WINDOW.MOZINDEXEDDB | | WINDOW.WEBKITINDEXEDDB | |Window.msindexeddb;5Window. idbtransaction = window. IDbTransaction | | window.webkitidbtransaction | |window.msidbtransaction;6Window. Idbkeyrange = window. Idbkeyrange | | Window.webkitidbkeyrange | |Window.msidbkeyrange;7Window. Idbcursor=window. idbcursor| | window.webkitidbcursor| |Window.msidbcursor;8 vardb={9DbName: ' Aptdb ',Tendbversion:2046,//will be rounded with decimals One dbinstance: {}, A -ErrorHandler:function(Error) { -Console.log (' ERROR: ' +error.target.error.message); the }, - -Openfunction(func,fail) { - varDbcontent =Window.indexedDB.open (Db.dbname, db.dbversion); +dbcontent.onupgradeneeded =Db.upgrade; -Dbcontent.onerror =Db.errorhandler; +Dbcontent.onsuccess =function(e) { ADb.dbinstance =Dbcontent.result; atDb.dbInstance.onerror =fail; - func (); - }; - }, -Upgradefunction(e) { - var_db = e.target.result, names =_db.objectstorenames; in //Here you can create multiple tables - varName = "Cart"; to if(!names.contains (name)) { + _db.createobjectstore ( - name, the { *KeyPath: ' Goodsid ', $AutoIncrement:falsePanax Notoginseng }); - } the }, +Getobjectstore:function(objectstorename,mode) { A varTXN, Store;mode = Mode | | ' ReadOnly '; theTXN =db.dbInstance.transaction ([objectstorename], mode); +store =Txn.objectstore (objectstorename); - returnstore; $ }, $Addfunction(objectstorename,data,success,fail) { -Db.open (function () { - varStore, req, mode = ' ReadWrite '; thestore =Db.getobjectstore (objectstorename,mode), -req =store.add (data);WuyiReq.onsuccess =success; theReq.onerror=fail; - },fail); Wu }, -Updatefunction(objectstorename,data,success,fail) { AboutDb.open (function () { $ varStore, req, mode = ' ReadWrite '; -store =Db.getobjectstore (objectstorename,mode), -req =store.put (data); -Req.onsuccess =success; AReq.onerror=fail; + },fail); the }, -GETALL:function(objectstorename,success,fail) { $ theDb.open (function () { the var thestore =Db.getobjectstore (objectstorename), thecursor =store.opencursor (), -data = []; in theCursor.onsuccess =function(e) { the varresult =E.target.result; About if(Result && result!==NULL) { the Data.push (result.value); theResult.Continue(); the}Else { + success (data); - } the };BayiCursor.onerror=fail; the the },fail); - }, -Getfunction(id,objectstorename,success,fail) { theDb.open (function () { the var thestore =Db.getobjectstore (objectstorename), thereq =store.get (ID); -Req.onsuccess =function(e) { the success (E.target.result); the }; theReq.onerror=fail;94 }); the }, the //Delete is a reserved word the' Delete ':function(id,objectstorename,success,fail) {98Db.open (function () { About var -mode = ' ReadWrite ',101 store, req;102store =Db.getobjectstore (objectstorename,mode);103req = store.Delete(ID);104Req.onsuccess =success; theReq.onerror=fail;106 });107 },108DELETEALL:function(objectstorename,success,fail) {109Db.open (function () { the varmode, store, req;111mode = ' ReadWrite '; thestore =Db.getobjectstore (objectstorename,mode);113req =store.clear (); theReq.onsuccess =success; theReq.onerror=fail; the });117 }118 };119 - returndb;121}]);View Code
INDEXEDDB general function encapsulation into angular service