IDBObjectStore: phương thức clear()
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.
Phương thức clear() của giao diện IDBObjectStore tạo và trả về ngay lập tức một đối tượng IDBRequest, đồng thời xóa sạch object store này trong một luồng riêng biệt. Phương thức này dùng để xóa tất cả dữ liệu hiện có khỏi một object store.
Việc xóa một object store bao gồm loại bỏ tất cả bản ghi khỏi object store và loại bỏ tất cả bản ghi trong các index tham chiếu đến object store đó. Để chỉ xóa một số bản ghi trong store, hãy sử dụng IDBObjectStore.delete truyền vào một khóa hoặc IDBKeyRange.
Cú pháp
clear()
Tham số
Không có.
Giá trị trả về
Một đối tượng IDBRequest mà các sự kiện tiếp theo liên quan đến thao tác này sẽ được kích hoạt trên đó.
Nếu thao tác thành công, giá trị của thuộc tính result của request là undefined.
Ngoại lệ
InvalidStateErrorDOMException-
Được ném nếu object store đã bị xóa.
ReadOnlyErrorDOMException-
Được ném nếu giao dịch liên quan đến thao tác này đang ở chế độ chỉ đọc.
TransactionInactiveErrorDOMException-
Được ném nếu giao dịch của
IDBObjectStorenày không hoạt động.
Ví dụ
Trong đoạn mã sau, chúng ta mở một giao dịch read/write trên cơ sở dữ liệu và xóa tất cả dữ liệu hiện có khỏi object store bằng clear(). Để xem ví dụ hoạt động đầy đủ, hãy tham khảo ứng dụng To-do Notifications của chúng tôi (xem ví dụ trực tiếp).
// Mở cơ sở dữ liệu
const DBOpenRequest = window.indexedDB.open("toDoList", 4);
DBOpenRequest.onsuccess = (event) => {
note.appendChild(document.createElement("li")).textContent =
"Database initialized.";
// Lưu kết quả mở cơ sở dữ liệu vào biến db.
// Biến này được sử dụng nhiều ở dưới
db = DBOpenRequest.result;
// Xóa tất cả dữ liệu từ object store
clearData();
};
function clearData() {
// mở một giao dịch db read/write, sẵn sàng để xóa dữ liệu
const transaction = db.transaction(["toDoList"], "readwrite");
// báo cáo về việc giao dịch hoàn tất thành công
transaction.oncomplete = (event) => {
note.appendChild(document.createElement("li")).textContent =
"Transaction completed.";
};
transaction.onerror = (event) => {
note.appendChild(document.createElement("li")).textContent =
`Transaction not opened due to error: ${transaction.error}`;
};
// tạo một object store trên giao dịch
const objectStore = transaction.objectStore("toDoList");
// Gửi yêu cầu xóa tất cả dữ liệu khỏi object store
const objectStoreRequest = objectStore.clear();
objectStoreRequest.onsuccess = (event) => {
// báo cáo sự thành công của yêu cầu
note.appendChild(document.createElement("li")).textContent =
"Request successful.";
};
}
Đặc tả kỹ thuật
| Specification |
|---|
| Indexed Database API 3.0> # ref-for-dom-idbobjectstore-clear③> |
Tương thích trình duyệt
Xem thêm
- Sử dụng IndexedDB
- Bắt đầu giao dịch:
IDBDatabase - Sử dụng giao dịch:
IDBTransaction - Thiết lập phạm vi khóa:
IDBKeyRange - Truy xuất và thay đổi dữ liệu của bạn:
IDBObjectStore - Sử dụng con trỏ:
IDBCursor - Ví dụ tham khảo: To-do Notifications (Xem ví dụ trực tiếp).