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 - 參考示例:待辦事項通知(檢視即時示例)。