The transaction
method of the IDBDatabase
interface immediately returns a transaction object (IDBTransaction
) containing the IDBTransaction.objectStore
method, which you can use to access your object store.
transaction(storeNames)
transaction(storeNames, mode)
transaction(storeNames, mode, options)
In this example we open a database connection, then use transaction() to open a transaction on the database. For a complete example, see our To-do Notifications app (view example live).
let db;
const DBOpenRequest = window.indexedDB.open("toDoList", 4);
DBOpenRequest.onsuccess = (event) => {
note.innerHTML += "<li>Database initialized.</li>";
db = DBOpenRequest.result;
displayData();
};
const transaction = db.transaction(["toDoList"], "readwrite");
transaction.oncomplete = (event) => {
note.innerHTML +=
"<li>Transaction completed: database modification finished.</li>";
};
transaction.onerror = (event) => {
note.innerHTML +=
"<li>Transaction not opened due to error. Duplicate items not allowed.</li>";
};
const objectStore = transaction.objectStore("toDoList");