Skip to content
tezvyn:
Menu

IndexedDB Object Stores: Your Browser's NoSQL Table

Source: developer.mozilla.orgMediumHow cards are made

IndexedDB Object Stores: Your Browser's NoSQL Table

An IndexedDB Object Store is like a NoSQL table in your browser, holding JavaScript objects by key. Use it for offline data like to-do lists or cached API responses. The main footgun: you can only create stores during a database version upgrade.

Why it exists

Browsers needed a robust way to store large amounts of structured data on the client-side, far beyond the simple key-value limitations of localStorage. IndexedDB provides a transactional database system, and Object Stores are the fundamental building blocks for holding that data.

The mental model

An IndexedDB Object Store is the browser equivalent of a table in a SQL database or a collection in a MongoDB database. It's a container for your data records, which are JavaScript objects. A single database can contain multiple object stores, just as a traditional database can have multiple tables.

How it works

You create an object store using db.createObjectStore('storeName', options). This call is only valid inside an onupgradeneeded event handler, which fires when you open the database with a version number higher than its current one. The options object is crucial. You can specify a keyPath, which tells the store to use a property from your data objects as the unique key (e.g., { keyPath: 'id' }). This is an 'in-line key'. If you omit the keyPath, you must provide a separate key each time you add an object, which is an 'out-of-line key'. You can also set autoIncrement: true to have the database automatically generate integer keys for you.

When to use it

Use object stores when you need to persist structured client-side data that exceeds the 5-10MB limit of localStorage. It's perfect for building offline-capable applications, caching complex data from APIs to improve performance, or storing user-generated content like notes or drafts before they are sent to a server.

When not to use it

The API is asynchronous and event-based, making it more complex than localStorage. If you just need to store a user's theme preference or a simple token, IndexedDB is overkill. Stick to localStorage or sessionStorage for simple, small-scale string storage.

One canonical example

To create a 'tasks' store where each task object has a unique title, you would open the database with a new version. Inside the onupgradeneeded event, you'd call db.createObjectStore('tasks', { keyPath: 'taskTitle' });. Now, when you add an object like { taskTitle: 'Buy milk', status: 'pending' }, the store automatically uses 'Buy milk' as the key to identify and index that record. Trying to add another task with the same title would throw a ConstraintError.

Interview question

Which transaction type permits creating an IndexedDB object store or changing its schema?

  • a.At any point, as long as the database transaction is active.
  • b.Only inside a versionchange transaction, normally from onupgradeneeded after the database version is incremented.Correct
  • c.Immediately before the first data object is added to the store.
  • d.When the database connection is successfully established and open.
Why?

Object stores can be created or deleted only inside a versionchange transaction, normally from the onupgradeneeded handler after opening the database with a higher version. Ordinary read/write transactions cannot change the schema.

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

Read the original → developer.mozilla.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

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