This article is more than 1 year old

IndexedDB pulls away from less-loved web storage options

IndexedDB API 2.0 spec has reached draft form. Contain yourself, and your data

The Indexed Database API 2.0 has arrived in draft form, promising a variety of improvements in the way developers of web applications can store data in browsers.

IndexedDB is a JavaScript-based object-oriented database within modern browsers. It began taking shape in 2011 and, after years in draft form, it met with W3C approval last year.

It's particularly useful for web applications that need to store large amounts of data to function offline, and for Progressive Web Apps. Now there's a second version under consideration.

IndexedDB is one of several client-side storage options for web apps. Other options include:

  • Cookies, which are limited in size and data type, and don't support recent APIs like Web Workers.
  • DOM Storage, which has similar limitations.
  • WebSQL, a one-time challenger that has been deprecated.
  • AppCache, characterized as a "douchebag" for being difficult to use.
  • The Cache API, intended for URL-addressable resources and not available in all browsers.
  • The File API, which isn't yet fully baked.
  • The Chrome-only FileSystem API.

Unlike Cookies and DOM Storage, IndexedDB works asynchronously, so its operations do not block other operations while active. Although broad support for IndexedDB has been slow in coming – Apple just recently implemented full support in Safari 10 – it's now a worthy choice for storing significant amounts of structured data in the browser.

In a blog post on Tuesday, Bevis Tseng, a platform engineer at Mozilla, highlighted some of the new capabilities coming to IndexedDB in Firefox 51, available for developers now and scheduled for the general public in January 2017.

The IndexedDB API 2.0 offers a setter to rename existing object stores and indexes, via IDBObjectStore.name and IDBIndex.name.

It also provides a way to listen for storage change events through the IDBDatabase.onclose() event handler.

The updated API also adds support for binary data types as index keys. "Now you can have binary signatures as keys directly, without serializing them into strings or array objects as required by the previous API version," explained Tseng.

And a variety of other methods have been added that improve how database keys can be accessed, such as IDBObjectStore.getKey(query), IDBObjectStore.openKeyCursor(range, direction),and getAll/getAllKeys(range, count).

IndexedDB doesn't yet support JavaScript Promises – a way to deal with events that may succeed or fail while running asynchronous code – but there's a proposal and there are libraries that provide support. For those who have to store client-side data in web apps, IndexedDB is worth a look. ®

More about

TIP US OFF

Send us news


Other stories you might like