Training Guide: Programming in HTML5 with JavaScript and CSS3 Ebook

(Nora) #1

Lesson 2: Working with IndexedDB CHAPTER 16 599


index.openCursor(IDBKeyRange.lowerBound('Defoe', true));

results: Herman Melville, Mark Twain, Jules Verne
■■only ou can restrict to a specific key value rather than to a range by using the only Y
method. The following is a parameter of the only method.
■■value he specific key value against which to match.T
authors content: Daniel Defoe, Herman Melville, Mark Twain, Jules Verne
index.openCursor(IDBKeyRange.only('Twain'));

results: Mark Twain

Dropping a database


The IDBFactory object that is referenced by the indexedDB object contains a deleteDatabase
method that removes an existing database. This method takes a name parameter and returns
a request object immediately while asynchronously attempting to drop the database, as
shown in the following example.
var dropRequest = indexedDB.deleteDatabase('Library');
dropRequest.onsuccess = function(response) { // success! };
dropRequest.onerror = function (response) { // display error };

If the database doesn’t exist, no action is taken. If another connection to the database is
open, a blocked event will be fired by the request object.

Lesson summary


■■IndexedDB is a key-based object database available in most current browsers.
■■Databases contain object stores, which are somewhat equivalent to table structures in
a relational database.
■■Each object store has a designated key path that identifies its key.
■■Instead of a property on the object value, a key can be created through a key genera-
tor such as autoIncrement, which creates a distinct numeric identifier as new records
are added.
■■Indexes can be created for properties other than the key that might be commonly used
for sorting or filtering.
■■All operations are performed through transactions, which can be read-only or read/
write. Read-only operations can run concurrently.
■■An object store’s add method can be used only for adding new records, but its put
method can be used for new or existing records. Its delete method removes records.
■■Cursors find records in an object store and can be created on either the object store
itself or one of its indexes.
Free download pdf