Text of web.dev › articles › indexeddb as Styx crawled it on Nov 18, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

Work with IndexedDB | Articles | web.dev

Skip to main content English Deutsch Español – América Latina Français Indonesia Italiano Polski Português – Brasil Tiếng Việt Türkçe Русский עברית العربيّة فارسی हिंदी বাংলা ภาษาไทย 中文 – 简体 中文 – 繁體 日本語 한국어 Sign in Articles Modern Web Guidance is now stable! Upgrade your coding agents with expert-vetted web development best practices.

Read the announcement . Home Articles Work with IndexedDB Stay organized with collections Save and categorize content based on your preferences. This guide covers the basics of the IndexedDB API . We're using Jake Archibald's IndexedDB Promised library, which is very similar to the IndexedDB API, but uses promises, which you can await for more concise syntax. This simplifies the API while maintaining its structure. What is IndexedDB?

IndexedDB is a large-scale, NoSQL storage system that allows storage of just about anything in the user's browser. In addition to the usual search, get, and put actions, IndexedDB also supports transactions, and it's well suited for storing large amounts of structured data. Each IndexedDB database is unique to an origin (typically the site domain or subdomain), meaning it can't access or be accessed by any other origin.

Its data storage limits are usually large, if they exist at all, but different browsers handle limits and data eviction differently. See the Further reading section for more information. IndexedDB terms Database The highest level of IndexedDB. It contains the object stores, which in turn contain the data you want to persist. You can create multiple databases with whatever names you choose. Object store An individual bucket to store data, similar to tables in relational databases.

Typically, there is one object store for each type (not JavaScript data type) of data you're storing. Unlike in database tables, the JavaScript data types of data in a store don't need to be consistent. For example, if an app has a people object store containing information about three people, those people's age properties could be 53 , 'twenty-five' , and unknown . Index A kind of object store for organizing data in another object store (called the reference object store) by an individual property of the data.

The index is used to retrieve records in the object store by this property. For example, if you're storing people, you might want to fetch them later by their name, age, or favorite animal. Operation An interaction with the database. Transaction A wrapper around an operation or group of operations that ensures database integrity. If one of the actions in a transaction fails, none of them are applied and the database returns to the state it was in before the transaction began.

All read or write operations in IndexedDB must be part of a transaction. This allows atomic read-modify-write operations without the risk of conflicts with other threads acting on the database at the same time. Cursor A mechanism for iterating over multiple records in a database. How to check for IndexedDB support IndexedDB is almost universally supported . However, if you're working with older browsers, it's not a bad idea to feature-detect support just in case.

The easiest way is to check the window object: function indexedDBStuff () { // Check for IndexedDB support: if ( ! ( 'indexedDB' in window )) { // Can't use IndexedDB console . log ( "This browser doesn't support IndexedDB" ); return ; } else { // Do IndexedDB stuff here: // ... } } // Run IndexedDB code: indexedDBStuff (); How to open a database With IndexedDB, you can create multiple databases with any names you choose.

If a database doesn't exist when you try to open it, it's' automatically created. To open a database, use the openDB() method from the idb library: import { openDB } from 'idb' ; async function useDB () { // Returns a promise, which makes `idb` usable with async-await. const dbPromise = await openDB ( 'example-database' , version , events ); } useDB (); This method returns a promise that resolves to a database object.

When using the openDB() method, provide a name, version number, and an events object to set up the database. Here's an example of the openDB() method in context: import { openDB } from 'idb' ; async function useDB () { // Opens the first version of the 'test-db1' database. // If the database does not exist, it will be created. const dbPromise = await openDB ( 'test-db1' , 1 ); } useDB (); Place the check for IndexedDB support at the top of the anonymous function.

This exits the function if the browser doesn't support IndexedDB. If the function can continue, it calls the openDB() method to open a database named 'test-db1' . In this example, the optional events object has been left out to keep things simple, but you need to specify it to do any meaningful work with IndexedDB. How to work with object stores An IndexedDB database contains one or more object stores , which each have a column for a key, and another column for the data associated with that key.

Create object stores A well structured IndexedDB database should have one object store for each type of data that needs to be persisted. For example, a site that persists user profiles and notes might have a people object store containing person objects, and a notes object store containing note objects. To ensure database integrity, you can only create or remove object stores in the events object in an openDB() call. The events object exposes a upgrade() method that lets you create object stores.

Call the createObjectStore() method inside the upgrade() method to create the object store: import { openDB } from 'idb' ; async function createStoreInDB () { const dbPromise = await openDB ( 'example-database' , 1 , { upgrade ( db ) { // Creates an object store: db . createObjectStore ( 'storeName' , options ); } }); } createStoreInDB (); This method takes the name of the object store and an optional configuration object that lets you define various properties for the object store.

The following is an example of how to use createObjectStore() : import { openDB } from 'idb' ; async function createStoreInDB () { const dbPromise = await openDB ( 'test-db1' , 1 , { upgrade ( db ) { console . log ( 'Creating a new object store...' ); // Checks if the object store exists: if ( ! db . objectStoreNames . contains ( 'people' )) { // If the object store does not exist, create it: db . createObjectStore ( 'people' ); } } }); } createStoreInDB (); In this example, an events object is passed to the openDB() method to create the object store, and as before, the work of creating the object store is done in the event object's upgrade() method.

However, because the browser throws an error if you try to create an object store that already exists, we recommend wrapping the createObjectStore() method in an if statement that checks whether the object store exists. Inside the if block, call createObjectStore() to create an object store named 'firstOS' . How to define primary keys When you define object stores, you can define how data is uniquely identified in the store using a primary key.

You can define a primary key either by defining a key path or by using a key generator. A key path is a property that always exists and contains a unique value. For example, in the case of a people object store, you might choose the email address as the key path: import { openDB } from 'idb' ; async function createStoreInDB () { const dbPromise = await openDB ( 'test-db2' , 1 , { upgrade ( db ) { if ( ! db . objectStoreNames . contains ( 'people' )) { db . createObjectStore ( 'people' , { keyPath : 'email' }); } } }); } createStoreInDB (); This example creates an object store called 'people' and assigns the email property as the primary key in the keyPath option.

You can also use a key generator such as autoIncrement . The key generator creates a unique value for every object added to the object store. By default, if

The rest of this page is on the live site.