IDBObjectStore: autoIncrement 属性

注意: 此功能在 Web Workers 中可用。

autoIncrementIDBObjectStore 接口的只读属性,它返回此对象存储的自动递增标志的值。

请注意,每个对象存储都有自己的独立自动递增计数器。

布尔值

含义
true 对象存储自动递增。
false 对象存储不自动递增。

示例

在以下代码片段中,我们在数据库上打开了一个读写事务,并使用 add() 将一些数据添加到对象存储中。对象存储创建后,我们将 objectStore.autoIncrement 记录到控制台中。有关完整的工作示例,请参见我们的 待办事项通知 应用程序 (查看实时示例).

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

DBOpenRequest.onsuccess = (event) => {
  note.appendChild(document.createElement("li")).textContent =
    "Database initialized.";

  // 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
  const 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
  const transaction = db.transaction(["toDoList"], "readwrite");

  // report on the success of the transaction completing, when everything is done
  transaction.oncomplete = (event) => {
    note.appendChild(document.createElement("li")).textContent =
      "Transaction completed.";
  };

  transaction.onerror = (event) => {
    note.appendChild(document.createElement("li")).textContent =
      "Transaction not opened due to error. Duplicate items not allowed.";
  };

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

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

  objectStoreRequest.onsuccess = (event) => {
    // report the success of our request
    note.appendChild(document.createElement("li")).textContent =
      "Request successful.";
  };
}

规范

规范
Indexed Database API 3.0
# ref-for-dom-idbobjectstore-autoincrement①

浏览器兼容性

BCD 表格仅在启用 JavaScript 的浏览器中加载。

另请参阅