--- title: IDBDatabase slug: Web/API/IDBDatabase tags: - API - Database - IDBDatabase - IndexedDB - Interface - NeedsTranslation - Reference - Storage - TopicStub - accessing data - asynchronous access - transactions translation_of: Web/API/IDBDatabase ---

{{APIRef("IndexedDB")}}

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.

{{AvailableInWorkers}}

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.

Methods

Inherits from: EventTarget

{{domxref("IDBDatabase.close()")}}
Returns immediately and closes the connection to a database in a separate thread.
{{domxref("IDBDatabase.createObjectStore()")}}
Creates and returns a new object store or index.
{{domxref("IDBDatabase.deleteObjectStore()")}}
Destroys the object store with the given name in the connected database, along with any indexes that reference it.
{{domxref("IDBDatabase.transaction()")}}
Immediately returns a transaction object ({{domxref("IDBTransaction")}}) containing the {{domxref("IDBTransaction.objectStore")}} method, which you can use to access your object store. Runs in a separate thread.

Properties

{{domxref("IDBDatabase.name")}} {{readonlyInline}}
A {{ domxref("DOMString") }} that contains the name of the connected database.
{{domxref("IDBDatabase.version")}} {{readonlyInline}}
A 64-bit integer that contains the version of the connected database. When a database is first created, this attribute is an empty string.
{{domxref("IDBDatabase.objectStoreNames")}} {{readonlyInline}}
A {{ domxref("DOMStringList") }} that contains a list of the names of the object stores currently in the connected database.

Event handlers

{{domxref("IDBDatabase.onabort")}}
Fires when access of the database is aborted.
{{domxref("IDBDatabase.onclose")}}
Fires when the {{event("close")}} event occurs; this happens when the database is unexpectedly closed, such as during application shutdown.
{{domxref("IDBDatabase.onerror")}}
Fires when access to the database fails.
{{domxref("IDBDatabase.onversionchange")}}

Fires when a database structure change ({{domxref("IDBOpenDBRequest.onupgradeneeded")}} event or {{domxref("IDBFactory.deleteDatabase()")}} was requested elsewhere (most probably in another window/tab on the same computer). This is different from the version change transaction (see {{domxref("IDBVersionChangeEvent")}}), but it is related.

Example

In the following code snippet, we open a database asynchronously ({{domxref("IDBFactory")}}), handle success and error cases, and create a new object store in the case that an upgrade is needed ({{ domxref("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'); 

Specifications

Specification Status Comment
{{SpecName('IndexedDB', '#idl-def-IDBDatabase', 'IDBDatabase')}} {{Spec2('IndexedDB')}} Initial version
{{SpecName("IndexedDB 2", "#database-interface", "IDBDatabase")}} {{Spec2("IndexedDB 2")}}  

Browser compatibility

{{Compat("api.IDBDatabase")}}

See also