The autoIncrement
read-only property of the IDBObjectStore
interface returns the value of the auto increment flag for this object store.
Note that every object store has its own separate auto increment counter.
In the following code snippet, we open a read/write transaction on our database and add some data to an object store using add()
. After the object store has been created, we log objectStore.autoIncrement
to the console. For a full working example, see our To-do Notifications app (view example live).
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.</li>";
};
transaction.onerror = (event) => {
note.innerHTML +=
"<li>Transaction not opened due to error. Duplicate items not allowed.</li>";
};
const objectStore = transaction.objectStore("toDoList");
console.log(objectStore.autoIncrement);
const objectStoreRequest = objectStore.add(newItem[0]);
objectStoreRequest.onsuccess = (event) => {
note.innerHTML += "<li>Request successful.</li>";
};
}