JavaScript Academy · Lesson

Opening a Database

Create databases and object stores.

Lesson 1 of 413 steps

Opening a Database is a free JavaScript Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, and your progress syncs across the web and the CoddyKit app. The JavaScript Academy course includes 4 lessons in total.

What Is IndexedDB?

IndexedDB is a large-capacity, transactional database built into the browser. Unlike localStorage (small, string-only, synchronous), it stores structured objects, supports indexes, and works asynchronously.

Opening a Database

You open (or create) a database with indexedDB.open(name, version). It returns a request object whose events report success or failure.

const request = indexedDB.open('notes-db', 1);

The Request Events

An open request fires events you handle:

  • onsuccess — database is ready
  • onerror — opening failed
  • onupgradeneeded — schema must be created or upgraded
request.onsuccess = (e) => {
  const db = e.target.result;
  console.log('database opened');
};
request.onerror = (e) => {
  console.log('open failed:', e.target.error);
};

onupgradeneeded

onupgradeneeded runs when the database is first created or its version number increases. This is the only place you can change the schema (create or delete object stores and indexes).

request.onupgradeneeded = (e) => {
  const db = e.target.result;
  // create object stores here
};

Object Stores

An object store is like a table: a container of records. Create one with db.createObjectStore(name, options) inside onupgradeneeded.

request.onupgradeneeded = (e) => {
  const db = e.target.result;
  db.createObjectStore('notes', { keyPath: 'id' });
};

The keyPath

The keyPath tells IndexedDB which property of each object is its primary key. Stored objects must have that property, and keys must be unique.

db.createObjectStore('users', { keyPath: 'email' });
// Each stored user object must have a unique 'email'.

Auto-Incrementing Keys

If your objects have no natural key, let IndexedDB generate one with autoIncrement: true. Each new record gets the next integer.

db.createObjectStore('logs', {
  keyPath: 'id',
  autoIncrement: true
});
// id is assigned automatically on add().

Checking for Existing Stores

Since onupgradeneeded runs on every version bump, guard creation with db.objectStoreNames.contains to avoid errors on re-runs.

request.onupgradeneeded = (e) => {
  const db = e.target.result;
  if (!db.objectStoreNames.contains('notes')) {
    db.createObjectStore('notes', { keyPath: 'id' });
  }
};

Holding the db Reference

Save the database object from onsuccess so later operations can use it. All reads and writes go through this db instance.

let db;
request.onsuccess = (e) => {
  db = e.target.result; // reuse for transactions
};

Handling Blocked Opens

If another tab holds an older version open, an upgrade may be blocked. Listen for onblocked and ask the user to close other tabs.

request.onblocked = () => {
  console.log('Close other tabs to upgrade the database.');
};

The Opening Flow

Opening IndexedDB: call open with a name and version, create your schema in onupgradeneeded, then grab the db in onsuccess. From there you run transactions to read and write data.

Quick Check

Test your understanding of opening a database.

Recap

You learned to open an IndexedDB database:

  • indexedDB.open(name, version) returns a request.
  • Create object stores in onupgradeneeded only.
  • keyPath sets the primary key; autoIncrement generates keys.
  • Save the db from onsuccess for later use.
  • Handle onerror and onblocked.

Next, transactions and CRUD operations.

Free to start

Learn JavaScript with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
59
Lessons
200

Frequently Asked Questions

Is the “Opening a Database” lesson free?

Yes — any 3 lessons of the JavaScript Academy learning path, including “Opening a Database”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The JavaScript Academy course includes 4 lessons in total.

What will I learn in “Opening a Database”?

Create databases and object stores. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start JavaScript Academy?

No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

How long does the “Opening a Database” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this JavaScript Academy lesson?

Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Opening a Database
  2. Transactions and CRUD
  3. Indexes and Queries
  4. Versioning and Upgrades
← Back to JavaScript Academy