HTML and CSS Reference
2. Get a reference to the store
store = transaction.objectStore('blockbusters')
This is where we get a hard fix on the object store we want to
work with, and here we have to name the store. This could be
any of the stores we listed in our transaction. Now, with our ref-
erence to the store, we can perform our actions, like add, put,
and even get, which we'll see in a moment.
3. Save the data
request = store.put(video)
The request variable will receive a success or error event on
the object. Maybe I care whether it's been stored, maybe I
want my code to continue and let the spotty 15-year-old clerk
continue adding the pile of Bruce Awesome videos to our data-
base. That's the nice thing about the asynchronous-ness of
IndexedDB—I can let my website carry on without being inter-
rupted as the data is stored.
Now that you've stored some data, what about getting it out?
Whipping it out again
If you've followed the process for storing data, the process of
getting data back out is very similar and simple.
We still need to create a transaction. Since we're only getting
data, we could ask for that transaction to be read-only, but it
doesn't matter in this case if we use a read/write permission. We
still get the object store, and instead of adding or putting, we get:
var transaction = db.transaction(['blockbusters'],READ_WRITE),
store = transaction.objectStore('blockbusters'),
request = store.get(key);
The important part is that the key variable we pass to the get
method is compared against the keyPath we defined when the
object store was created. In our video example, we said that
blockbusters have their key based on the video title.