IndexedDB API の IDBDatabase インターフェイスは、データベースへの接続を提供します。IDBDatabase オブジェクトで、データベースの transaction を開き、データベースのオブジェクト(データ)を生成したり、操作したり、削除したりできます。このインターフェイスはデータベースのバージョンを取得したり、統合したりする唯一の方法を提供します。
注: IndexedDB で行うことはすべて、常に transaction (トランザクション)(en-US)のコンテキストで行われます。データベース内のデータとの相互作用を表します。IndexedDB 内のすべてのオブジェクト ― オブジェクト ストア、インデックス、およびカーソル ― 特定のトランザクションに関連付けられています。したがって、コマンドを実行することはできません、データにアクセスするか、トランザクションの外部で何かを開きます。
継承元: EventTarget
// 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');
| 表示内容: |
| 表示内容: |