Web/API/IDBDatabase

From Get docs

The IDBDatabase interface of the IndexedDB API provides a connection to a database; you can use an IDBDatabase object to open a transaction on your database then create, manipulate, and delete objects (data) in that database. The interface provides the only way to get and manage versions of the database.

Note: This feature is available in Web Workers.

Note: Everything you do in IndexedDB always happens in the context of a transaction, representing interactions with data in the database. All objects in IndexedDB — including object stores, indexes, and cursors — are tied to a particular transaction. Thus, you cannot execute commands, access data, or open anything outside of a transaction.


Properties

IDBDatabase.name Read only
A DOMString that contains the name of the connected database.
IDBDatabase.version Read only
A 64-bit integer that contains the version of the connected database. When a database is first created, this attribute is an empty string.
IDBDatabase.objectStoreNames Read only
A DOMStringList that contains a list of the names of the object stores currently in the connected database.

Methods

Inherits from: EventTarget

IDBDatabase.close()
Returns immediately and closes the connection to a database in a separate thread.
IDBDatabase.createMutableFile()
Creates a file handle, allowing files to be stored inside an IndexedDB database.
IDBDatabase.createObjectStore()
Creates and returns a new object store or index.
IDBDatabase.deleteObjectStore()
Destroys the object store with the given name in the connected database, along with any indexes that reference it.
IDBDatabase.transaction()
Immediately returns a transaction object (IDBTransaction) containing the IDBTransaction.objectStore method, which you can use to access your object store. Runs in a separate thread.

Events

Listen to these events using addEventListener() or by assigning an event listener to the oneventname property of this interface.

abort
Fired when when a transaction is aborted and bubbles up to the connection object. Also available via the onabort property.
close
Fired when the database connection is unexpectedly closed. Also available via the onclose property.
error
Fired when a request returns an error and the event bubbles up to the connection object. Also available via the onerror property.
versionchange
Fired when a database structure change was requested. Also available via the onversionchange property.

Example

In the following code snippet, we open a database asynchronously (IDBFactory), handle success and error cases, and create a new object store in the case that an upgrade is needed (IDBdatabase). For a complete working example, see our To-do Notifications app ([[../../../../../../mdn.github.io/to-do-notifications/index|view example live]].)

// Let us open our database
var DBOpenRequest = window.indexedDB.open("toDoList", 4);

// these two event handlers act on the IDBDatabase object,
// when the database is opened successfully, or not
DBOpenRequest.onerror = function(event) {
  note.innerHTML += '<li>Error loading database.</li>';
};
 
DBOpenRequest.onsuccess = function(event) {
  note.innerHTML += '<li>Database initialised.</li>';

  // store the result of opening the database in the db
  // variable. This is used a lot later on
  db = DBOpenRequest.result;

  // Run the displayData() function to populate the task
  // list with all the to-do list data already in the IDB
  displayData();
};

// This event handles the event whereby a new version of
// the database needs to be created Either one has not
// been created before, or a new version number has been
// submitted via the window.indexedDB.open line above

DBOpenRequest.onupgradeneeded = function(event) {
  var db = event.target.result;

  db.onerror = function(event) {
    note.innerHTML += '<li>Error loading database.</li>';
  };

  // Create an objectStore for this database using
  // IDBDatabase.createObjectStore

  var objectStore = db.createObjectStore("toDoList", { keyPath: "taskTitle" });

  // define what data items the objectStore will contain

  objectStore.createIndex("hours", "hours", { unique: false });
  objectStore.createIndex("minutes", "minutes", { unique: false });
  objectStore.createIndex("day", "day", { unique: false });
  objectStore.createIndex("month", "month", { unique: false });
  objectStore.createIndex("year", "year", { unique: false });

  objectStore.createIndex("notified", "notified", { unique: false });

  note.innerHTML += '<li>Object store created.</li>';
};

This next line opens up a transaction on the Database, then opens an object store that we can then manipulate the data inside of.

var objectStore = db.transaction('toDoList', 'readwrite').objectStore('toDoList');

Specifications

Specification Status Comment
Indexed Database API 2.0The definition of 'IDBDatabase' in that specification. Recommendation Initial version
Indexed Database API DraftThe definition of 'IDBDatabase' in that specification. Recommendation

Browser compatibility

Update compatibility data on GitHub

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
IDBDatabase

Chrome Full support 24


Full support 24


No support 23 — 57

Prefixed'

Prefixed' Implemented with the vendor prefix: webkit

Edge

Full support 12

Firefox Full support 16


Full support 16


No support 10 — 16

Prefixed'

Prefixed' Implemented with the vendor prefix: moz

IE

Partial support 10

Opera

Full support 15

Safari

Full support 7

WebView Android Full support Yes


Full support Yes


No support ? — 57

Prefixed'

Prefixed' Implemented with the vendor prefix: webkit

Chrome Android Full support 25


Full support 25


No support 25 — 57

Prefixed'

