Transactions in IndexedDB

Transactions in IndexedDB provide atomicity and isolation, meaning that changes made within a transaction are either applied in full or not at all, and concurrent transactions do not interfere with each other’s data.

To perform a transaction in IndexedDB, you need to follow these steps:

  1. Open a database connection: First, you need to open a connection to the IndexedDB database using the open method. This method takes two parameters: the database name and version. If the specified database does not exist, a new one will be created.
const request = window.indexedDB.open('myDatabase', 1);

request.onsuccess = function(event) {
  const db = event.target.result;
  // Use the database connection
};

request.onerror = function(event) {
  // Handle errors
};
  1. Create a transaction: Once the connection is established, you can create a transaction using the transaction method on the database object. This method takes two arguments: an array of object stores to access and the transaction mode (read-only, read-write, or version change).
const transaction = db.transaction(['myObjectStore'], 'readwrite');
  1. Access object stores: With the transaction object in place, you can access the object stores within that transaction using the objectStore method. This method takes the name of the object store as an argument.
const objectStore = transaction.objectStore('myObjectStore');
  1. Perform data operations: Now that you have access to the object store, you can perform various data operations such as adding, updating, or deleting records.
const data = { id: 1, name: 'John Doe' };

const request = objectStore.add(data);

request.onsuccess = function(event) {
  console.log('Data added successfully');
};

request.onerror = function(event) {
  console.error('Error adding data');
};
  1. Complete the transaction: Finally, you need to complete the transaction by calling either the commit or abort method on the transaction object. The commit method applies the changes made within the transaction, while the abort method rolls back any changes made.
transaction.commit();

Transactions in IndexedDB are essential for maintaining data integrity and avoiding conflicts when working with large datasets. By following the steps outlined above, you can leverage the power of transactions to ensure smooth and consistent data operations in your IndexedDB-driven web applications.

#indexeddb #transactions