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.appendChild(document.createElement("li")).textContent =
"Database initialized.";
db = DBOpenRequest.result;
displayData();
};
const transaction = db.transaction(["toDoList"], "readwrite");
transaction.oncomplete = (event) => {
note.appendChild(document.createElement("li")).textContent =
"Transaction completed: database modification finished.";
};
transaction.onerror = (event) => {
note.appendChild(document.createElement("li")).textContent =
"Transaction not opened due to error. Duplicate items not allowed.";
};
const objectStore = transaction.objectStore("toDoList");