IDBObjectStore

Baseline 广泛可用 *

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

* 此特性的某些部分可能存在不同级别的支持。

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

IndexedDB API 的 IDBObjectStore 接口代表数据库中的一个对象存储。对象存储中的记录根据它们的键进行排序。这种排序支持快速插入、查找和有序检索。

实例属性

IDBObjectStore.indexNames 只读

此对象存储中对象上的索引名称列表。

IDBObjectStore.keyPath 只读

此对象存储的键路径。如果此属性为 null,则应用程序必须为每个修改操作提供一个键。

IDBObjectStore.name

此对象存储的名称。

IDBObjectStore.transaction 只读

此对象存储所属的IDBTransaction 对象。

IDBObjectStore.autoIncrement 只读

此对象存储的自动递增标志的值。

实例方法

IDBObjectStore.add()

返回一个 IDBRequest 对象,并在单独的线程中创建 value结构化克隆,然后将克隆的值存储在对象存储中。这用于向对象存储添加新记录。

IDBObjectStore.clear()

创建并立即返回一个 IDBRequest 对象,并在单独的线程中清空此对象存储。这用于删除对象存储中的所有当前记录。

IDBObjectStore.count()

返回一个 IDBRequest 对象,并在单独的线程中返回与提供的键或 IDBKeyRange 匹配的记录总数。如果不提供参数,则返回存储中的记录总数。

IDBObjectStore.createIndex()

在版本升级期间创建新索引,并在连接的数据库中返回一个新的 IDBIndex 对象。

IDBObjectStore.delete()

返回一个 IDBRequest 对象,并在单独的线程中删除由指定键选择的存储对象。这用于从对象存储中删除单个记录。

IDBObjectStore.deleteIndex()

销毁连接的数据库中的指定索引,用于版本升级期间。

IDBObjectStore.get()

返回一个 IDBRequest 对象,并在单独的线程中返回由指定键选择的存储对象。这用于从对象存储中检索特定记录。

IDBObjectStore.getKey()

返回一个 IDBRequest 对象,并在单独的线程中检索并返回对象存储中与指定参数匹配的对象的记录键。

IDBObjectStore.getAll()

返回一个 IDBRequest 对象,检索对象存储中与指定参数匹配的所有对象,或者在未提供参数时检索存储中的所有对象。

IDBObjectStore.getAllKeys()

返回一个 IDBRequest 对象,检索对象存储中与指定参数匹配的所有对象的记录键,或者在未提供参数时检索存储中的所有对象。

IDBObjectStore.index()

打开此对象存储的索引,之后可以使用该索引,例如,通过游标返回按该索引排序的记录序列。

IDBObjectStore.openCursor()

返回一个 IDBRequest 对象,并在单独的线程中返回一个新的 IDBCursorWithValue 对象。用于通过主键使用游标遍历对象存储。

IDBObjectStore.openKeyCursor()

返回一个 IDBRequest 对象,并在单独的线程中返回一个新的 IDBCursor。用于使用键遍历对象存储。

IDBObjectStore.put()

返回一个 IDBRequest 对象,并在单独的线程中创建 value结构化克隆,然后将克隆的值存储在对象存储中。这用于在事务模式为 readwrite 时更新对象存储中的现有记录。

示例

此示例展示了对象存储的多种不同用法,从在 onupgradeneeded 函数中使用 IDBObjectStore.createIndex 更新数据结构,到使用 IDBObjectStore.add 向我们的对象存储添加新项。有关完整的实际示例,请参阅我们的 待办事项通知 应用(实时查看示例)。

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 db.
  db = DBOpenRequest.result;
};

// This event handles the event whereby a new version of
// the database needs to be created Either one has not
// been created before, or a new version number has been
// submitted via the window.indexedDB.open line above
DBOpenRequest.onupgradeneeded = (event) => {
  const db = event.target.result;

  db.onerror = (event) => {
    note.appendChild(document.createElement("li")).textContent =
      "Error loading database.";
  };

  // Create an objectStore for this database

  const objectStore = db.createObjectStore("toDoList", {
    keyPath: "taskTitle",
  });

  // define what data items the objectStore will contain

  objectStore.createIndex("hours", "hours", { unique: false });
  objectStore.createIndex("minutes", "minutes", { unique: false });
  objectStore.createIndex("day", "day", { unique: false });
  objectStore.createIndex("month", "month", { unique: false });
  objectStore.createIndex("year", "year", { unique: false });

  objectStore.createIndex("notified", "notified", { unique: false });

  note.appendChild(document.createElement("li")).textContent =
    "Object store created.";
};

// Create a new item to add in to the object store
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");
// make a request to add our newItem object to the object store
const objectStoreRequest = objectStore.add(newItem[0]);

objectStoreRequest.onsuccess = (event) => {
  note.appendChild(document.createElement("li")).textContent =
    "Request successful.";
};

规范

规范
Indexed Database API 3.0
# object-store-interface

浏览器兼容性

另见