Table of contents
- 1. Example
- 2. Method overview
- 3. Methods
- 3.1. open() Obsolete
- 3.2. deleteDatabase()
- 3.3. cmp()
- 4. Browser compatibility
The IDBFactory interface of the IndexedDB API lets applications asynchronously access the indexed databases. The object that implements the interface is window.indexedDB. You open—that is, create and access—and delete a database with the object and not directly with IDBFactory.
This interface still has vendor prefixes, that is to say, you have to make calls with mozIndexedDB.open() for Firefox and webkitIndexedDB.open() for Chrome.
Example
In the following code snippet, we open a database asynchronously and make a request. Event handlers are registered for responding to various situations.
// Taking care of the browser-specific prefixes.
if ('webkitIndexedDB' in window) {
window.indexedDB = window.webkitIndexedDB;
} else if ('mozIndexedDB' in window) {
window.indexedDB = window.mozIndexedDB;
...
//Open a database called myAwesomeDatabase
var request = window.indexedDB.open('myAwesomeDatabase');
//When a success event happens, do something.
request.onsuccess = function(event) {
var db = this.result;
var transaction = db.transaction([], IDBTransaction.READ_ONLY);
var curRequest = transaction.objectStore('ObjectStore Name').openCursor();
curRequest.onsuccess = ...;
};
//When an error event happens, do something else.
request.onerror = function(event) {
...;
};
Method overview
IDBRequest open (in DOMString name);
Obsolete
(some browser still implement it) |
IDBOpenDBRequest open (in DOMString name, long long version);Requires Gecko 10
|
IDBRequest deleteDatabase (in DOMString name); |
integer cmp (any first, any second) |
Methods
open() Obsolete
Request opening a connection to a database. The method returns an IDBRequest object immediately, and performs the opening operation asynchronously.
The opening operation—which is performed in a separate thread—consists of the following steps:
- If a database named
myAwesomeDatabasealready exists:- Wait until any existing
VERSION_CHANGEtransactions have finished. - If the database has a deletion pending, wait until it has been deleted.
- Wait until any existing
- If no database with that name exists, create a database with the provided name, with the empty string as its version, and no object stores.
- Create a connection to the database.
If the operation is successful, an IDBSuccessEvent is fired on the request object that is returned from this method, with its result attribute set to the new IDBDatabase object for the connection.
If an error occurs while the database connection is being opened, then an error event is fired on the request object returned from this method, with its code and message set to appropriate values.
IDBRequest open( in DOMString name );
Parameters
- name
- The name of the database.
- version
- The version of the database.
Returns
IDBRequest- A request object on which subsequent events related to this request are fired. In the latest draft of the specification, which has not yet been implemented by browsers, the returned object is
IDBOpenRequest.
deleteDatabase()
Request deleting a database. The method returns an IDBRequest object immediately, and performs the deletion operation asynchronously.
The deletion operation (performed in a different thread) consists of the following steps:
- If there is no database with the given name, exit successfully.
- Fire an IDBVersionChangeEvent at all connection objects connected to the named database, with
versionset tonull. - If any connection objects connected to the database are still open, fire a
blockedevent at the request object returned by thedeleteDatabasemethod, using IDBVersionChangeEvent withversionset tonull. - Wait until all open connections to the database are closed.
- Delete the database.
If the database is successfully deleted, then an IDBSuccessEvent is fired on the request object returned from this method, with its result set to null.
If an error occurs while the database is being deleted, then an error event is fired on the request object that is returned from this method, with its code and message set to appropriate values.
Tip: If the browser you are using hasn't implemented this yet, you can delete the object stores one by one, thus effectively removing the database.
IDBRequest deleteDatabase( in DOMString name );
Parameters
- name
- The name of the database.
Returns
IDBRequest- A request object on which subsequent events related to this request are fired. In the latest draft of the specification, which has not yet been implemented by browsers, the returned object is
IDBOpenRequest.
cmp()
Compares two values as keys to determine equality and ordering for IndexedDB operations, such as storing and iterating. Do not use this method for comparing arbitrary JavaScript values, because many JavaScript values are either not valid IndexedDB keys (booleans and objects, for example) or are treated as equivalent IndexedDB keys (for example, since IndexedDB ignores arrays with non-numeric properties and treats them as empty arrays, so any non-numeric array are treated as equivalent).
This throws an exception if either of the values is not a valid key.
int cmp( in any first, in any second );
Parameters
- first
- The first key to compare.
- second
- The second key to compare.
Returns
- Integer
-
Returned value Description -1 1st key < 2nd 0 1st key = 2nd 1 1st key > 2nd
Exceptions
This method can raise an IDBDatabaseException with the following code:
| Attribute | Description |
|---|---|
NON_TRANSIENT_ERR | One of the supplied keys was not a valid key. |
Browser compatibility
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
|---|---|---|---|---|---|
open() old specification | 12 webkit | 4.0 (2.0) moz until 9.0 (9.0) | -- | -- | -- |
open() new specification | -- | 10.0 (10.0) | -- | -- | -- |
cmp () | 16 webkit | 11.0 (11.0) | -- | -- | -- |
deleteDatabase() | 17 webkit | 10.0 (10.0) | -- | -- | -- |
| Feature | Android | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|
| Basic support | -- | 6.0 (6.0) | ? | -- | -- |


Mozilla Developer Network