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: 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.
IDBDatabase.name Read only
DOMString that contains the name of the connected database.IDBDatabase.version Read only
IDBDatabase.objectStoreNames Read only
DOMStringList that contains a list of the names of the object stores currently in the connected database.IDBDatabase.onabortIDBDatabase.oncloseclose event occurs; this happens when the database is unexpectedly closed, such as during application shutdown.IDBDatabase.onerrorIDBDatabase.onversionchangeFires when a database structure change (IDBOpenDBRequest.onupgradeneeded event orIDBFactory.deleteDatabase() was requested elsewhere (most probably in another window/tab on the same computer). This is different from the version change transaction (see IDBVersionChangeEvent), but it is related.
Inherits from: EventTarget
IDBDatabase.close()IDBDatabase.createObjectStore()IDBDatabase.deleteObjectStore()IDBDatabase.transaction()IDBTransaction) containing the IDBTransaction.objectStore method, which you can use to access your object store. Runs in a separate thread.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 (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').objectStore('toDoList'); | Specification | Status | Comment |
|---|---|---|
| Indexed Database API The definition of 'IDBDatabase' in that specification. | Recommendation | Initial version |
| Indexed Database API 2.0 The definition of 'IDBDatabase' in that specification. | Recommendation |
| Desktop | ||||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
| Basic support | 24
|
Yes | 16
|
10
|
15 | 7 |
| Available in workers | Yes
|
Yes | 37 | ? | Yes | ? |
name |
24
|
12 | 16
|
10
|
15 | 7 |
objectStoreNames |
24
|
12 | 16
|
10
|
15 | 7 |
onabort |
24
|
12 | 16
|
10
|
15 | 7 |
onclose |
31
|
Yes | 50 | ? | Yes | 10.1 |
onerror |
24
|
12 | 16
|
10
|
15 | 7 |
onversionchange |
24
|
? | 16
|
10
|
15 | 7 |
version |
24
|
12 | 16
|
10
|
15 | 7 |
close |
24
|
12 | 16
|
10
|
15 | 7 |
createObjectStore |
24
|
12 | 16
|
10
|
15 | 7 |
deleteObjectStore |
24
|
12 | 16
|
10
|
15 | 7 |
transaction |
24
|
12 | 16
|
10
|
15 | 7 |
| Mobile | |||||||
|---|---|---|---|---|---|---|---|
| Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
| Basic support | Yes
|
25
|
? | 22 | 22 | 8 | Yes
|
| Available in workers | Yes
|
Yes
|
Yes | 37 | Yes | ? | Yes
|
name |
Yes | Yes | Yes | 22 | 22 | 8 | Yes |
objectStoreNames |
Yes | Yes | Yes | 22 | 22 | 8 | Yes |
onabort |
Yes | Yes | Yes | 22 | 22 | 8 | Yes |
onclose |
Yes | Yes | Yes | 50 | Yes | 10.1 | Yes |
onerror |
Yes | Yes | Yes | 22 | 22 | 8 | Yes |
onversionchange |
Yes | Yes | ? | 22 | 22 | 8 | Yes |
version |
Yes | Yes | Yes | 22 | 22 | 8 | Yes |
close |
Yes | Yes | Yes | 22 | 22 | 8 | Yes |
createObjectStore |
Yes | Yes | Yes | 22 | 22 | 8 | Yes |
deleteObjectStore |
Yes | Yes | Yes | 22 | 22 | 8 | Yes |
transaction |
Yes | Yes | Yes | 22 | 22 | 8 | Yes |
IDBDatabase
IDBTransaction
IDBKeyRange
IDBObjectStore
IDBCursor
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/IDBDatabase