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.
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).