Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

IndexedDB in JavaScript

Save an attendance record in JavaScript with IndexedDB.put, then read it after the write transaction completes. This browser database is not a shared server.

AdvancedJavaScript Browser APIsExample 8 of 15

Run this example once in a fresh browser console. The console shows the printed results.

indexeddb.js
1const request = indexedDB.open('AttendancePractice', 1);
2request.onupgradeneeded = () => {
3 const db = request.result;
4 if (!db.objectStoreNames.contains('students')) db.createObjectStore('students', { keyPath: 'id' });
5};
6request.onerror = () => console.error('Database could not open:', request.error?.message);
7request.onsuccess = () => {
8 const db = request.result;
9 const tx = db.transaction('students', 'readwrite');
10 tx.objectStore('students').put({ id: 1, name: 'Asha', present: true });
11 tx.oncomplete = () => {
12 const read = db.transaction('students').objectStore('students').get(1);
13 read.onsuccess = () => { console.log(JSON.stringify(read.result)); db.close(); };
14 read.onerror = () => { console.error('Read failed'); db.close(); };
15 };
16 tx.onabort = () => { console.error('Save failed:', tx.error?.message); db.close(); };
17};

Sample Output

{"id":1,"name":"Asha","present":true}

The stored record was read after the write transaction completed. JSON.stringify produces this text.

In this one

  • Create an object store during upgrade
  • Write an object using put
  • Wait for transaction completion
  • Read a saved record by key

How it works

  1. 1

    Open AttendancePractice at version 1.

  2. 2

    Create students with id as the key during an upgrade.

  3. 3

    Start a readwrite transaction and put the attendance record.

  4. 4

    After completion, open a read transaction and get ID 1.

  5. 5

    Print the record as JSON and close the database connection.

Edge cases

Opening an existing database

Upgrade setup does not run on every visit.

Use onupgradeneeded when changing the database layout. If you change the store design later, raise the version or delete this practice database.

Storage refused or full

Browser settings or storage limits can stop a write.

Handle request errors and transaction aborts. Do not show Saved until the transaction completes.

What's going on

IndexedDB is a database built into the browser for storing structured data. A database keeps records that can be read again later. This example saves one attendance record, waits for the save to finish, and reads it back by student ID.

Run it in a browser console on a normal HTTPS or localhost page. Opening version 1 triggers onupgradeneeded only when the database needs that version's setup. The students object store is the collection of records, and keyPath says that each record's id is its lookup key.

A transaction is a group of database work that succeeds together or fails together. put writes or replaces the record with ID 1. Only after the write transaction completes does a new read transaction request that ID. The JSON output shows the stored values, not just that the database opened. Browser storage belongs to this site and browser profile; it is not a shared school attendance server or a safe place for secrets.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Which value identifies this record?

Pick an answer to check it.

Question 2

When is the write confirmed?

Pick an answer to check it.

Questions people ask

Why use put instead of add?

put replaces an existing record with the same key. add would fail if ID 1 already existed.

Why not read immediately after open?

The database must open first, and this lesson waits for the write transaction to finish before reading.

Will another student see this record on their laptop?

No. This example stores data in your own browser for the current site.