IndexedDB | Introduction

IndexedDB is a key-value database in the browser. It is a NoSQL Database. It is transactional, i.e. if a particular action falls within a transaction, none of the actions of that transaction is applied. This ensures the database remains consistent.
Why use IndexedDB?
- The localStorage was designed for smaller amounts of data and can only store string data for keys and values whereas IndexedDB can work with a large amount of unstructured data, including Files/Blobs.
- It is similar to a JavaScript Object, it can also have nested objects and properties.
- It can be accessed asynchronously, it can be used with service workers which helps to store the data offline and once the device gains Internet access it synchronizes it to the servers.
Using IndexedDB: JavaScript is used to access IndexedDB.
- Open a Database –
// Syntax let request = indexedDB.open(name, version); // name : database name, string value // version : version number, by default 1, positive integer The code to open a database should check if the database exists or not. let request = indexedDB.open("gfg", 1);ÂÂÂrequest.onupgradeneeded =function() {Â Â// Initialize new databaseÂ};ÂÂrequest.onerror =function() {Â Âconsole.error("Unable to access database", request.error);Â Â// Logs error to the console};ÂÂrequest.onsuccess =function() {Â Âlet db = request.result;Â Â// Use existing database};
- Create an object store in the database –
// Syntax let objectStore = db.createObjectStore(name, [keyOption]); // name : object store name // keyOption : object property key let request = indexedDB.open("gfg", 1);ÂÂ// Check if object store exists and// then creates itrequest.onupgradeneeded =function() {Â Âlet db = request.result;Â Âif(!db.objectStoreNames.contains('articles')) {ÂÂ Â Â Âdb.createObjectStore('articles', {keyPath:'id'});Â ÂÂ Â}};
- Starting a transaction –
// Syntax db.transaction(objectStore, type]); // objectStore : objectStore which is to be used // type : readonly or readwrite let transaction = db.transaction("articles","readwrite");ÂÂ// Access an object storelet articles = transaction.objectStore("articles");ÂÂ// Create an objectlet article = { Âid:'Array', Âtopic :'Introduction to Array'};ÂÂ// Add an objectÂlet request = articles.add(article);ÂÂ// Successrequest.onsuccess =function() { Âconsole.log("Article Published", request.result);};ÂÂ// Failedrequest.onerror =function() { Âconsole.log("Article Publish failed", request.error);};
- Close the transaction
let transaction = db.transaction("books","readwrite");ÂÂ// Conducting operations in the transactionÂÂ// When transaction is overtransaction.oncomplete =function() {Â Âconsole.log("Transaction is complete");};We can forcefully abort the transaction by transaction.abort() method. 
The usage and implementation of IndexedDB are simple. This is how you can use IndexedDB in your code through JavaScript.
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, zambiatek Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
 
				 
					



