files/en-us/web/api/idbobjectstore/createindex/index.md
{{ APIRef("IndexedDB") }} {{AvailableInWorkers}}
The createIndex() method of the
{{domxref("IDBObjectStore")}} interface creates and returns a new
{{domxref("IDBIndex")}} object in the connected database. It creates a new
field/column defining a new data point for each database record to contain.
Bear in mind that IndexedDB indexes can contain any JavaScript data type; IndexedDB uses the structured clone algorithm to serialize stored objects, which allows for storage of simple and complex objects.
Note that this method must be called only from a VersionChange transaction
mode callback.
createIndex(indexName, keyPath)
createIndex(indexName, keyPath, options)
indexName
keyPath
keyPath, and also to pass in a sequence (array) as a keyPath.options {{optional_inline}}
unique
true, the index will not allow duplicate values for a single key. Defaults to false.multiEntry
true, the index will add an entry in the index for each array element when the keyPath resolves to an array.
If false, it will add one single entry containing the array. Defaults to false.locale {{non-standard_inline}} {{deprecated_inline}}
string: A string containing a specific locale code, e.g., en-US, or pl.auto: The platform default locale will be used (may be changed by user agent settings).null or undefined: If no locale is specified, normal JavaScript sorting will be used — not locale-aware.An {{domxref("IDBIndex")}} object: the newly created index.
This method may raise a {{domxref("DOMException")}} of one of the following types:
ConstraintError {{domxref("DOMException")}}
InvalidAccessError {{domxref("DOMException")}}
multiEntry is set to true in the objectParameters object.InvalidStateError {{domxref("DOMException")}}
versionchange transaction mode callback, i.e., from inside an {{domxref("IDBOpenDBRequest.upgradeneeded_event", "onupgradeneeded")}} handler.SyntaxError {{domxref("DOMException")}}
keyPath is not a valid key path.TransactionInactiveError {{domxref("DOMException")}}
InvalidStateError was raised in
this case as well, which was misleading; this has now been fixed (see
Firefox bug 1176165.)In the following example you can see
the {{domxref("IDBOpenDBRequest.upgradeneeded_event", "onupgradeneeded")}} handler being used to update the
database structure if a database with a higher version number is loaded.
createIndex() is used to create new indexes on the object store. For a
full working example, see our To-do Notifications app (view example live).
let db;
// Let us open our database
const DBOpenRequest = window.indexedDB.open("toDoList", 4);
// Two event handlers for opening the database.
DBOpenRequest.onerror = (event) => {
note.appendChild(document.createElement("li")).textContent =
"Error loading database.";
};
DBOpenRequest.onsuccess = (event) => {
note.appendChild(document.createElement("li")).textContent =
"Database initialized.";
// store the result of opening the database in the db variable.
// This is used a lot below.
db = request.result;
// Run the displayData() function to populate the task list with
// all the to-do list data already in the IDB
displayData();
};
// This handler fires when a new database is created and indicates
// either that one has not been created before, or a new version
// was submitted with window.indexedDB.open(). (See above.)
// It is only implemented in recent browsers.
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 });
};
{{Specifications}}
{{Compat}}