The keyPath
read-only property of the IDBObjectStore
interface returns the key path of this object store.
If this property is null, the application must provide a key for each modification operation.
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.keyPath
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.keyPath);
const objectStoreRequest = objectStore.add(newItem[0]);
objectStoreRequest.onsuccess = (event) => {
note.innerHTML += "<li>Request successful.</li>";
};
}