Table of contents
IndexedDB is an API for client-side storage of significant amounts of structured data and for high performance searches on this data using indexes. While DOM Storage is useful for storing smaller amounts of data, it is less useful for storing larger amounts of structured data. IndexedDB provides a solution.
IndexedDB provides separate APIs for synchronous and asynchronous access. The synchronous API is intended to be used inside workers.
Asynchronous API
The asynchronous API methods return without blocking the calling thread. To get asynchronous access to a database, call open()
on the indexedDB
attribute of a window object. This method returns an IDBRequest object; asynchronous operations communicate to the calling application by firing events on IDBRequest objects.
IDBCursor
iterates over object stores and indexes.IDBDatabase
represents a connection to a database. It's the only way to get a transaction on the database.IDBFactory
provides access to a database.IDBEnvironment
provides access to a client-side database. It is implemented by window objects.IDBIndex
provides access to the metadata of an index.IDBObjectStore
represents an object store.IDBOpenDBRequest
represents a request to open a database.IDBRequest
provides access to results of asynchronous requests to databases and database objects. It is what you get when you call an asynchronous method.IDBTransaction
represents a transaction. You create a transaction on a database, specify the scope (such as which object stores you want to access), and determine the kind of access (read only or write) you want.IDBVersionChangeEvent
indicates that the version of the database has changed.
Early version of the specification also defined the now removed interface. They are still documented in case you need to update previously writtent code:
IDBVersionChangeRequest
represents a request to change the version of a database. The way to change the version of the database has since changed (by callingIDBFactory.open()
rather thanIDBDatabase.setVersion()
) and the interfaceIDBOpenDBRequest
now have the functionality of the removedIDBVersionChangeRequest
.
Synchronous API
To get synchronous access to a database, call open()
on the
Unimplemented
indexedDBSync
attribute of a worker object. This returns an IDBDatabaseSync
object, which enables you to create, open, and remove object stores and indexes, set the version of the database, and create transactions.
IDBCursorSync
iterates over object stores and indexes.IDBDatabaseSync
represents a connection to a database. It's the only way to get a transaction on the database.IDBEnvironmentSync
provides access to a client-side database. It is implemented by worker objects.IDBFactorySync
provides access to a database.IDBIndexSync
provides access to the metadata of an index.IDBObjectStoreSync
represents an object store.IDBTransactionSync
creates a transaction in the database.
Shared interfaces
The following interfaces are used by both the synchronous and the asynchronous APIs.
IDBDatabaseException
represents exception conditions that can be encountered while performing database operations.IDBKeyRange
defines a range of keys.
Example
A powerful example of what IndexedDB can be utilized for on the web is the example by Marco Castelluccio, winner of the IndexedDB Mozilla DevDerby. The winning demo was eLibri, a library and eBook reader application.
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Asynchronous API | 12 webkit | 4.0 (2.0) | 10 ms | -- | -- |
Synchronous API (used with WebWorkers) | -- | -- See bug 701634 | -- | -- | -- |
Feature | Android | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|
Asynchronous API | -- | 6.0 (6.0) | -- | -- | -- |
Storage limits
For Google Chrome, you can have up to 5 MB of storage, by default. Installed apps can have unlimited storage if you set your manifest file to have unlimited storage and your user grants that permission to your app.
See also
- Basic Concepts About IndexedDB
- Using IndexedDB
- Storing images and files in IndexedDB
- A simple TODO list using HTML5 IndexedDB. Note: This tutorial is based on an old version of the specification and does not work on up-to-date browsers: for example, it still uses the removed setVersion() method.
- Indexed Database API specification
- IndexedDB — The Store in Your Browser
- IndexedDB Support in browsers
- IndexedDB Examples