Storing Data to the Object Store
Explore the methods to store data in IndexedDB object stores by creating transactions, handling add operations, and listening to success or error events. Understand how transactions ensure data integrity and how to detect their completion to manage efficient client-side storage.
To add data to a data store in IndexedDB, we need to follow these steps:
Open a database.
Create an object store.
Start a transaction.
Add the data to the object store using the
add()method.
Till now, we know how to open a database and create an object store. Let’s see how to start a transaction and add data to the object store.
Start a transaction
To add data to a database, we need to create transactions. A transaction is a unit of work that makes changes to the database. To create a transaction, we should use the syntax below:
db.transaction(store[, type]);
We can use the transaction() method of the IDBDatabase object. The method accepts as arguments an array with the list of stores and the
The code below demonstrates how to create a transaction:
In the code above:
Line 1: We open the
School_add_data_demodatabase.Lines 3–8: We add a ...