Skip to content
tezvyn:

IndexedDB Versioning: The 'upgradeneeded' Gatekeeper

Source: developer.mozilla.orgHardHow cards are made

IndexedDB Versioning: The 'upgradeneeded' Gatekeeper

IndexedDB uses a version number to manage schema changes. Incrementing the version in indexedDB.open() triggers a special upgradeneeded event, which is the only context where you can create or modify object stores and indexes.

Why it exists

Web apps evolve, and so must their client-side databases. IndexedDB needs a safe, structured way to update its schema (the object stores and indexes) without forcing users to lose their stored data. The versioning system provides this controlled migration path.

The mental model

Think of the version number you pass to indexedDB.open() as a "target schema version." If the browser's stored database is at an older version, IndexedDB triggers the upgradeneeded event, giving you a single, exclusive transaction to run your migration code and bring the schema up to date.

How it works

When you call window.indexedDB.open("myDB", 2), the browser checks the current database version. If it's 1 (or doesn't exist), it blocks all other connections, fires the upgradeneeded event, and waits. Inside this event handler, you receive a database connection (event.target.result) and can run structural changes like db.createObjectStore() and objectStore.createIndex(). The event object also contains oldVersion and newVersion for handling sequential migrations. Once your handler finishes without error, the new schema is committed and the database version is officially set to 2.

When to use it

Use this mechanism whenever you deploy a new version of your web app that requires a change to the client-side database structure. This includes adding an object store for a new feature, adding an index to improve query performance, or removing an obsolete store.

When not to use it

Do not increment the version for routine data operations like adding, reading, or deleting records. The upgradeneeded event is strictly for schema migrations. Trying to perform structural changes outside of this event handler is the most common error; the API will throw an InvalidStateError.

One canonical example

To add a 'users' table, you'd increment your database version. First, const request = indexedDB.open("appDB", 2);. Then, you'd define the schema change in the event handler: request.onupgradeneeded = event => { const db = event.target.result; if (event.oldVersion < 2) { const store = db.createObjectStore("users", { keyPath: "id" }); store.createIndex("email", "email", { unique: true }); } }; This ensures the 'users' store is created only when upgrading from a version less than 2.

Interview question

If you need to add a new index to an existing IndexedDB object store in your web application, what is the correct approach?

  • a.Call db.createIndex() directly on the object store within a standard read/write transaction.
  • b.Increment the database version number when calling indexedDB.open() and perform the createIndex() operation within the upgradeneeded event handler.Correct
  • c.Use a special db.alterSchema() method provided by IndexedDB to modify the store's structure.
  • d.Delete the existing database and recreate it with the new index definition.
Why?

The 'upgradeneeded' event is the exclusive context for making structural changes like adding an index, triggered by incrementing the database version. Attempting structural changes outside this event, such as in a regular transaction, will result in an InvalidStateError.

Just read this? Test yourself on what you have been reading.

Read the original → developer.mozilla.org

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles