IDBDatabase

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

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

IndexedDB API 的 IDBDatabase 介面提供了對資料庫的連線;您可以使用 IDBDatabase 物件在資料庫上開啟事務,然後建立、操作和刪除資料庫中的物件(資料)。該介面提供了獲取和管理資料庫版本的唯一途徑。

注意:在 IndexedDB 中,您所做的一切始終發生在事務的上下文中,代表與資料庫資料的互動。IndexedDB 中的所有物件——包括物件儲存、索引和遊標——都與特定事務相關聯。因此,您無法在事務之外執行命令、訪問資料或開啟任何內容。

EventTarget IDBDatabase

例項屬性

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 可以使用以下事件:

IDBTransaction abort

事務中止時觸發的事件。

IDBTransaction error

請求返回錯誤並且事件冒泡到連線物件時觸發的事件。

示例

在以下程式碼片段中,我們非同步開啟資料庫(IDBFactory),處理成功和錯誤情況,並在需要升級時(IDBDatabase)建立一個新的物件儲存。有關完整的可執行示例,請參閱我們的待辦事項通知應用程式(線上檢視示例)。

js
// 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.";
};

下一行程式碼將開啟資料庫上的事務,然後開啟一個物件儲存,以便我們隨後能夠操作其中的資料。

js
const objectStore = db
  .transaction("toDoList", "readwrite")
  .objectStore("toDoList");

規範

規範
Indexed Database API 3.0
# database-interface

瀏覽器相容性

另見