IDBFactory

Baseline 广泛可用 *

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

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

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

IDBFactory 接口是 IndexedDB API 的一部分,它允许应用程序异步访问索引数据库。实现该接口的对象是 window.indexedDB。您使用该对象打开(即创建和访问)以及删除数据库,而不是直接使用 IDBFactory

实例方法

IDBFactory.open()

请求打开 数据库连接

IDBFactory.deleteDatabase()

请求删除数据库。

IDBFactory.cmp()

比较两个键,并返回一个指示哪个键值更大的结果。

IDBFactory.databases()

返回一个 Promise,该 Promise 会以一个包含所有可用数据库(包括它们的名称和版本)的数组进行 fulfill。

示例

在下面的代码片段中,我们发起了打开数据库的请求,并包含了成功和错误情况的处理程序。有关完整的可运行示例,请参阅我们的 待办事项通知 应用(实时查看示例)。

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

// these two event handlers act on the database being opened successfully, or not
DBOpenRequest.onerror = (event) => {
  console.error("Error loading database.");
};

DBOpenRequest.onsuccess = (event) => {
  console.info("Database initialized.");

  // store the result of opening the database in the db variable. This is used a lot later on, for opening transactions and suchlike.
  db = DBOpenRequest.result;
};

规范

规范
Indexed Database API 3.0
# factory-interface

浏览器兼容性

另见