files/en-us/web/api/idbdatabase/createobjectstore/index.md
{{ APIRef("IndexedDB") }} {{AvailableInWorkers}}
The createObjectStore() method of the
{{domxref("IDBDatabase")}} interface creates and returns a new {{domxref("IDBObjectStore")}}.
The method takes the name of the store as well as a parameter object that lets you define important optional properties. You can use the property to uniquely identify individual objects in the store. As the property is an identifier, it should be unique to every object, and every object should have that property.
This method can be called only within a versionchange
transaction.
createObjectStore(name)
createObjectStore(name, options)
name
options {{optional_inline}}
keyPath {{optional_inline}}
keyPath.autoIncrement {{optional_inline}}
true, the object store has a key generator.
Defaults to <code>false</code>.A new {{domxref("IDBObjectStore")}}.
This method may raise a {{domxref("DOMException")}} with a name of
one of the following types:
ConstraintError {{domxref("DOMException")}}
InvalidAccessError {{domxref("DOMException")}}
autoIncrement is set to true and keyPath is
either an empty string or an array.InvalidStateError {{domxref("DOMException")}}
versionchange transaction callback.SyntaxError
keyPath option contains an invalid key path.TransactionInactiveError {{domxref("DOMException")}}
// Let us open our database
const request = window.indexedDB.open("toDoList", 4);
// This handler is called when a new version of the database
// is created, either when one has not been created before
// or when a new version number is submitted by calling
// window.indexedDB.open().
// This handler is only supported in recent browsers.
request.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.";
};
{{Specifications}}
{{Compat}}