0Pricing
JavaScript Academy · Lesson

Opening a Database

Create databases and object stores.

Opening a Database is a free JavaScript Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

Is the “Opening a Database” lesson free?

Yes — the full text of “Opening a Database” is free to read here on the web, and the JavaScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

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