--- title: IDBDatabase slug: Web/API/IDBDatabase tags: - API - Database - IDBDatabase - IndexedDB - Storage - transactions translation_of: Web/API/IDBDatabase ---
{{APIRef()}}
IndexedDB APIのIDBDatabaseインターフェイスは、データベースへの接続を提供します。 IDBDatabaseオブジェクトで、データベースのtransactionを開き、データベースのオブジェクト(データ)を生成したり、操作したり、削除したりできます。このインターフェイスはデータベースのバージョンを取得したり、統合したりする唯一の方法を提供します。
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.
Inherits from: EventTarget
データベースの構造が({{domxref("IDBOpenDBRequest.onupgradeneeded")}}イベントで変更されるか、{{domxref("IDBFactory.deleteDatabase")}} がどこかで(ほとんどの場合、同じコンピューターの他のウィンドウやタブで)要求された場合に発生します。これはversion change transaction (see {{domxref("IDBVersionChangeEvent")}})とは異なりますが、関連があります。
次のコードスニペットでは、データベースを非同期で開き({{domxref("IDBFactory")}})、成功と失敗の場合にイベントを登録し、アップグレードが必要な場合には、新しいオブジェクトストアを生成しています({{ domxref("IDBdatabase") }})。 完璧に動作する例は、 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>';
};
次の行は、データベースでトランザクションを開いて、そしてオブジェクトストアを開いて、内側のデータを操作しています。
var objectStore = db.transaction('toDoList').objectStore('toDoList');
| Specification | Status | Comment |
|---|---|---|
| {{SpecName('IndexedDB', '#idl-def-IDBDatabase', 'IDBDatabase')}} | {{Spec2('IndexedDB')}} |
{{Compat("api.IDBDatabase")}}