この記事はまだ日本語に翻訳されていません。MDN の翻訳はボランティアによって行われています。是非 MDN に登録し、私たちの力になって下さい。
The IDBObjectStore interface of the 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.
Methods
-
IDBObjectStore.add -
Returns an
IDBRequestobject, and, in a separate thread, creates a structured clone of thevalue, and stores the cloned value in the object store. -
IDBObjectStore.clear -
Creates and immediately returns an
IDBRequestobject, and clears this object store in a separate thread. -
IDBObjectStore.delete -
returns an
IDBRequestobject, and, in a separate thread, deletes the current object store. -
IDBObjectStore.get -
returns an
IDBRequestobject, and, in a separate thread, returns the object store selected by the specified key. -
IDBObjectStore.createIndex -
Creates and returns a new
IDBIndexobject in the connected database. -
IDBObjectStore.deleteIndex - Destroys the index with the specified name in the connected database.
-
IDBObjectStore.index - Opens the named index in this object store.
-
IDBObjectStore.put -
Returns an
IDBRequestobject, and, in a separate thread, creates a structured clone of thevalue, and stores the cloned value in the object store. -
IDBObjectStore.openCursor -
Returns an
IDBRequestobject, and, in a separate thread, returns a newIDBCursorWithValue
Properties
-
IDBObjectStore.indexNamesRead only - A list of the names of indexes on objects in this object store.
-
IDBObjectStore.keyPathRead only - The key path of this object store. If this attribute is null, the application must provide a key for each modification operation.
-
IDBObjectStore.nameRead only - The name of this object store.
-
IDBObjectStore.transactionRead only - The name of the transaction to which this object store belongs.
-
IDBObjectStore.autoIncrementRead only - The value of the auto increment flag for this object store.
Example
// Let us open our database
var request = window.indexedDB.open("toDoList", 4);
request.onsuccess = function(event) {
note.innerHTML += '<li>Database initialised.</li>';
// store the result of opening the database in the db variable.
db = request.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
request.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 opening the transaction
transaction.oncomplete = function(event) {
note.innerHTML += '<li>Transaction opened for task addition.</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");
// add our newItem object to the object store
var request = objectStore.add(newItem[0]);
request.onsuccess = function(event) {
note.innerHTML += '<li>New item added to database.</li>';
}
Specifications
| Specification | Status | Comment |
|---|---|---|
| Indexed Database API The definition of 'IDBObjectStore' in that specification. |
Candidate Recommendation |
Browser compatibility
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
|---|---|---|---|---|---|
| Basic support | 12 | 4.0 (2.0) | 10 | 17 | Not supported |
count() |
23 | 10.0 (10.0) | 10 | 17 | Not supported |
| Feature | Android | Firefox Mobile (Gecko) | Firefox OS | IE Phone | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Basic support | 4.4 | 6.0 (6.0) | 1.0.1 | 10 | 17 | Not supported |
count() |
4.4 | ? | 1.0.1 | 10 | 17 | Not supported |
Be careful in Chrome as it still implements the old specification along the new one. Similarly it still has the prefixed webkitIndexedDB property even if the unprefixed indexedDB is present.
See also
To learn more about various topics, see the following
- Using IndexedDB
- Starting transactions:
IDBDatabase - Setting transaction modes:
IDBTransaction - The reference application for the examples in this reference: To-do Notifications (view example live.) Not every snippet appears in this example, but every example uses the same data structure and syntax, and they will make sense in the context of this example.
Document Tags and Contributors
Contributors to this page: scottrowe, pdm, danieljohansson, n1k0, jswisher, jusio, chrisdavidmills, claudepache, teoli, mihai, soswow, grendel, nnnn20430, madarche
最終更新者:
jusio,