IDBObjectStore.keyPath

IDBObjectStore的只读属性 keyPath 接口返回当前 objectStore 的key path (en-US)

什么是 keyPath 呢?在 indexedDB 中,一条记录就是一个 object,object 里面有一个属性作为这条记录的主要依据用来进行查询,而这个属性的属性名就是 keyPath,属性值就是 key。在用 indexedDB 的 get 方法时,提供 key,而不需要指定 keyPath,因为 get 方法把参数作为这个最主要的属性的值,在数据库中进行查询。(译者注)

如果该属性值为 null,应用中必须在每一次进行修改性质的操作时提供一个 key。

add、put 方法都可以传第二个参数,当你当前的 objectStore 的 autoIncrement 为 true 时,你一般不会设置 keyPath,如果这个时候你在 put 的时候不提供第二个参数,indexedDB 就不知道要更新哪一条记录了。(译者注)

备注: 此特性在 Web Worker 中可用。

句法

var mykeyPath = objectStore.keyPath;

Value

任何类型。

例子

在下面代码片段中,我们在数据库里打开了一个可读写的事务(transaction),并且用add()向一个 objectStore 中添加了一些数据。在 objectStore 被创建之后,我们在 console 中打印了 objectStore.keyPath 的值。想查看完整的例子,请查看我们的To-do Notifications应用(查看在线例子)。

js
// Let us open our database
var DBOpenRequest = window.indexedDB.open("toDoList", 4);

DBOpenRequest.onsuccess = function (event) {
  note.innerHTML += "<li>Database initialised.</li>";

  // store the result of opening the database in the db variable.
  // This is used a lot below
  db = DBOpenRequest.result;

  // Run the addData() function to add the data to the database
  addData();
};

function addData() {
  // Create a new object ready to insert into the IDB
  var newItem = [
    {
      taskTitle: "Walk dog",
      hours: 19,
      minutes: 30,
      day: 24,
      month: "December",
      year: 2013,
      notified: "no",
    },
  ];

  // open a read/write db transaction, ready for adding the data
  var transaction = db.transaction(["toDoList"], "readwrite");

  // report on the success of the transaction completing, when everything is done
  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>";
  };

  // create an object store on the transaction
  var objectStore = transaction.objectStore("toDoList");
  console.log(objectStore.keyPath);

  // Make a request to add our newItem object to the object store
  var objectStoreRequest = objectStore.add(newItem[0]);

  objectStoreRequest.onsuccess = function (event) {
    // report the success of our request
    note.innerHTML += "<li>Request successful.</li>";
  };
}

规范

Specification
Indexed Database API 3.0
# ref-for-dom-idbobjectstore-keypath①

浏览器兼容性

BCD tables only load in the browser

相关内容