You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importidbfrom'idb';vardbPromise=idb.open('test-db',1,function(upgradeDb){varkeyValStore=upgradeDb.createObjectStore('keyval');keyValStore.put("world","hello");});// read "hello" in "keyval"dbPromise.then(function(db){vartx=db.transaction('keyval');varkeyValStore=tx.objectStore('keyval');returnkeyValStore.get('hello');}).then(function(val){console.log('The value of "hello" is:',val);});// set "foo" to be "bar" in "keyval"dbPromise.then(function(db){vartx=db.transaction('keyval','readwrite');varkeyValStore=tx.objectStore('keyval');keyValStore.put('bar','foo');returntx.complete;}).then(function(){console.log('Added foo:bar to keyval');});
Creating index in IDB
vardbPromise=idb.open('test-db',3,function(upgradeDb){switch(upgradeDb.oldVersion){case0:
varkeyValStore=upgradeDb.createObjectStore('keyval');keyValStore.put("world","hello");case1:
upgradeDb.createObjectStore('people',{keyPath: 'name'});case2:
varpeopleStore=upgradeDb.transaction.objectStore('people');peopleStore.createIndex('animal','favoriteAnimal');case3:
varpeopleStore=upgradeDb.transaction.objectStore('people');peopleStore.createIndex('age','age');}});// list all cat peopledbPromise.then(function(db){vartx=db.transaction('people');varpeopleStore=tx.objectStore('people');varanimalIndex=peopleStore.index('animal');returnanimalIndex.getAll('cat');}).then(function(people){console.log('Cat people:',people);});// list all people ordered by agedbPromise.then(function(db){vartx=db.transaction('people');varpeopleStore=tx.objectStore('people');varageIndex=peopleStore.index('age');returnageIndex.getAll();}).then(function(people){console.log('People sorted by age:',people);});
Using cursors
dbPromise.then(function(db){vartx=db.transaction('people');varpeopleStore=tx.objectStore('people');varageIndex=peopleStore.index('age');returnageIndex.openCursor();}).then(function(cursor){if(!cursor)return;returncursor.advance(2);}).then(functionlogPerson(cursor){if(!cursor)return;console.log("Cursored at:",cursor.value.name);// I could also do things like:// cursor.update(newValue) to change the value, or// cursor.delete() to delete this entryreturncursor.continue().then(logPerson);}).then(function(){console.log('Done cursoring');});
Example of database with defined key and index
functionopenDatabase(){// If the browser doesn't support service worker,// we don't care about having a databaseif(!navigator.serviceWorker){returnPromise.resolve();}returnidb.open('wittr',1,function(upgradeDb){varstore=upgradeDb.createObjectStore('wittrs',{keyPath: 'id'});store.createIndex('by-date','time');});}
Update database when the web socket sends message data
IndexController.prototype._showCachedMessages=function(){varindexController=this;returnthis._dbPromise.then(function(db){// if we're already showing posts, eg shift-refresh// or the very first load, there's no point fetching// posts from IDBif(!db||indexController._postsView.showingPosts())return;varindex=db.transaction('wittrs').objectStore('wittrs').index('by-date');returnindex.getAll().then(function(messages){indexController._postsView.addPosts(messages.reverse());});});};
Clean up IndexedDB
IndexController.prototype._onSocketMessage=function(data){varmessages=JSON.parse(data);this._dbPromise.then(function(db){if(!db)return;vartx=db.transaction('wittrs','readwrite');varstore=tx.objectStore('wittrs');messages.forEach(function(message){store.put(message);});// limit store to 30 itemsstore.index('by-date').openCursor(null,"prev").then(function(cursor){returncursor.advance(30);}).then(functiondeleteRest(cursor){if(!cursor)return;cursor.delete();returncursor.continue().then(deleteRest);});});this._postsView.addPosts(messages);};
Get image from cache - if there is no image, fetch it from networkResponse