IDBDatabase
注意:此功能在 Web Workers 中可用。
IndexedDB API 的 IDBDatabase
接口提供了对数据库的连接;您可以使用 IDBDatabase
对象在数据库上开启事务,然后创建、操作和删除数据库中的对象(数据)。该接口提供了获取和管理数据库版本的唯一途径。
注意:在 IndexedDB 中,您所做的一切始终发生在事务的上下文中,代表与数据库数据的交互。IndexedDB 中的所有对象——包括对象存储、索引和游标——都与特定事务相关联。因此,您无法在事务之外执行命令、访问数据或打开任何内容。
实例属性
IDBDatabase.name
只读-
一个包含已连接数据库名称的字符串。
IDBDatabase.version
只读-
一个包含已连接数据库版本的 64 位整数。当数据库首次创建时,此属性为空字符串。
IDBDatabase.objectStoreNames
只读-
一个
DOMStringList
,其中包含已连接数据库中当前所有对象存储名称的列表。
实例方法
继承自:EventTarget
IDBDatabase.close()
-
立即返回,并在单独的线程中关闭与数据库的连接。
IDBDatabase.createObjectStore()
-
创建并返回一个新的对象存储或索引。
IDBDatabase.deleteObjectStore()
-
销毁已连接数据库中指定名称的对象存储,以及任何引用它的索引。
IDBDatabase.transaction()
-
立即返回一个事务对象(
IDBTransaction
),其中包含IDBTransaction.objectStore
方法,您可以使用该方法访问对象存储。在单独的线程中运行。
事件
使用 addEventListener()
或通过将事件监听器分配给此接口的 oneventname
属性来监听这些事件。
close
-
当数据库连接意外关闭时触发的事件。
versionchange
-
当数据库结构更改被请求时触发的事件。
通过从 IDBTransaction
冒泡的事件,IDBDatabase
可以使用以下事件:
示例
在以下代码片段中,我们异步打开数据库(IDBFactory
),处理成功和错误情况,并在需要升级时(IDBDatabase
)创建一个新的对象存储。有关完整的可运行示例,请参阅我们的待办事项通知应用程序(在线查看示例)。
// Let us open our database
const DBOpenRequest = window.indexedDB.open("toDoList", 4);
// these two event handlers act on the IDBDatabase object,
// when the database is opened successfully, or not
DBOpenRequest.onerror = (event) => {
note.appendChild(document.createElement("li")).textContent =
"Error loading database.";
};
DBOpenRequest.onsuccess = (event) => {
node.appendChild(document.createElement("li")).textContent =
"Database initialized.";
// store the result of opening the database in the db
// variable. This is used a lot later on
db = DBOpenRequest.result;
// Run the displayData() function to populate the task
// list with all the to-do list data already in the IDB
displayData();
};
// 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 using
// IDBDatabase.createObjectStore
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.";
};
下一行代码将打开数据库上的事务,然后打开一个对象存储,以便我们随后能够操作其中的数据。
const objectStore = db
.transaction("toDoList", "readwrite")
.objectStore("toDoList");
规范
规范 |
---|
Indexed Database API 3.0 # database-interface |
浏览器兼容性
加载中…
另见
- 使用 IndexedDB
- 使用事务:
IDBTransaction
- 设置键的范围:
IDBKeyRange
- 检索和修改数据:
IDBObjectStore
- 使用游标:
IDBCursor
- 参考示例:待办事项通知(查看实时示例)。