Game Development Reference
In-Depth Information
The first step to persisting objects in IndexedDB is to start a transaction. This is
done by requesting a transaction object from the database we have opened earlier.
A transaction is always related to a particular data store. Also, when requesting a
transaction, we can specify what type of transaction we'd like to start. The possible
types of transactions in IndexedDB are as follows:
readwrite
This transaction mode allows for objects to be stored into the data store, retrieved
from it, updated, and deleted. In other words, readwrite mode allows for full CRUD
functionality.
readonly
This transaction mode is similar to readwrite, but clearly restricts the interactions with
the data store to only reading. Anything that would modify the data store is not al-
lowed, so any attempt to create a new record (in other words, persisting a new object
into the data store), update an existing object (in other words, trying to save an ob-
ject that was already in the data store), or delete an object from the data store will
result in the transaction failing, and an exception being raised.
versionchange
This transaction mode allows us to create or modify an object store or indexes used
in the data store. Within a transaction of this mode, we can perform any action or
operation, including modifying the structure of the database.
Getting elements
Simply storing data into a black box is not at all useful if we're not able to retrieve that
data at a later point in time. With IndexedDB, this can be done in several different
ways. More commonly, the data store where we persist the data is set up with one
or more indexes, which keep the objects organized by a particular field. Again, for
those accustomed to relational databases, this would be similar to indexing/applying
a key to a particular table column. If we want to get to an object, we can query it by
its unique ID, or we can search the data store for objects that fit particular character-
istics, which we can do through indexed values of that object.
Search WWH ::




Custom Search