IDBFactory
Baseline 广泛可用 *
注意:此功能在 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 |
浏览器兼容性
加载中…
另见
- 使用 IndexedDB
- 开始事务:
IDBDatabase - 使用事务:
IDBTransaction - 设置键的范围:
IDBKeyRange - 检索和修改数据:
IDBObjectStore - 使用游标:
IDBCursor - 参考示例:待办事项通知(查看实时示例)。