IDBTransaction: objectStore() method
The objectStore()
method of the IDBTransaction
interface returns an object store that has already been added to the scope of this transaction.
Every call to this method on the same transaction object, with the same name, returns the same IDBObjectStore
instance. If this method is called on a different transaction object, a different IDBObjectStore
instance is returned.
Syntax
Parameters
-
name
-
The name of the requested object store.
Return value
An IDBObjectStore
object for accessing an object store.
Exceptions
NotFoundError
DOMException
-
Thrown if the requested object store is not in this transaction's scope.
InvalidStateError
DOMException
-
Thrown if the request was made on a source object that has been deleted or removed, or if the transaction has finished.
Examples
In the following code snippet, we open a read/write transaction on our database and add some data to an object store. Note also the functions attached to transaction event handlers to report on the outcome of the transaction opening in the event of success or failure. For a full working example, see our To-do Notifications app (View example live).
const note = document.getElementById("notifications");
let db;
const DBOpenRequest = window.indexedDB.open("toDoList", 4);
DBOpenRequest.onsuccess = (event) => {
note.innerHTML += "<li>Database initialized.</li>";
db = DBOpenRequest.result;
addData();
};
function addData() {
const newItem = [
{
taskTitle: "Walk dog",
hours: 19,
minutes: 30,
day: 24,
month: "December",
year: 2013,
notified: "no",
},
];
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");
const objectStoreRequest = objectStore.add(newItem[0]);
objectStoreRequest.onsuccess = (event) => {
note.innerHTML += "<li>Request successful.</li>";
};
}
Specifications
Browser compatibility
|
Desktop |
Mobile |
|
Chrome |
Edge |
Firefox |
Internet Explorer |
Opera |
Safari |
WebView Android |
Chrome Android |
Firefox for Android |
Opera Android |
Safari on IOS |
Samsung Internet |
objectStore |
23 |
12 |
10 |
10 |
15 |
8 |
4.4 |
25 |
22 |
14 |
8 |
1.5 |
See also