Use the library indexeddb-promised.
Create a database and Stroe:
Import IDB from ' IDB '; // Open (db_name, version, CB) var function (upgradedb) { var// CREATE TABLE //put (value, key) keyvalstore.put (' World ', ' Hello ') ;
Notice put () function take value frist then key.
Read the key in Stroe:
// read "Hello" in "Keyval"dbpromise.then (function (db) { var// Open a transaction var// read the store return// get value by Key}). Then (function (val) { console.log (' The value of "Hello" is: ', Val);});
Write value to store:
//set "foo" to Is "bar" in "Keyval"Dbpromise.then (function(db) {vartx = Db.transaction (' keyval ', ' readwrite ');//Ready for add key value varKeyvalstore = Tx.objectstore (' keyval ');//Get the storeKeyvalstore.put (' Bar ', ' foo ');//set the value, notice it May is success if any step in transaction fail. returnTx.complete;//Tx.complete is a promise}). Then (function() {Console.log (' Added Foo:bar to Keyval ');});
[PWA] 12. Intro to IndexedDB