IDBDatabase.createObjectStore()
The createObjectStore()
method of the IDBDatabase
interface creates and returns a new object store or index.
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.
Syntax
var objectStore = db.createObjectStore("toDoList", { keyPath: "taskTitle" });
Returns
IDBObjectStore
- The newly created object store.
Exceptions
This method may raise a DOMException
with a DOMError
of one of the following types:
Exception | Description |
---|---|
InvalidStateError |
Occurs if the method was not called from a versionchange transaction callback. For older WebKit browsers, you must call |
TransactionInactiveError |
Occurs if a request is made on a source database that doesn't exist (e.g. 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 bug 1176165.) |
ConstraintError |
An object store with the given name (based on case-sensitive comparison) already exists in the connected database. |
InvalidAccessError |
If autoIncrement is set to true and keyPath is either an empty string or an array containing an empty string. |
Example
// Let us open our database var request = window.indexedDB.open("toDoList", 4); // 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 //it is only implemented in recent browsers 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>'; };
Parameters
- name
- The name of the new object store to be created. Note that it is possible to create an object store with an empty name.
- optionalParameters Optional
-
An options object whose attributes are optional parameters to the method. It includes the following properties:
Attribute Description keyPath
The key path to be used by the new object store. If empty or not specified, the object store is created without a key path and uses out-of-line keys. You can also pass in an array as a keyPath
.autoIncrement
If true
, the object store has a key generator. Defaults tofalse
.Unknown parameters are ignored.
Specification
Specification | Status | Comment |
---|---|---|
Indexed Database API The definition of 'createObjectStore()' in that specification. |
Editor's Draft |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support | 23webkit 24 |
10 moz 16.0 (16.0) |
10, partial | 15 | 7.1 |
Available in workers | (Yes) | 37.0 (37.0) | ? | (Yes) | ? |
Feature | Android | Firefox Mobile (Gecko) | Firefox OS | IE Phone | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | 4.4 | 22.0 (22.0) | 1.0.1 | 10 | 22 | 8 |
Available in workers | (Yes) | 37.0 (37.0) | (Yes) | ? | (Yes) | ? |
See also
- Using IndexedDB
- Starting transactions:
IDBDatabase
- Using transactions:
IDBTransaction
- Setting a range of keys:
IDBKeyRange
- Retrieving and making changes to your data:
IDBObjectStore
- Using cursors:
IDBCursor
- Reference example: To-do Notifications (view example live.)
License
© 2016 Mozilla Contributors
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-us/docs/web/api/idbdatabase/createobjectstore