IndexedDB:
storing data locally on browsers with HTML5

Tomomi Imura, @girlie_mac

IndexedDB

Storing data on browsers with HTML5

a cat in a bucket

http://photozou.jp/photo/show/1033469/126297401 by je5xkdbnd

Hello

my name is

Tomomi

@girlie_mac

Storing Data with HTML5

  • Cookie
  • DOM Storage
    • Local Storage
    • Session Storage
  • IndexedDB
  • WebSQL (Deprecated)

Cookies

  • name/value pair
  • 4k limit per cookie
  • Sent with the HTTP header
  • Old-School but cross-browser

http://www.flickr.com/photos/melissacorey/4210541084/ by Melissa Corey bna

HTML5 DOM Storage

  • name/value pair
  • 2.5MB - 10MB per origin
  • Read in client - JavaScript
  • Not supported on IE6, 7, and Opera 10.

Local Storage

Session Storage

WebSQL

Deprecated by W3C

  • Relational DB on client - SQLite syntax
  • Converting JS obj into relational schema is PITA
  • Still supported on Safari, Chrome, Opera, and Blackberry browsers

IndexedDB

  • W3C Standard (w3.org/TR/IndexedDB)
  • NoSQL
  • fast indexing and searching of objects
  • JS objects & keys (objectStore)
  • synchronous & asynchronous API

Quota Management API

		
navigator.temporaryStorage.queryUsageAndQuota(
    function(usage, quota){...}, 
    function(error){...}
  );

* webkitTemporaryStorage

w3.org/TR/quota-api

Browser Support

Basic Async iDB Support

11
15bl
4
10

Mobile Browser Support

Basic Async iDB Support

18
14wk
15bl
10
16
10

Feature Detection

var indexedDB = window.indexedDB || 
                window.webkitIndexedDB ||
                window.mozIndexedDB;
if(indexedDB) {
   // Yay, iDB is supported!
} 
		

• Chrome < 23 & Blackberry require the webkit prefix.
• Firefox < 16 requires the moz prefix.
• Pre-released ver IE10 requires ms, but not production IE10+.

Demo: Grocery List

Try the demo on: http://goo.gl/zLG83c

Creating A Simple Grocery List

  1. Open an iDB
  2. Retrieve data
  3. Display the data

User Interaction

Opening a DB


if(indexedDB) {
   var req = indexedDB.open('myDatabase'); 
   
   // async callbacks
   req.onsuccess = function(e) {
      ...
   };
   ...
} 
		
		

Latest spec (FF, IE10, Chrome >= 23)

...
req.onsuccess = function(e) {
   // retrieve data from DB
};
req.onerror = function(e) {console.log(e);};

req.onupgradeneeded = function(e) {
   e.target.result.createObjectStore('shop', {
      keyPath: 'id', autoIncrement: true
   });
};
		
		

Retrieve All Data

var tx = db.transaction('shop', 'readonly');
var objStore = tx.objectStore('shop');
var cursorReq = objStore.openCursor();  
var arr = [];
cursorReq.onsuccess = function(e) {
   var cursor = e.target.result;
   if(cursor) {
      arr.push(cursor.value);
      cursor.continue();
   } else {
      // Display the data in DOM
   }
}
		
		

Add Data

var input = document.getElementsByTagName('input')[0];
var data = {
   item: input.value,
   created: new Date()
};
var tx = db.transaction('shop', 'readwrite');	    
var objStore = tx.objectStore('shop');      
var req = objStore.put(data);
               
req.onsuccess = function(e) {
   // Display the data
};
		

Delete Data

var tx = db.transaction('shop', 'readwrite');
var objStore = tx.objectStore('shop');
var req = objStore.delete(key);
      
req.onsuccess = function(e) {
   // Display the remaining data
};
		

Chrome DevTools

You can inspect:

  • A List of Database
  • Properties of a DB
  • DB's Object stores

Try on mobile: http://goo.gl/MX7fe

Storing Blob

e.g. Convert a canvas data to blob, then store it in iDB

canvas.toBlob(function(blob) {
   data = {
        name: 'cat',
        photo: blob
    };
   var tx = db.transaction('gallery', 'readwrite');	    
   var objStore = tx.objectStore('gallery');      
   var req = objStore.put(data);
   ...
}, 'image/jpeg');
		

Blob URLs

Displaying Blob

<img src="blob:958c1b50-09a0-2543-8528-8697ae387667">

var URL = window.URL || window.webkitURL*;
var imgURL = URL.createObjectURL(blob);
		

So, can we use it now?

So, can we use it now?

Meh: Spotty Browser Support

Polyfill

Prolly, you need to polyfill with Web SQL for now...

Indexed DB Web SQL
Chrome 31 Yes Yes
Opera 17 Yes Yes
Blackberry 10 Yes Yes
Firefox 25 Yes No
IE 11 Yes No
Safari 7 No Yes
Android 4.2 No Yes
}

caniuse.com

Thank you!

Tomomi Imura

Slides: girliemac.github.io/presentation-slides/html5-indexedDB

Mandatory Kitteh

Taco cat spelt backwards is taco cat

http://www.flickr.com/photos/ilovepaisley/6943837126/ by ilovepaisleybd