Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. IDBDatabase

IDBDatabase

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨July 2015⁩.

Note: This feature is available inWeb Workers.

TheIDBDatabase interface of the IndexedDB API provides aconnection to a database; you can use anIDBDatabase object to open atransaction 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 atransaction, 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.

EventTarget IDBDatabase

Instance properties

IDBDatabase.nameRead only

A string that contains the name of the connected database.

IDBDatabase.versionRead 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.objectStoreNamesRead only

ADOMStringList that contains a list of the names of theobject stores currently in the connected database.

Instance methods

Inherits from:EventTarget

IDBDatabase.close()

Returns immediately and closes the connection to a database in a separate thread.

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 theIDBTransaction.objectStore method, which you can use to access your object store. Runs in a separate thread.

Events

Listen to these events usingaddEventListener() or by assigning an event listener to theoneventname property of this interface.

close

An event fired when the database connection is unexpectedly closed.

versionchange

An event fired when a database structure change was requested.

The following events are available toIDBDatabase via event bubbling fromIDBTransaction:

IDBTransactionabort

An event fired when a transaction is aborted.

IDBTransactionerror

An event fired when a request returns an error and the event bubbles up to the connection object.

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 ourTo-do Notifications app (view example live).

js
// Let us open our databaseconst DBOpenRequest = window.indexedDB.open("toDoList", 4);// these two event handlers act on the IDBDatabase object,// when the database is opened successfully, or notDBOpenRequest.onerror = (event) => {  note.appendChild(document.createElement("li")).textContent =    "Error loading database.";};DBOpenRequest.onsuccess = (event) => {  node.appendChild(document.createElement("li")).textContent =    "Database initialized.";  // 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 aboveDBOpenRequest.onupgradeneeded = (event) => {  const db = event.target.result;  db.onerror = (event) => {    note.appendChild(document.createElement("li")).textContent =      "Error loading database.";  };  // Create an objectStore for this database using  // IDBDatabase.createObjectStore  const 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.appendChild(document.createElement("li")).textContent =    "Object store created.";};

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

js
const objectStore = db  .transaction("toDoList", "readwrite")  .objectStore("toDoList");

Specifications

Specification
Indexed Database API 3.0
# database-interface

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp