IDBObjectStore
Baseline 廣泛可用 *
注意:此功能在 Web Workers 中可用。
IndexedDB API 的 IDBObjectStore 介面代表資料庫中的一個物件儲存。物件儲存中的記錄根據它們的鍵進行排序。這種排序支援快速插入、查詢和有序檢索。
例項屬性
IDBObjectStore.indexNames只讀-
此物件儲存中物件上的索引名稱列表。
IDBObjectStore.keyPath只讀-
此物件儲存的鍵路徑。如果此屬性為
null,則應用程式必須為每個修改操作提供一個鍵。 IDBObjectStore.name-
此物件儲存的名稱。
IDBObjectStore.transaction只讀-
此物件儲存所屬的
IDBTransaction物件。 IDBObjectStore.autoIncrement只讀-
此物件儲存的自動遞增標誌的值。
例項方法
IDBObjectStore.add()-
返回一個
IDBRequest物件,並在單獨的執行緒中建立value的結構化克隆,然後將克隆的值儲存在物件儲存中。這用於向物件儲存新增新記錄。 IDBObjectStore.clear()-
建立並立即返回一個
IDBRequest物件,並在單獨的執行緒中清空此物件儲存。這用於刪除物件儲存中的所有當前記錄。 IDBObjectStore.count()-
返回一個
IDBRequest物件,並在單獨的執行緒中返回與提供的鍵或IDBKeyRange匹配的記錄總數。如果不提供引數,則返回儲存中的記錄總數。 IDBObjectStore.createIndex()-
在版本升級期間建立新索引,並在連線的資料庫中返回一個新的
IDBIndex物件。 IDBObjectStore.delete()-
返回一個
IDBRequest物件,並在單獨的執行緒中刪除由指定鍵選擇的儲存物件。這用於從物件儲存中刪除單個記錄。 IDBObjectStore.deleteIndex()-
銷燬連線的資料庫中的指定索引,用於版本升級期間。
IDBObjectStore.get()-
返回一個
IDBRequest物件,並在單獨的執行緒中返回由指定鍵選擇的儲存物件。這用於從物件儲存中檢索特定記錄。 IDBObjectStore.getKey()-
返回一個
IDBRequest物件,並在單獨的執行緒中檢索並返回物件儲存中與指定引數匹配的物件的記錄鍵。 IDBObjectStore.getAll()-
返回一個
IDBRequest物件,檢索物件儲存中與指定引數匹配的所有物件,或者在未提供引數時檢索儲存中的所有物件。 IDBObjectStore.getAllKeys()-
返回一個
IDBRequest物件,檢索物件儲存中與指定引數匹配的所有物件的記錄鍵,或者在未提供引數時檢索儲存中的所有物件。 IDBObjectStore.index()-
開啟此物件儲存的索引,之後可以使用該索引,例如,透過遊標返回按該索引排序的記錄序列。
IDBObjectStore.openCursor()-
返回一個
IDBRequest物件,並在單獨的執行緒中返回一個新的IDBCursorWithValue物件。用於透過主鍵使用遊標遍歷物件儲存。 IDBObjectStore.openKeyCursor()-
返回一個
IDBRequest物件,並在單獨的執行緒中返回一個新的IDBCursor。用於使用鍵遍歷物件儲存。 IDBObjectStore.put()-
返回一個
IDBRequest物件,並在單獨的執行緒中建立value的結構化克隆,然後將克隆的值儲存在物件儲存中。這用於在事務模式為readwrite時更新物件儲存中的現有記錄。
示例
此示例展示了物件儲存的多種不同用法,從在 onupgradeneeded 函式中使用 IDBObjectStore.createIndex 更新資料結構,到使用 IDBObjectStore.add 向我們的物件儲存新增新項。有關完整的實際示例,請參閱我們的 待辦事項通知 應用(即時檢視示例)。
// Let us open our database
const DBOpenRequest = window.indexedDB.open("toDoList", 4);
DBOpenRequest.onsuccess = (event) => {
note.appendChild(document.createElement("li")).textContent =
"Database initialized.";
// store the result of opening the database in db.
db = DBOpenRequest.result;
};
// 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
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.";
};
// Create a new item to add in to the object store
const newItem = [
{
taskTitle: "Walk dog",
hours: 19,
minutes: 30,
day: 24,
month: "December",
year: 2013,
notified: "no",
},
];
// open a read/write db transaction, ready for adding the data
const transaction = db.transaction(["toDoList"], "readwrite");
// report on the success of the transaction completing, when everything is done
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. Duplicate items not allowed.";
};
// create an object store on the transaction
const objectStore = transaction.objectStore("toDoList");
// make a request to add our newItem object to the object store
const objectStoreRequest = objectStore.add(newItem[0]);
objectStoreRequest.onsuccess = (event) => {
note.appendChild(document.createElement("li")).textContent =
"Request successful.";
};
規範
| 規範 |
|---|
| Indexed Database API 3.0 # object-store-interface |
瀏覽器相容性
載入中…
另見
- 使用 IndexedDB
- 開始事務:
IDBDatabase - 使用事務:
IDBTransaction - 設定鍵的範圍:
IDBKeyRange - 使用遊標:
IDBCursor - 參考示例:待辦事項通知(檢視即時示例)。