IDBObjectStore
接口在
IndexedDB API
represents an object store in a database. Records within an object store are sorted according to their keys. This sorting enables fast insertion, look-up, and ordered retrieval.
IDBObjectStore.indexNames
只读
IDBObjectStore.keyPath
只读
null
, the application must provide a key for each modification operation.
IDBObjectStore.name
The name of this object store.
IDBObjectStore.transaction
只读
IDBTransaction
object to which this object store belongs.
IDBObjectStore.autoIncrement
只读
The value of the auto increment flag for this object store.
IDBObjectStore.add()
IDBRequest
object, and, in a separate thread, creates a
structured clone
的
value
, and stores the cloned value in the object store. This is for adding new records to an object store.
IDBObjectStore.clear()
IDBRequest
object, and clears this object store in a separate thread. This is for deleting all current records out of an object store.
IDBObjectStore.count()
IDBRequest
object, and, in a separate thread, returns the total number of records that match the provided key or
IDBKeyRange
. If no arguments are provided, it returns the total number of records in the store.
IDBObjectStore.createIndex()
IDBIndex
object in the connected database.
IDBObjectStore.delete()
IDBRequest
object, and, in a separate thread, deletes the store object selected by the specified key. This is for deleting individual records out of an object store.
IDBObjectStore.deleteIndex()
Destroys the specified index in the connected database, used during a version upgrade.
IDBObjectStore.get()
IDBRequest
object, and, in a separate thread, returns the store object store selected by the specified key. This is for retrieving specific records from an object store.
IDBObjectStore.getKey()
IDBRequest
object, and, in a separate thread retrieves and returns the record key for the object in the object stored matching the specified parameter.
IDBObjectStore.getAll()
IDBRequest
object retrieves all objects in the object store matching the specified parameter or all objects in the store if no parameters are given.
IDBObjectStore.getAllKeys()
IDBRequest
object retrieves record keys for all objects in the object store matching the specified parameter or all objects in the store if no parameters are given.
IDBObjectStore.index()
Opens an index from this object store after which it can, for example, be used to return a sequence of records sorted by that index using a cursor.
IDBObjectStore.openCursor()
IDBRequest
object, and, in a separate thread, returns a new
IDBCursorWithValue
object. Used for iterating through an object store by primary key with a cursor.
IDBObjectStore.openKeyCursor()
IDBRequest
object, and, in a separate thread, returns a new
IDBCursor
. Used for iterating through an object store with a key.
IDBObjectStore.put()
IDBRequest
object, and, in a separate thread, creates a
structured clone
的
value
, and stores the cloned value in the object store. This is for updating existing records in an object store when the transaction's mode is
readwrite
.
This example shows a variety of different uses of object stores, from updating the data structure with
IDBObjectStore.createIndex
inside an
onupgradeneeded
function, to adding a new item to our object store with
IDBObjectStore.add
. For a full working example, see our
待办通知
app (
实时查看范例
)。
// Let us open our database
var DBOpenRequest = window.indexedDB.open("toDoList", 4);
DBOpenRequest.onsuccess = function(event) {
note.innerHTML += '<li>Database initialised.</li>';
// 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 = function(event) {
var db = event.target.result;
db.onerror = function(event) {
note.innerHTML += '<li>Error loading database.</li>';
};
// Create an objectStore for this database
var 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.innerHTML += '<li>Object store created.</li>';
};
// Create a new item to add in to the object store
var 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
var transaction = db.transaction(["toDoList"], "readwrite");
// report on the success of the transaction completing, when everything is done
transaction.oncomplete = function(event) {
note.innerHTML += '<li>Transaction completed.</li>';
};
transaction.onerror = function(event) {
note.innerHTML += '<li>Transaction not opened due to error. Duplicate items not allowed.</li>';
};
// create an object store on the transaction
var objectStore = transaction.objectStore("toDoList");
// make a request to add our newItem object to the object store
var objectStoreRequest = objectStore.add(newItem[0]);
objectStoreRequest.onsuccess = function(event) {
note.innerHTML += '<li>Request successful .</li>';
}
| 规范 | 状态 | 注释 |
|---|---|---|
|
索引数据库 API 2.0
在该规范中的 IDBObjectStore 定义。 |
推荐 | |
|
索引数据库 API 草案
在该规范中的 IDBObjectStore 定义。 |
推荐 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
IDBObjectStore
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 |
WebView Android
Yes
|
Chrome Android
25
|
Firefox Android 22 | Opera Android 14 | Safari iOS 8 |
Samsung Internet Android
1.5
|
add
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 |
WebView Android
Yes
|
Chrome Android
25
|
Firefox Android 22 | Opera Android 14 | Safari iOS 8 |
Samsung Internet Android
1.5
|
autoIncrement
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 |
WebView Android
Yes
|
Chrome Android
25
|
Firefox Android 22 | Opera Android 14 | Safari iOS 8 |
Samsung Internet Android
1.5
|
clear
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 |
WebView Android
Yes
|
Chrome Android
25
|
Firefox Android 22 | Opera Android 14 | Safari iOS 8 |
Samsung Internet Android
1.5
|
count
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 |
WebView Android
Yes
|
Chrome Android
25
|
Firefox Android 22 | Opera Android 14 | Safari iOS 8 |
Samsung Internet Android
1.5
|
createIndex
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 |
WebView Android
Yes
|
Chrome Android
25
|
Firefox Android 22 | Opera Android 14 | Safari iOS 8 |
Samsung Internet Android
1.5
|
delete
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 |
WebView Android
Yes
|
Chrome Android
25
|
Firefox Android 22 | Opera Android 14 | Safari iOS 8 |
Samsung Internet Android
1.5
|
deleteIndex
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 |
WebView Android
Yes
|
Chrome Android
25
|
Firefox Android 22 | Opera Android 14 | Safari iOS 8 |
Samsung Internet Android
1.5
|
get
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 |
WebView Android
Yes
|
Chrome Android
25
|
Firefox Android 22 | Opera Android 14 | Safari iOS 8 |
Samsung Internet Android
1.5
|
getAll
|
Chrome 48 | Edge ≤79 | Firefox 44 | IE ? | Opera 35 | Safari 10.1 | WebView Android 48 | Chrome Android 48 | Firefox Android 48 | Opera Android 35 | Safari iOS 10.3 | Samsung Internet Android 5.0 |
getAllKeys
|
Chrome 48 | Edge ≤79 | Firefox 44 | IE ? | Opera 35 | Safari 10.1 | WebView Android 48 | Chrome Android 48 | Firefox Android 48 | Opera Android 35 | Safari iOS 10.3 | Samsung Internet Android 5.0 |
getKey
|
Chrome 48 | Edge ≤79 | Firefox 51 | IE ? | Opera 45 | Safari 10.1 | WebView Android 48 | Chrome Android 48 | Firefox Android 58 | Opera Android 43 | Safari iOS 10.3 | Samsung Internet Android 5.0 |
index
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 |
WebView Android
Yes
|
Chrome Android
25
|
Firefox Android 22 | Opera Android 14 | Safari iOS 8 |
Samsung Internet Android
1.5
|
indexNames
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 |
WebView Android
Yes
|
Chrome Android
25
|
Firefox Android 22 | Opera Android 14 | Safari iOS 8 |
Samsung Internet Android
1.5
|
keyPath
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 |
WebView Android
Yes
|
Chrome Android
25
|
Firefox Android 22 | Opera Android 14 | Safari iOS 8 |
Samsung Internet Android
1.5
|
名称
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 |
WebView Android
Yes
|
Chrome Android
25
|
Firefox Android 22 | Opera Android 14 | Safari iOS 8 |
Samsung Internet Android
1.5
|
openCursor
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 |
WebView Android
Yes
|
Chrome Android
25
|
Firefox Android 22 | Opera Android 14 | Safari iOS 8 |
Samsung Internet Android
1.5
|
openKeyCursor
|
Chrome
24
|
Edge 12 |
Firefox
44
Disabled
|
IE 部分支持 10 | Opera 15 | Safari 7 |
WebView Android
Yes
|
Chrome Android
25
|
Firefox Android 22 | Opera Android 14 | Safari iOS 8 |
Samsung Internet Android
1.5
|
put
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 |
WebView Android
Yes
|
Chrome Android
25
|
Firefox Android 22 | Opera Android 14 | Safari iOS 8 |
Samsung Internet Android
1.5
|
transaction
|
Chrome
24
|
Edge 12 |
Firefox
16
|
IE 部分支持 10 | Opera 15 | Safari 7 |
WebView Android
Yes
|
Chrome Android
25
|
Firefox Android 22 | Opera Android 14 | Safari iOS 8 |
Samsung Internet Android
1.5
|
| Available in workers | Chrome Yes | Edge ≤18 | Firefox 37 | IE ? | Opera Yes | Safari ? | WebView Android Yes | Chrome Android Yes | Firefox Android 37 | Opera Android Yes | Safari iOS ? | Samsung Internet Android Yes |
完整支持
部分支持
兼容性未知
用户必须明确启用此特征。
要求使用供应商前缀或不同名称。
IDBDatabase
IDBTransaction
IDBKeyRange
IDBObjectStore
IDBCursor
IDBObjectStore
IDBCursor
IDBCursorSync
IDBCursorWithValue
IDBDatabase
IDBDatabaseException
IDBDatabaseSync
IDBEnvironment
IDBEnvironmentSync
IDBFactory
IDBFactorySync
IDBIndex
IDBIndexSync
IDBKeyRange
IDBObjectStoreSync
IDBOpenDBRequest
IDBRequest
IDBTransaction
IDBTransactionSync
IDBVersionChangeEvent
IDBVersionChangeRequest