Accessing IndexedDB

Asynchronous nature

IndexedDB API is asynchronous, and once the requested operation is completed, its corresponding DOM Event is triggered. In other words, the triggered event type can help us identify whether the performed operation is successful or not.

IndexedDB supports transactionsThis is a group of operations that ensures the accuracy, completeness, and consistency of data. that ensure reliability. In a transaction, if one of the operations fails, then the applied operations are reverted, and the database returns to the previous stateThat’s the state it was before starting the transaction..

Overview of IndexedDB operations

The steps below demonstrate how we interact with IndexedDB:

  1. Create/Open a database.

  2. Create/Access an object storeThis is similar to tables in SQL in the database.

  3. Make transactions, like storing or requesting data.

  4. Listen for the operation to complete using the respective DOM Event.

Accessing the IndexedDB database

To work in a database, we need to open a connection to a database, and we can do this using the open() method.

For the open() method, we should provide the following:

  • Name of the database

  • An integer value denoting the version of the database

The second point, the database version, is an optional argument. If it’s skipped, the default value will be 1.

Get hands-on with 1200+ tech skills courses.