HTML and CSS Reference
In-Depth Information
// Store the piece types
var typeStore = db.createObjectStore
("pieceType", { keyPath: "name" });
for (var i in pieceTypes){
typeStore.add(pieceTypes[i]);
}
// Create the piece data store (you'll add
// the data later)
var pieceStore = db.createObjectStore
("piece", { autoIncrement: true });
pieceStore.createIndex
("piecePosition", "pos", { unique: true });
}
The configureDatabase() function will be called if the database does not exists or if it is not the
current version. For version changes, you can get the current version by using the db.version property and then
make the necessary adjustments. Also, the event object passed to the onupgradeneeded event handler will have
the e.oldVersion and e.newVersion properties. To simplify things in this project, you'll simply remove all object
stores and rebuild the database from scratch. This will wipeout all existing data. That is fine for this example but in
most cases you'll need to preserve the data where possible.
Caution
The objectStoreNames property of the database object contains a list of the names of all the object stores
that have been created. To remove all of the existing object stores, each of the names in this list is passed to the
deleteObjectStore() method.
Initially, you'll create two data stores using the createObjectStore() method:
pieceType - contains an object for each type of piece such as pawn, rook, or king.
piece - contains an object for each piece, 16 black and 16 white.
Specifying the Object Key
When creating an object store, you must specify a name for the store when calling the createObjectStore()
method. You can also specify one or more optional parameters. Only two are supported:
keypath - this is specified as a collection of property names. If you're using a single
property, you can specify this as a string rather than a collection of strings. This defines
the object property(ies) that will be used as the key. If no keypath is specified, the key
must be defined out-of-line using a key generator or providing the key as explained
below.
autoIncrement - If true, this indicates that the keys are sequentially assigned by the
object store.
 
 
Search WWH ::




Custom Search