Using IndexedDB: working with data
EXPLICIT USE OF A TRANSACTION IS NECESSARY
All operations in the database should occur within a transaction!
While the creation of the database occurred in a transaction that ran “under the hood” without explicit use of the “transaction” keyword, for adding/removing/updating/retrieving data, explicit use of a transaction is required.
We generate a transaction object from the database, indicate with which object store the transaction will be associated, and specify an access mode .
Source code example for creating a transaction associated with the object store named “customers”:
- var transaction = db.transaction([“customers”], “readwrite”); // or “read”…
Transactions, when created, must have a mode set that is either readonly, readwrite or versionchange (this last mode is only for creating a new database or for modifying its schemas: i.e. changing the primary key or the indexes).
When you can, use readonly mode. Concurrent read transactions will become possible.
In the following pages, we will explain how to insert, search, remove, and update data. A final example that merges all examples together will also be shown at the end of this section.