IDBOpenDBRequest

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Note: This feature is available in Web Workers.

Giao diện IDBOpenDBRequest của IndexedDB API cung cấp quyền truy cập vào kết quả của các yêu cầu mở hoặc xóa cơ sở dữ liệu (được thực hiện bằng IDBFactory.openIDBFactory.deleteDatabase), sử dụng các thuộc tính trình xử lý sự kiện cụ thể.

EventTarget IDBRequest IDBOpenDBRequest

Thuộc tính phiên bản

Cũng kế thừa các thuộc tính từ các thực thể cha IDBRequestEventTarget.

Phương thức phiên bản

Không có phương thức, nhưng kế thừa các phương thức từ các thực thể cha IDBRequestEventTarget.

Sự kiện

Các sự kiện được định nghĩa trên giao diện cha, IDBRequestEventTarget, cũng có thể được gửi trên các đối tượng IDBOpenDBRequest.

Lắng nghe các sự kiện chung và cụ thể này bằng addEventListener() hoặc bằng cách gán trình lắng nghe sự kiện cho thuộc tính oneventname của giao diện này.

Các sự kiện cụ thể cho giao diện này là:

blocked

Kích hoạt khi một kết nối mở đến cơ sở dữ liệu đang chặn giao dịch versionchange trên cùng cơ sở dữ liệu. Cũng có sẵn qua thuộc tính onblocked.

upgradeneeded

Kích hoạt khi có nỗ lực mở cơ sở dữ liệu với số phiên bản cao hơn phiên bản hiện tại của nó. Cũng có sẵn qua thuộc tính onupgradeneeded.

Ví dụ

Trong ví dụ sau, bạn có thể thấy trình xử lý onupgradeneeded được sử dụng để cập nhật cấu trúc cơ sở dữ liệu nếu cơ sở dữ liệu có số phiên bản cao hơn được tải. Để xem ví dụ đầy đủ, xem ứng dụng To-do Notifications (xem ví dụ trực tiếp.)

js
let db;

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

// these event handlers act on the database being opened.
DBOpenRequest.onerror = (event) => {
  note.appendChild(document.createElement("li")).textContent =
    "Error loading database.";
};

DBOpenRequest.onsuccess = (event) => {
  note.appendChild(document.createElement("li")).textContent =
    "Database initialized.";

  // store the result of opening the database in the db
  // variable. This is used a lot below
  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
// it is only implemented in recent browsers
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
  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 });
};

Thông số kỹ thuật

Specification
Indexed Database API 3.0
# idbopendbrequest

Tương thích trình duyệt

Xem thêm