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.)
var DBOpenRequest = window.indexedDB.open("toDoList", 4);
DBOpenRequest.onsuccess = function(event) {
note.innerHTML += '<li>Database initialized.</li>';
db = DBOpenRequest.result;
addData();
};
function addData() {
var newItem = [ { taskTitle: "Walk dog", hours: 19, minutes: 30, day: 24, month: "December", year: 2013, notified: "no" } ];
var transaction = db.transaction(["toDoList"], "readwrite");
transaction.oncomplete = function(event) {
note.innerHTML += '<li>Transaction completed.</li>';
};
transaction.onerror = function(event) {
note.innerHTML += '<li>Transaction not opened due to error. Duplicate items not allowed.</li>';
};
var objectStore = transaction.objectStore("toDoList");
console.log(objectStore.keyPath);
var objectStoreRequest = objectStore.add(newItem[0]);
objectStoreRequest.onsuccess = function(event) {
note.innerHTML += '<li>Request successful.</li>';
};
};