web api interface
IDBObjectStore
Available in workers
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.
Instance properties
indexNamesRead only- : A list of the names of indexes on objects in this object store.
keyPathRead only- : The key path of this object store. If this attribute is
null, the application must provide a key for each modification operation.
- : The key path of this object store. If this attribute is
name- : The name of this object store.
transactionRead only- : The
IDBTransactionobject to which this object store belongs.
- : The
autoIncrementRead only- : The value of the auto increment flag for this object store.
Instance methods
add()- : Returns an
IDBRequestobject and, in a separate thread, creates a structured clone of thevalue, and stores the cloned value in the object store. This is for adding new records to an object store.
- : Returns an
clear()- : Creates and immediately returns an
IDBRequestobject, and clears this object store in a separate thread. This is for deleting all current records out of an object store.
- : Creates and immediately returns an
count()- : Returns an
IDBRequestobject and, in a separate thread, returns the total number of records that match the provided key orIDBKeyRange. If no arguments are provided, it returns the total number of records in the store.
- : Returns an
createIndex()- : Creates a new index during a version upgrade, returning a new
IDBIndexobject in the connected database.
- : Creates a new index during a version upgrade, returning a new
delete()- : returns an
IDBRequestobject 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.
- : returns an
deleteIndex()- : Destroys the specified index in the connected database, used during a version upgrade.
get()- : Returns an
IDBRequestobject 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.
- : Returns an
getKey()- : Returns an
IDBRequestobject and, in a separate thread, retrieves and returns the record key for the object in the object stored matching the specified parameter.
- : Returns an
getAll()- : Returns an
IDBRequestobject and, in a separate thread, retrieves all objects in the object store matching the specified parameter or all objects in the store if no parameters are given.
- : Returns an
getAllKeys()- : Returns an
IDBRequestobject and, in a separate thread, 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.
- : Returns an
getAllRecords()- : Returns an
IDBRequestobject and, in a separate thread, finds all matching records in the object store (including primary keys and values) that correspond to the given key or are in range, ifkeyis anIDBKeyRange.
- : Returns an
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.
openCursor()- : Returns an
IDBRequestobject and, in a separate thread, returns a newIDBCursorWithValueobject. Used for iterating through an object store by primary key with a cursor.
- : Returns an
openKeyCursor()- : Returns an
IDBRequestobject and, in a separate thread, returns a newIDBCursor. Used for iterating through an object store with a key.
- : Returns an
put()- : Returns an
IDBRequestobject and, in a separate thread, creates a structured clone of thevalue, and stores the cloned value in the object store. This is for updating existing records in an object store when the transaction’s mode isreadwrite.
- : Returns an
Example
This example shows a variety of different uses of object stores, from updating the data structure with createIndex inside an onupgradeneeded function, to adding a new item to our object store with add. For a full working example, see our To-do Notifications app (view example live).
// 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.";
};
Specifications
SpecificationsStandards references are available on the canonical MDN page.
Browser compatibility
Browser compatibilityCompatibility data is available on the canonical MDN page.
See also
- Using IndexedDB
- Starting transactions:
IDBDatabase - Using transactions:
IDBTransaction - Setting a range of keys:
IDBKeyRange - Using cursors:
IDBCursor - Reference example: To-do Notifications (View the example live).