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:
- 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
};
- 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');
- 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');
- 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');
};
- Complete the transaction: Finally, you need to complete the transaction by calling either the
commit
orabort
method on the transaction object. Thecommit
method applies the changes made within the transaction, while theabort
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