Game Development Reference
In-Depth Information
To create an index on a data store, we must specify our intentions during the creation
of the data store (inside the onupgradeneeded callback when the store is first cre-
ated, or inside a transaction mode versionchange). The code for this is as follows:
request.onupgradeneeded = function(event) {
var settings = {
keyPath: "myKey",
autoIncrement: true
};
db = event.target.result;
store = db.createObjectStore("myDataStore",
settings);
var indexSettings = {
unique: true
};
store.createIndex("taskIndex", "task",
indexSettings);
};
In the preceding example, we create an index for the task attribute of our objects.
The name of this index can be anything we want, and commonly is the same name
as the object property to which it applies. In our case, we simply named it taskIndex.
The possible settings we can configure are as follows:
• unique - if true, an object being stored with a duplicate value for the same
attribute is rejected
• multiEntry - if true, and the indexed attribute is an array, each element
will be indexed
Note
Note that zero or more indexes can be created for a data store. Just
like any other database system, indexing your database/data store
can really boost the performance of the storage container. However,
just adding indexes for the fun it provides is not a good idea, as the
size of your data store will grow accordingly. A good data store design
Search WWH ::




Custom Search