IDBDatabase.createObjectStore
IDBDatabase
インターフェイスのcreateObjectStore()
メソッドは、新しいオブジェクトストアやインデックスを生成して返します。
このメソッドは、ストアの名前をとるだけでなく、重要なオプションプロパティを定義するためのオプションオブジェクトもとります。ストアの個々のオブジェクトを一意にするために、プロパティを使用できます。 プロパティが識別子ならば、それはすべてのオブジェクトで一意であり、すべてのオブジェクトはそのプロパティを持つべきです。
WebKitブラウザでは、オブジェクトストアやインデックスを生成できるようになる前に、IDBVersionChangeRequest.setVersion
メソッドを呼び出さなければなりません。
構文
IDBDatabase.createObjectStore(name); IDBDatabase.createObjectStore(name, options);
戻り値
IDBObjectStore
(en-US)- 新しく生成されたオブジェクトストア。
例外
このメソッドは、次の型の1つを含むDOMError
を伴うDOMException
を発生させるかもしれません。
Exception | Description |
---|---|
InvalidStateError |
このメソッドがversionchange トランザクションのコールバックとして呼び出されなかった。WebKitブラウザでは、はじめに を呼び出さなければならない。既に削除されたか取り除かれたオブジェクトを要求した場合も発生する。 |
ConstraintError |
与えられた名前のオブジェクトストア(based on case-sensitive comparison) が接続中のデータベースに既に存在する。 |
InvalidAccessError |
autoIncrement がtrueに設定されていて、keyPathが空文字か空文字を含む配列の場合。 |
例
// 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>';
};
パラメーター
- name
- 新しく作られるオブジェクトストア名。
- optionalParameters
-
オプション。メソッドのオプションパラメーターとなる属性を持つオプションオブジェクト。これは次のプロパティを持つ。
Attribute Description keyPath
新しいオブジェクトストアで使用されるkey path。空や特定されていない場合、オブジェクトストアはKey Pathなしで生成されて、out-of-line keysが使用される。 autoIncrement
trueだった場合、オブジェクトストアは
key generatorを持つ。既定値はfalse。
未知のパラメーターは無視される。
仕様
Specification | Status | Comment |
---|---|---|
Indexed Database API 2.0 createObjectStore() の定義 |
勧告 |
ブラウザ実装状況
BCD tables only load in the browser
関連項目
- Using IndexedDB
- Starting transactions:
IDBDatabase
- Using transactions:
IDBTransaction
- Setting a range of keys:
IDBKeyRange
(en-US) - Retrieving and making changes to your data:
IDBObjectStore
(en-US) - Using cursors:
IDBCursor
- Reference example: To-do Notifications (view example live.)