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
-
IDBDatabase.close
- 即座に応答して、別スレッドでデータベースを閉じる。
-
IDBDatabase.createObjectStore
- 新しくオブジェクトストアかインデックスを生成して返す。
-
IDBDatabase.deleteObjectStore
- 参照しているインデックスがあったとしても、接続中のデータベースで与えられた名前のオブジェクトストアを削除する。
-
IDBDatabase.transaction
-
オブジェクトストアにアクセスできる
IDBTransaction.objectStore
メソッドを含むトランザクションオブジェクト (IDBTransaction
)を即座に返す。別スレッドで実行される。
プロパティ
-
IDBDatabase.name
読取専用 -
接続しているデータベース名を含む
DOMString
。 -
IDBDatabase.version
読取専用 - 接続しているデータベースのバージョンを含む64-bit integer。データベースが初めて作られた場合、この属性は空文字である。
-
IDBDatabase.objectStoreNames
読取専用 -
接続しているデータベースのobject stores名のリストを含む
DOMStringList
。
イベントハンドラ
-
IDBDatabase.onabort
- データベースの接続が中止された場合に発生する。
-
IDBDatabase.onerror
- データベースへの接続が失敗した場合に発生する。
-
IDBDatabase.onversionchange
-
データベースの構造が(
IDBOpenDBRequest.onupgradeneeded
イベントで変更されるか、
IDBFactory.deleteDatabase
がどこかで(ほとんどの場合、同じコンピューターの他のウィンドウやタブで)要求された場合に発生します。これはversion change transaction (seeIDBVersionChangeEvent
)とは異なりますが、関連があります。
例
次のコードスニペットでは、データベースを非同期で開き(IDBFactory
)、成功と失敗の場合にイベントを登録し、アップグレードが必要な場合には、新しいオブジェクトストアを生成しています(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 |
---|---|---|
Indexed Database API 2.0 IDBDatabase の定義 |
勧告 |
ブラウザ実装状況
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support | 23webkit 24 |
10 moz 16.0 (16.0) |
10, partial | 15 | 7.1 |
Feature | Android | Firefox Mobile (Gecko) | Firefox OS | IE Phone | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | 4.4 | 22.0 (22.0) | 1.0.1 | 10 | 22 | 未サポート |
関連情報
- Using IndexedDB
- Starting transactions:
IDBDatabase
- Using transactions:
IDBTransaction
- Setting a range of keys:
IDBKeyRange
- Retrieving and making changes to your data:
IDBObjectStore
- Using cursors:
IDBCursor
- Reference example: To-do Notifications (view example live.)