Prefixed' Implemented with the vendor prefix: webkit

Firefox Android

Full support 22

Opera Android

Full support 14

Safari iOS

Full support 8

Samsung Internet Android Full support 1.5


Full support 1.5


No support 1.5 — 7.0

Prefixed'

Prefixed' Implemented with the vendor prefix: webkit

abort event

Chrome Full support 24


Full support 24


No support 23 — 24

Prefixed'

Prefixed' Implemented with the vendor prefix: webkit

Edge

Full support 12

Firefox Full support 16


Full support 16


No support 10 — 16

Prefixed'

Prefixed' Implemented with the vendor prefix: moz

IE

Partial support 10

Opera

Full support 15

Safari

Full support 7

WebView Android

Full support Yes

Chrome Android

Full support 25

Firefox Android

Full support 22

Opera Android

Full support 14

Safari iOS

Full support 8

Samsung Internet Android

Full support 1.5

close

Chrome Full support 24


Full support 24


No support 23 — 24

Prefixed'

Prefixed' Implemented with the vendor prefix: webkit

Edge

Full support 12

Firefox Full support 16


Full support 16


No support 10 — 16

Prefixed'

Prefixed' Implemented with the vendor prefix: moz

IE

Partial support 10

Opera

Full support 15

Safari

Full support 7

WebView Android

Full support Yes

Chrome Android

Full support 25

Firefox Android

Full support 22

Opera Android

Full support 14

Safari iOS

Full support 8

Samsung Internet Android

Full support 1.5

close event Chrome

Full support 31

Edge

Full support ≤18

Firefox

Full support 50

IE

?

Opera

Full support Yes

Safari

Full support 10.1

WebView Android

Full support Yes

Chrome Android

Full support 31

Firefox Android

Full support 50

Opera Android

Full support Yes

Safari iOS

Full support 10.3

Samsung Internet Android

Full support 2.0

createObjectStore

Chrome Full support 24


Full support 24


No support 23 — 24

Prefixed'

Prefixed' Implemented with the vendor prefix: webkit

Edge

Full support 12

Firefox Full support 16


Full support 16


No support 10 — 16

Prefixed'

Prefixed' Implemented with the vendor prefix: moz

IE

Partial support 10

Opera

Full support 15

Safari

Full support 7

WebView Android

Full support Yes

Chrome Android

Full support 25

Firefox Android

Full support 22

Opera Android

Full support 14

Safari iOS

Full support 8

Samsung Internet Android

Full support 1.5

deleteObjectStore

Chrome Full support 24


Full support 24


No support 23 — 24

Prefixed'

Prefixed' Implemented with the vendor prefix: webkit

Edge

Full support 12

Firefox Full support 16


Full support 16


No support 10 — 16

Prefixed'

Prefixed' Implemented with the vendor prefix: moz

IE

Partial support 10

Opera

Full support 15

Safari

Full support 7

WebView Android

Full support Yes

Chrome Android

Full support 25

Firefox Android

Full support 22

Opera Android

Full support 14

Safari iOS

Full support 8

Samsung Internet Android

Full support 1.5

error event

Chrome Full support 24


Full support 24


No support 23 — 24

Prefixed'

Prefixed' Implemented with the vendor prefix: webkit

Edge

Full support 12

Firefox Full support 16


Full support 16


No support 10 — 16

Prefixed'

Prefixed' Implemented with the vendor prefix: moz

IE

Partial support 10

Opera

Full support 15

Safari

Full support 7

WebView Android

Full support Yes

Chrome Android

Full support 25

Firefox Android

Full support 22

Opera Android

Full support 14

Safari iOS

Full support 8

Samsung Internet Android

Full support 1.5

name

Chrome Full support 24


Full support 24


No support 23 — 24

Prefixed'

Prefixed' Implemented with the vendor prefix: webkit

Edge

Full support 12

Firefox Full support 16


Full support 16


No support 10 — 16

Prefixed'

Prefixed' Implemented with the vendor prefix: moz

IE

Partial support 10

Opera

Full support 15

Safari

Full support 7

WebView Android

Full support Yes

Chrome Android

Full support 25

Firefox Android

Full support 22

Opera Android

Full support 14

Safari iOS

Full support 8

Samsung Internet Android

Full support 1.5

objectStoreNames

Chrome Full support 24


Full support 24


No support 23 — 24

Prefixed'

Prefixed' Implemented with the vendor prefix: webkit

Edge

Full support 12

Firefox Full support 16


Full support 16


No support 10 — 16

Prefixed'

Prefixed' Implemented with the vendor prefix: moz

IE

Partial support 10

Opera

Full support 15

Safari

Full support 7

WebView Android

Full support Yes

Chrome Android

Full support 25

Firefox Android

Full support 22

Opera Android

Full support 14

Safari iOS

Full support 8

Samsung Internet Android

Full support 1.5

onabort

Chrome Full support 24


Full support 24


No support 23 — 24

Prefixed'

Prefixed' Implemented with the vendor prefix: webkit

