MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
IndexedDB Architecture & Transactions0%
Study Materials
JavaScript for AdvancedProgress 0%
1.1Higher-Order Functions & Currying1.2Function Composition & Pipelining1.3Memoization & Pure Functions1.4Execution Context & Call Stack Deep Dive1.5Scope Chain & Lexical Environment
7.1IndexedDB Architecture & Transactions7.2Storing Complex Objects & Blobs in IndexedDB7.3Cache API & Service Worker Storage7.4Storage Quotas & Persistence Policies
HomeTutorialsJavaScript for AdvancedIndexedDB Architecture & Transactions
Storing Complex Objects & Blobs in IndexedDB

IndexedDB Architecture & Transactions

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study Materials

While localStorage is restricted to ~5MB of synchronous string data, modern Progressive Web Apps (PWAs) and offline-first enterprise applications require gigabytes of structured storage. IndexedDB is a transactional, asynchronous, object-oriented database built natively into every modern web browser.


1. Architecture: Key Concepts

1┌─────────────────────────────────────────────────────────────┐
2│ INDEXEDDB DATABASE │
3├─────────────────────────────────────────────────────────────┤
4│ • Database Name (e.g. 'EnterpriseAppDB') │
5│ • Version Number (e.g. 1, 2, 3) │
6│ ┌───────────────────────────────────────────────────────┐ │
7│ │ Object Store: 'users' (like a SQL Table or Mongo Coll)│ │
8│ │ • Primary Key: 'id' (keyPath) │ │
9│ │ • Index: 'email' (unique: true) │ │
10│ │ • Index: 'role' (unique: false) │ │
11│ └───────────────────────────────────────────────────────┘ │
12└─────────────────────────────────────────────────────────────┘
  • Object Stores: Collections of records storing JavaScript objects, arrays, and binary Blobs.
  • Transactions: Every read or write operation is scoped inside an atomic transaction (readonly or readwrite).
  • Asynchronous: Operates without blocking the main browser thread.

2. Opening a Database & Schema Migration: onupgradeneeded

Schema changes (creating object stores or indexes) can only occur during a database version change in the onupgradeneeded event:

1const DB_NAME = 'EnterpriseDB';
2const DB_VERSION = 1;
3 
4const request = indexedDB.open(DB_NAME, DB_VERSION);
5 
6request.onupgradeneeded = (event) => {
7 const db = event.target.result;
8 console.log('Database upgrade triggered! Initializing schema...');
9 
10 // Create Object Store with keyPath 'id'
11 if (!db.objectStoreNames.contains('customers')) {
12 const store = db.createObjectStore('customers', { keyPath: 'id' });
13
14 // Create searchable indexes
15 store.createIndex('by_email', 'email', { unique: true });
16 store.createIndex('by_country', 'country', { unique: false });
17 }
18};
19 
20request.onsuccess = (event) => {
21 const db = event.target.result;
22 console.log('Database opened successfully!');
23 performOperations(db);
24};
25 
26request.onerror = (event) => {
27 console.error('Failed to open IndexedDB:', event.target.error);
28};

3. CRUD Operations with Atomic Transactions

All operations require opening a transaction:

1function performOperations(db) {
2 // 1. Open a ReadWrite Transaction
3 const transaction = db.transaction(['customers'], 'readwrite');
4 const store = transaction.objectStore('customers');
5 
6 // 2. Insert Record
7 const newCustomer = {
8 id: 'CUST-101',
9 name: 'Sarah Connor',
10 email: 'sarah@skynet-resistance.org',
11 country: 'USA',
12 orders: [100, 250, 40]
13 };
14 
15 const addRequest = store.put(newCustomer); // 'put' inserts or updates!
16 
17 addRequest.onsuccess = () => {
18 console.log('Customer saved to IndexedDB.');
19 };
20 
21 // 3. Query Record via Index
22 const emailIndex = store.index('by_email');
23 const queryRequest = emailIndex.get('sarah@skynet-resistance.org');
24 
25 queryRequest.onsuccess = () => {
26 console.log('Found Customer:', queryRequest.result.name);
27 };
28 
29 // Transaction Lifecycle Events
30 transaction.oncomplete = () => {
31 console.log('All transaction operations committed to disk atomically!');
32 };
33 
34 transaction.onerror = (e) => {
35 console.error('Transaction aborted:', e.target.error);
36 };
37}

4. Modernizing IndexedDB with Promises (idb Library Pattern)

Because native IndexedDB uses legacy DOM event listeners (onsuccess/onerror), wrapping it in Promises or using modern libraries like idb creates cleaner async/await code:

1function getRecordAsync(db, storeName, key) {
2 return new Promise((resolve, reject) => {
3 const tx = db.transaction([storeName], 'readonly');
4 const store = tx.objectStore(storeName);
5 const req = store.get(key);
6 
7 req.onsuccess = () => resolve(req.result);
8 req.onerror = () => reject(req.error);
9 });
10}

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
In which lifecycle event can object stores and indexes be created or modified in IndexedDB?
Question 2
What are the two primary transaction modes used in IndexedDB?
Question 3
How does IndexedDB differ fundamentally from localStorage?
Question 4
What method inserts or overwrites an existing record based on primary key in an Object Store?
Question 5
What is the purpose of an Index in an IndexedDB Object Store?
Next Lesson

Storing Complex Objects & Blobs in IndexedDB

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
ResizeObserver & IntersectionObserver APIsStoring Complex Objects & Blobs in IndexedDB

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master JavaScript for Advanced with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext