The createObjectStore()
method of the IDBDatabase
interface creates and returns a new 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)
This method may raise a DOMException
with a name
of one of the following types:
-
InvalidStateError
DOMException
-
Thrown if the method was not called from a versionchange
transaction callback.
-
TransactionInactiveError
DOMException
-
Thrown if a request is made on a source database that does not exist (for example, when the database has been deleted or removed). In Firefox previous to version 41, an InvalidStateError
was raised in this case as well, which was misleading; this has now been fixed (see Webkit bug 1176165).
-
ConstraintError
DOMException
-
Thrown if an object store with the given name (based on a case-sensitive comparison) already exists in the connected database.
-
InvalidAccessError
DOMException
-
Thrown if autoIncrement
is set to true and keyPath
is either an empty string or an array containing an empty string.
const request = window.indexedDB.open("toDoList", 4);
request.onupgradeneeded = (event) => {
const db = event.target.result;
db.onerror = (event) => {
note.innerHTML += "<li>Error loading database.</li>";
};
const objectStore = db.createObjectStore("toDoList", {
keyPath: "taskTitle",
});
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>";
};