Edge

Full support 12

Firefox Full support 16


Full support 16


No support 10 — 16

Prefixed'

Prefixed' Implemented with the vendor prefix: moz

IE

Partial support 10

Opera

Full support 15

Safari

Full support 7

WebView Android

Full support Yes

Chrome Android

Full support 25

Firefox Android

Full support 22

Opera Android

Full support 14

Safari iOS

Full support 8

Samsung Internet Android

Full support 1.5

onclose

Chrome Full support 31

Notes'

Full support 31

Notes'

Notes' approx

Edge

Full support ≤18

Firefox

Full support 50

IE

No support No

Opera

Full support Yes

Safari

Full support 10.1

WebView Android

Full support Yes

Chrome Android

Full support 31

Firefox Android

Full support 50

Opera Android

Full support Yes

Safari iOS

Full support 10.3

Samsung Internet Android

Full support 2.0

onerror

Chrome Full support 24


Full support 24


No support 23 — 24

Prefixed'

Prefixed' Implemented with the vendor prefix: webkit

Edge

Full support 12

Firefox Full support 16


Full support 16


No support 10 — 16

Prefixed'

Prefixed' Implemented with the vendor prefix: moz

IE

Partial support 10

Opera

Full support 15

Safari

Full support 7

WebView Android

Full support Yes

Chrome Android

Full support 25

Firefox Android

Full support 22

Opera Android

Full support 14

Safari iOS

Full support 8

Samsung Internet Android

Full support 1.5

onversionchange

Chrome Full support 24


Full support 24


No support 23 — 24

Prefixed'

Prefixed' Implemented with the vendor prefix: webkit

Edge

Full support 12

Firefox Full support 16


Full support 16


No support 10 — 16

Prefixed'

Prefixed' Implemented with the vendor prefix: moz

IE

Partial support 10

Opera

Full support 15

Safari

Full support 7

WebView Android

Full support Yes

Chrome Android

Full support 25

Firefox Android

Full support 22

Opera Android

Full support 14

Safari iOS

Full support 8

Samsung Internet Android

Full support 1.5

transaction

Chrome Full support 24


Full support 24


No support 23 — 24

Prefixed'

Prefixed' Implemented with the vendor prefix: webkit

Edge

Full support 12

Firefox Full support 16


Full support 16


No support 10 — 16

Prefixed'

Prefixed' Implemented with the vendor prefix: moz

IE

Partial support 10

Opera

Full support 15

Safari

Full support 7

WebView Android

Full support Yes

Chrome Android

Full support 25

Firefox Android

Full support 22

Opera Android

Full support 14

Safari iOS

Full support 8

Samsung Internet Android

Full support 1.5

version

Chrome Full support 24


Full support 24


No support 23 — 24

Prefixed'

Prefixed' Implemented with the vendor prefix: webkit

Edge

Full support 12

Firefox Full support 16


Full support 16


No support 10 — 16

Prefixed'

Prefixed' Implemented with the vendor prefix: moz

IE

Partial support 10

Opera

Full support 15

Safari

Full support 7

WebView Android

Full support Yes

Chrome Android

Full support 25

Firefox Android

Full support 22

Opera Android

Full support 14

Safari iOS

Full support 8

Samsung Internet Android

Full support 1.5

versionchange event

Chrome Full support 24


Full support 24


No support 23 — 24

Prefixed'

Prefixed' Implemented with the vendor prefix: webkit

Edge

Full support 12

Firefox Full support 16


Full support 16


No support 10 — 16

Prefixed'

Prefixed' Implemented with the vendor prefix: moz

IE

Partial support 10

Opera

Full support 15

Safari

Full support 7

WebView Android

Full support Yes

Chrome Android

Full support 25

Firefox Android

Full support 22

Opera Android

Full support 14

Safari iOS

Full support 8

Samsung Internet Android

Full support 1.5

Available in workers

Chrome Full support Yes


Full support Yes


No support 23 — 57

Prefixed'

Prefixed' Implemented with the vendor prefix: webkit

Edge

Full support ≤18

Firefox

Full support 37

IE

?

Opera

Full support Yes

Safari

?

WebView Android Full support Yes


Full support Yes


No support ? — 57

Prefixed'

Prefixed' Implemented with the vendor prefix: webkit

Chrome Android Full support Yes


Full support Yes


No support 25 — 57

Prefixed'

Prefixed' Implemented with the vendor prefix: webkit

Firefox Android

Full support 37

Opera Android

Full support Yes

Safari iOS

?

Samsung Internet Android Full support Yes


Full support Yes


No support 1.5 — 7.0

Prefixed'

Prefixed' Implemented with the vendor prefix: webkit

Legend

Full support  
Full support
Partial support  
Partial support
No support  
No support
Compatibility unknown  
Compatibility unknown
See implementation notes.'
See implementation notes.
Requires a vendor prefix or different name for use.'
Requires a vendor prefix or different name for use.


See also